需要jsx文件而不指定扩展名 [英] Require jsx files without specifying extension
问题描述
我正在使用 browserify 和 watchify ,并且想要 require()
文件(默认扩展名除外) .js
和 .json
而不指定扩展名,例如:
I am using browserify and watchify, and would like to require()
files other than the default extensions .js
and .json
without specifying the extension, for instance:
// Not ideal (tedious)
var Carousel = require('./components/Carousel/Carousel.jsx')
// Ideal
var Carousel = require('./components/Carousel/Carousel')
我已经尝试了 browserify 文档中指定的-extension = EXTENSION
:
I have tried --extension=EXTENSION
as specified in the browserify documentation:
"scripts": {
"build": "browserify ./src/App.js --transform [ reactify --es6 ] > dist/script.js -v -d --extension=jsx",
"watch": "watchify ./src/App.js --transform [ reactify --es6 ] -o dist/script.js -v -d --extension=jsx"
},
但是我看不到任何变化。这可能吗?正确的方法是什么?
However I don't see any change. Is this possible? What would be the right way to do this?
推荐答案
编辑(2015年4月27日):
我只是注意到在问题中,我为扩展名
设置了无效的参数,例如:
Edit (April 27, 2015):
I just noticed that in the question, I had an invalid argument for extension
, like so:
"watch": "watchify ./src/App.js --extension=jsx -o dist/script.js -v -d"
应该是(请注意。
(点)在中- -extension = .jsx
):
It should be (notice the .
(dot) in --extension=.jsx
):
"watch": "watchify ./src/App.js --extension=.jsx -o dist/script.js -v -d"
原始答案:
在软件包中添加
是为 browserify 做的,但不是 watchify 的。 browserify
选项。 json
Adding in the browserify
option to package.json
did it for browserify but not for watchify.
"scripts": {
"build": "browserify ./src/App.js > dist/script.js -v -d",
"watch": "watchify ./src/App.js -o dist/script.js -v -d"
},
"browserify": {
"extension": [ "jsx" ],
"transform": [ [ "reactify", { "es6": true } ] ]
}
为 watch
命令添加了扩展名
选项,使 watchify 工作。
Add in the extension
option for the watch
command made watchify work.
"watch": "watchify ./src/App.js --extension=.jsx -o dist/script.js -v -d"
但是,非DRY。我想使我的命令尽可能简短,但是〜哦~~。
However, non-DRY. I'd like to keep my commands short as possible, but ~oh well~.
这篇关于需要jsx文件而不指定扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!