强制Browserify转换依赖关系?

我正在处理两个Node包,我们称之为Library和Consumer。 库负责在浏览器中渲染一堆东西。 所有消费者都是import Library from 'library'并调用Library(someConfigHere) – 它基本上只是一个testing,以确保库正在做我所期望的浏览器。

我已经npm link到消费者的库,并试图在Consumer上运行Browserify,但我得到这个错误: ParseError: 'import' and 'export' may appear only with 'sourceType: module' 。 库确实包含一个ES6 export语句,所以我猜测,Browserify只针对消费者而不是库运行。

所以我的问题是: 有没有办法强制Browserify转换依赖关系

这是我的package.json

 { "name": "consumer", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "budo index.js --port $PORT", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "devDependencies": { "babel-preset-es2015": "^6.13.2", "babel-preset-react": "^6.11.1", "babelify": "^7.3.0", "browserify-shim": "^3.8.12" }, "browserify": { "transform": [ "babelify" ] }, "babel": { "presets": [ "es2015", "react" ] } } 

这是消费者的index.js

 import Library from 'library' // <= this is what isn't getting babelified console.log(Library); 

这是图书馆的index.js

 export default (config) => { console.log('Testing testing') } 

Browserify变换可以configuration为全局variables,这意味着它们也将被应用于node_modules文件。

configuration是每转换。 用babelify,你可以像这样configuration它:

 browserify().transform("babelify", { global: true }) 

或者,如果你正在使用命令行,像这样:

 browserify ... -t [ babelify --global ] ... 

或者,在package.jsonconfiguration它,应该是这样的(注意添加的方括号):

 "browserify": { "transform": [ ["babelify", { "global": true }] ] } 

Babelify也实现了ignore选项,所以可以将其configuration为仅转换您node_modules中的文件。 这里有更多的信息。

另一个解决scheme是在你的library模块的package.json包含一个类似的babelify / babelifyconfiguration。 当处理依赖关系时,Browserify将检查所有依赖的pacakge.json文件进行转换,并将应用所有configuration。