【发布时间】:2016-05-05 18:44:49
【问题描述】:
我正在尝试在我的项目中包含 https://www.npmjs.com/package/react-bootstrap-typeahead,但是当我运行我的 grunt browserify 任务时,我收到了 ParseError: Unexpected token 错误。完整的消息在这里:
Running "browserify:dist" (browserify) task
>> /path/to/project/node_modules/react-bootstrap-typeahead/css/Typeahead.css:1
>> .bootstrap-typeahead .dropdown-menu {
>> ^
>> ParseError: Unexpected token
Warning: Error running grunt-browserify. Use --force to continue.
Aborted due to warnings.
问题在于 react-bootstrap-typeahead 源 js 中有一个 require('/some/file.css')。
这是我的繁琐的 browserify 任务:
browserify: {
dist: {
cwd: 'build',
options: {
transform : ['browserify-css'],
alias: {
'index' : './dist/index.js',
'root' : './dist/containers/root.js',
'designs' : './dist/components/designs.js',
'addMutationForm' : './dist/components/addMutationForm.js',
'ProteinChecker': './dist/containers/ProteinChecker.js',
'configureStore': './dist/configureStore.js',
'actions' : './dist/actions.js',
'reducers' : './dist/reducers.js',
'utils': './dist/utils.js',
},
require: [
'./node_modules/isomorphic-fetch',
'./node_modules/jquery',
'./node_modules/react',
'./node_modules/react-dom',
'./node_modules/bootstrap',
'./node_modules/redux',
'./node_modules/babel-polyfill',
'./node_modules/redux-logger',
'./node_modules/redux-thunk',
'./node_modules/underscore',
'./node_modules/redux-form',
'./node_modules/react-bootstrap-typeahead'
]
},
src: ['./dist/*.js', './dist/*/*.js'],
dest: './public/build/app.js'
}
}
我在尝试解决问题时遇到了一些事情: 一个类似的 SO(尚未回答)问题,但使用的是 webpack,而不是 grunt+browserify,所以我认为不适用: Using react-bootstrap-typeahead generating CSS errors 以及 react-bootstrap-typeahead 项目的 github 页面上的一个已关闭问题,但没有解决通过 grunt 使用的问题: https://github.com/ericgio/react-bootstrap-typeahead/issues/2 这建议使用 browserify-css 转换。
我试图通过在选项字段中添加transform : ['browserify-css'] 来使用此转换,但这不起作用。我仍然收到完全相同的错误消息。
我尝试在命令行上使用 browserify 和 browserify-css 转换来捆绑这个库,然后包含该捆绑包,但这会导致更多错误,我认为这与相对导入有关(而且我认为这无论如何都不是一个好的解决方案,因为如果我理解 browserify 正确,它最终会包括两次反应之类的东西,一次是我在命令行上制作的预输入包,另一次是实际的应用程序包和那么最终的 js 文件是巨大的!)。
关于如何解决这个问题的任何想法?
提前致谢!
【问题讨论】:
-
请注意,模块的 v0.5.x(当前为 v0.10.4)不再需要直接在 JS 文件中使用 CSS,因此这应该不再是问题。
标签: javascript css reactjs browserify grunt-browserify