Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Last active December 18, 2015 05:59
Show Gist options
  • Save thomasboyt/5736652 to your computer and use it in GitHub Desktop.
Save thomasboyt/5736652 to your computer and use it in GitHub Desktop.
concat: {
amd: {
src: "tmp/**/*.amd.js",
dest: "dist/my_library.amd.js"
}
}
transpile: {
amd: {
type: 'amd',
files: [{
expand: true,
cwd: 'lib/',
src: ['**/*.js'],
dest: 'tmp/',
ext: '.amd.js'
}]
}
}
grunt.registerMultiTask('browser', "Export a module to the window", function() {
var opts = this.options();
this.files.forEach(function(f) {
var output = ["(function(globals) {"];
output.push.apply(output, f.src.map(grunt.file.read));
output.push(grunt.template.process(
'window.<%= namespace %> = requireModule("<%= barename %>");', {
data: {
namespace: opts.namespace,
barename: opts.barename
}
}));
output.push('})(window);');
grunt.file.write(f.dest, grunt.template.process(output.join("\n")));
});
});
// ...
grunt.initConfig({
// ...
browser: {
dist: {
src: ["vendor/loader.js", "dist/my_library.amd.js"],
dest: "dist/my_library.js",
options: {
barename: "my_library",
namespace: "MyLibrary"
}
}
}
})
transpile: {
// ...
commonjs: {
type: 'cjs',
files: [{
expand: true,
cwd: 'lib/',
src: ['my_library/*.js'],
dest: 'dist/commonjs/',
ext: '.js'
},
{
src: ['lib/my_library.js'],
dest: 'dist/commonjs/main.js'
}]
}
}
module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-es6-module-transpiler");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.initConfig({
transpile: {
amd: {
type: 'amd',
files: [{
expand: true,
cwd: 'lib/',
src: ['**/*.js'],
dest: 'tmp/',
ext: '.amd.js'
}]
},
commonjs: {
type: 'cjs',
files: [{
expand: true,
cwd: 'lib/',
src: ['my_library/*.js'],
dest: 'dist/commonjs/',
ext: '.js'
},
{
src: ['lib/my_library.js'],
dest: 'dist/commonjs/main.js'
}]
}
},
concat: {
amd: {
src: "tmp/**/*.amd.js",
dest: "dist/my_library.amd.js"
},
browser: {
src: ["vendor/loader.js", "dist/my_library.amd.js"],
dest: "dist/my_library.js"
}
}
});
grunt.registerTask("default", ["transpile", "concat"]);
}
import { shout } from "./my_library/shout";
import { ssshh } from "./my_library/ssshh";
export { shout, ssshh };
{
"name": "my-library",
"main": "dist/commonjs/main.js"
}
var shout = function(s) {
return s.toUpperCase();
}
export shout;
var ssshh = function(s) {
return s.toLowerCase();
}
export ssshh;
@jgable
Copy link

jgable commented Feb 26, 2014

A word of caution about using grunt.template.process here. It will mangle your underscore template strings if you have any in your module code.

We've switched it to

grunt.file.write(f.dest, output.join("\n"));

@xcatliu
Copy link

xcatliu commented Jul 4, 2014

running grunt transpile:commonjs has the error as follow:

Running "transpile:commonjs" (transpile) task
>> lib/my_library/shout.js: [L5:C8] Unexpected identifier
Warning: Error compiling lib/my_library/shout.js Use --force to continue.

Aborted due to warnings.

line 5 should be revised as export { shout }, and then grunt running successed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment