我有一组相互依赖的Node.js模块,我已经将它们构建为ES6模块,理想情况下,我希望能够指定一个模块作为入口点,并将这些模块(使用grunt)构建到一个Node应用程序所需的单个文件中。
grunt-babel似乎不能处理这个包装。
我知道browserify可以为浏览器做这件事,我也知道browserify可以包含Node模块,但我还不能弄清楚如何让browserify将单个模块入口点转换为需要的Node模块。
因此,如果我的源文件(和入口点) src/hello.js是:
import world from './world.js';
export default function () {console.log('Hello' + world + '!');};和src/world.js是:
export default 'world';我希望它能够从一个普通的Node应用程序中使用它,比如:
var hw = require('./dest/hello-world.js');
hw();我的grunt文件应该是什么样子的?
发布于 2016-06-02 05:14:42
原来我有两个问题:
在我的.babelrc文件中,我需要:
{
"presets": ["es2015"],
"plugins": ["add-module-exports"]
}我需要的Grunt文件中的...whereas:
browserify: {
myNode: {
options: {
transform: [['babelify', {sourceMaps: true}]],
browserifyOptions: {
standalone: 'dummyPlaceholder'
}
// Depending on your needs, you may also
// need `node: true` here or the options
// in the 2nd argument of the `browserify` call
// (as I required to allow GLOBAL.window = GLOBAL
// injection)
// at https://github.com/substack/node-browserify/issues/1277#issuecomment-115198436
},
files: {
'dist/<%= pkg.name%>-node.js': 'src/node.js'
}
}
},插件"add-module-exports"是必要的,以避免改变Babel,这需要像var mod = require('mod').default;这样的调用,而不仅仅是var mod = require('mod');。
standalone属性实际上不是用来创建全局的,因为它是在模块环境中使用的(Node/CommonJS),但是该属性的存在是触发导出所必需的。
https://stackoverflow.com/questions/37561111
复制相似问题