需要jsx文件而不指定扩展名 [英] Require jsx files without specifying extension

查看:89
本文介绍了需要jsx文件而不指定扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 选项。 json 是为 browserify 做的,但不是 watchify 的。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆