Last active
December 18, 2015 05:59
-
-
Save thomasboyt/5736652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
concat: { | |
amd: { | |
src: "tmp/**/*.amd.js", | |
dest: "dist/my_library.amd.js" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transpile: { | |
amd: { | |
type: 'amd', | |
files: [{ | |
expand: true, | |
cwd: 'lib/', | |
src: ['**/*.js'], | |
dest: 'tmp/', | |
ext: '.amd.js' | |
}] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { shout } from "./my_library/shout"; | |
import { ssshh } from "./my_library/ssshh"; | |
export { shout, ssshh }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "my-library", | |
"main": "dist/commonjs/main.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var shout = function(s) { | |
return s.toUpperCase(); | |
} | |
export shout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ssshh = function(s) { | |
return s.toLowerCase(); | |
} | |
export ssshh; |
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
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