强制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.json
configuration它,应该是这样的(注意添加的方括号):
"browserify": { "transform": [ ["babelify", { "global": true }] ] }
Babelify也实现了ignore
选项,所以可以将其configuration为仅转换您node_modules
中的文件。 这里有更多的信息。
另一个解决scheme是在你的library
模块的package.json
包含一个类似的babelify
/ babelify
configuration。 当处理依赖关系时,Browserify将检查所有依赖的pacakge.json
文件进行转换,并将应用所有configuration。