How to specify browserify extensions in package.json?

半城伤御伤魂 提交于 2019-12-22 10:20:03

问题


In package.json:

...
"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [ ".cjsx", ".coffee", ".js", ".json" ],
  "extensions": [ ".cjsx", ".coffee", ".js", ".json" ]
},
...

When using browserify transform option works as expected, however browserify is not seeing extension(s) options - it throws error and I have to pass extension options manually to browserify...


in gulpfile.coffee

b = browserify
  entries: './' # ./ = root = directory where package.json is
  debug: true
b.bundle()
.pipe(source('client.js'))
.pipe(buffer())
.pipe(gulp.dest(distDir))

in package.json

"browser": "src/client/client",
"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [
    "cjsx",
    "coffee",
    "js",
    "json"
  ]
},

src/client/client.cjsx

otherModule = require './other-module' # other-module.cjsx
  1. When I remove coffee-reactify from transforms in package.json then browserify throws error Parsing file .../src/client/client.cjsx: Unexpected token (2:16)

  2. When I put back coffee-reactify to transforms in package.json, then browserify successfully parses client.cjsx as long as I wont require any other .cjsx files from inside of client.cjsx. So for the example code of client.cjsx above browserify throws error: Cannot find module './other-module' from '/src/client - browserify still does not recognize extension...

So browserify reads package.json (recognizes package.browserify.transforms and package.browser fields but it does not recognize extensions)


回答1:


We were running into the same problem. We were able to get it working by adding extensions to the browserify gulp function call.

browserify({
  entries: "src/index.coffee",
  extensions: [".cjsx", ".coffee", ".js", ".json" ]
})

We don't have it in the package.json at all, just in the gulp command.




回答2:


Try this:

"browserify": {
  "transform": [
    "coffee-reactify"
  ],
  "extension": [ 
    "cjsx", 
    "coffee", 
    "js", 
    "json" 
  ]
},

Remove the . dots. Take a look at this question.



来源:https://stackoverflow.com/questions/30969318/how-to-specify-browserify-extensions-in-package-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!