Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edgar-maciel/b463e8e638a3f4a24734 to your computer and use it in GitHub Desktop.
Save edgar-maciel/b463e8e638a3f4a24734 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment