Reputation: 14345
I have a set of interdependent Node.js modules that I have been building as ES6 modules, and I would ideally like to be able to specify a single module as an entry point and have these built (using grunt) into a single file which can be required by a Node application.
grunt-babel does not seem to be able to handle this packaging.
I know that browserify can do this for the browser, and I know browserify can include Node modules, but I haven't been able to figure out how to have browserify transform the single module entry point into a require-able Node module.
So, if my source file (and entry point), src/hello.js
were:
import world from './world.js';
export default function () {console.log('Hello' + world + '!');};
and src/world.js
were:
export default 'world';
I'd like it to be able to use it from a normal Node application like:
var hw = require('./dest/hello-world.js');
hw();
What would my grunt file need to look like?
Upvotes: 0
Views: 62
Reputation: 14345
Turns out I had two problems:
In my .babelrc
file I needed:
{
"presets": ["es2015"],
"plugins": ["add-module-exports"]
}
...whereas in my Grunt file I needed:
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'
}
}
},
The plugin "add-module-exports" was necessary to avoid a change in Babel which necessitates calls like var mod = require('mod').default;
instead of just var mod = require('mod');
.
The standalone
property is not actually used here to create a global since it is being used in a module environment (Node/CommonJS), but the property's presence is required to trigger an export.
Upvotes: 0