now generating systemjs and commonjs entrypoints in the following variations:

- <module type>.js (uses subordinate require or imports)
- <module type>-all.js (concat version of all subordinates)
- <module type>-all.min.js minified all version.

In addition, don't forget that the source es6 is also available.

Docs aren't done yet or integrated.
This commit is contained in:
Kevin Ross 2015-12-28 22:06:48 -06:00
parent 77e58a2335
commit 0835304d9f
4 changed files with 98 additions and 93 deletions

View File

@ -56,7 +56,7 @@ module.exports = function (grunt) {
] ]
}); });
var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js'); var moduleGenerator = require('./grunt/module-generator.js');
var configBridge = grunt.file.readJSON('./grunt/configBridge.json', {encoding: 'utf8'}); var configBridge = grunt.file.readJSON('./grunt/configBridge.json', {encoding: 'utf8'});
// dynamically create js file list (we do this for several different directories) // dynamically create js file list (we do this for several different directories)
@ -115,15 +115,6 @@ module.exports = function (grunt) {
' * Copyright 2014-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' + ' * Copyright 2014-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
' * Licensed under MIT (https://github.com/FezVrasta/bootstrap-material-design/blob/master/LICENSE)\n' + ' * Licensed under MIT (https://github.com/FezVrasta/bootstrap-material-design/blob/master/LICENSE)\n' +
' */\n', ' */\n',
jqueryCheck: 'if (typeof jQuery === \'undefined\') {\n' +
' throw new Error(\'Bootstrap Material Design\\\'s JavaScript requires jQuery\')\n' +
'}\n',
jqueryVersionCheck: '+function ($) {\n' +
' var version = $.fn.jquery.split(\' \')[0].split(\'.\')\n' +
' if ((version[0] < 2 && version[1] < 9) || (version[0] == 1 && version[1] == 9 && version[2] < 1) || (version[0] >= 3)) {\n' +
' throw new Error(\'Bootstrap Material Design\\\'s JavaScript requires at least jQuery v1.9.1 but less than v3.0.0\')\n' +
' }\n' +
'}(jQuery);\n\n',
// Task configuration. // Task configuration.
clean: { clean: {
@ -135,7 +126,7 @@ module.exports = function (grunt) {
babel: { babel: {
options: { options: {
sourceMap: true, sourceMap: true,
presets: ['babel-preset-es2015-rollup'] // the following is the es2015 preset minus the commonjs requirement presets: ['es2015'] // the following is the es2015 preset minus the commonjs requirement
}, },
core: { core: {
files: coreFileMap('dist/js/demoduled/', 'js/src/') files: coreFileMap('dist/js/demoduled/', 'js/src/')
@ -143,6 +134,12 @@ module.exports = function (grunt) {
docs: { docs: {
files: docsFileMap() files: docsFileMap()
}, },
systemjs: {
options: {
plugins: ['transform-es2015-modules-systemjs']
},
files: coreFileMap('dist/js/systemjs/', 'js/src/')
},
umd: { umd: {
options: { options: {
plugins: ['transform-es2015-modules-umd'] plugins: ['transform-es2015-modules-umd']
@ -181,37 +178,29 @@ module.exports = function (grunt) {
stamp: { stamp: {
options: { options: {
banner: '<%= banner %>\n<%= jqueryCheck %>\n<%= jqueryVersionCheck %>\n+function ($) {\n', banner: '<%= banner %>\n'
footer: '\n}(jQuery);'
//banner: '<%= banner %>\n'
}, },
core: { core: {
files: { files: {
src: 'dist/js/<%= pkg.name %>.js' src: 'dist/js/*.js'
} }
} }
}, },
concat: { concat: {
dist_demoduled: { options: {
options: { stripBanners: false,
stripBanners: false, sourceMap: true
sourceMap: true },
}, systemjs: {
dest: 'dist/js/<%= pkg.name %>.js', src: coreFileArray('dist/js/systemjs/'),
src: coreFileArray('dist/js/demoduled/') dest: 'dist/js/system-all.js'
},
commonjs: {
src: coreFileArray('dist/js/umd/'),
dest: 'dist/js/common-all.js'
} }
}, },
lineremover: {
core: {
options: {
exclusionPattern: /^(import|export)/g
},
files: coreFileMap('dist/js/demoduled/', 'dist/js/demoduled/')
},
},
uglify: { uglify: {
options: { options: {
compress: { compress: {
@ -220,17 +209,21 @@ module.exports = function (grunt) {
mangle: false, mangle: false,
preserveComments: /^!|@preserve|@license|@cc_on/i preserveComments: /^!|@preserve|@license|@cc_on/i
}, },
core: { 'systemjs-all': {
src: 'dist/js/<%= pkg.name %>.js', src: 'dist/js/system-all.js',
dest: 'dist/js/<%= pkg.name %>.min.js' dest: 'dist/js/system-all.min.js'
}, },
docs: { 'commonjs-all': {
options: { src: 'dist/js/common-all.js',
compress: false dest: 'dist/js/common-all.min.js'
}, },
src: configBridge.paths.docsJs, // docs: {
dest: 'docs/assets/js/docs.min.js' // options: {
} // compress: false
// },
// src: configBridge.paths.docsJs,
// dest: 'docs/assets/js/docs.min.js'
// }
}, },
qunit: { qunit: {
@ -338,8 +331,6 @@ module.exports = function (grunt) {
'!js/demoduled/**/*', '!js/demoduled/**/*',
'!js/umd', '!js/umd',
'!js/umd/**/*', '!js/umd/**/*',
//'!js/commonjs',
//'!js/commonjs/**/*',
//'!js/systemjs', //'!js/systemjs',
//'!js/systemjs/**/*', //'!js/systemjs/**/*',
'!js/npm.js' '!js/npm.js'
@ -603,12 +594,15 @@ module.exports = function (grunt) {
grunt.registerTask('dist-js', [ grunt.registerTask('dist-js', [
'clean:dist-js', 'clean:dist-js',
'eslint', 'eslint',
'babel:core', 'babel:umd',
'lineremover:core', 'babel:systemjs',
'concat',
'stamp',
'uglify:core',
'commonjs', 'commonjs',
'systemjs',
'concat:commonjs',
'concat:systemjs',
'stamp',
'uglify:commonjs-all',
'uglify:systemjs-all',
'copy:dist-to-docs' 'copy:dist-to-docs'
]); ]);
@ -630,14 +624,12 @@ module.exports = function (grunt) {
// Default task. // Default task.
grunt.registerTask('default', ['clean:dist', 'test']); grunt.registerTask('default', ['clean:dist', 'test']);
grunt.registerTask('commonjs', ['babel:umd', 'npm-js']); grunt.registerTask('commonjs', 'Generate npm-js/commonjs entrypoint module.', function () {
moduleGenerator.commonJs(grunt, coreFileArray('./umd/'), 'dist/js/common.js');
});
grunt.registerTask('npm-js', 'Generate npm-js entrypoint module in dist dir.', function () { grunt.registerTask('systemjs', 'Generate systemjs entrypoint module.', function () {
var srcFiles = Object.keys(grunt.config.get('babel.umd.files')).map(function (filename) { moduleGenerator.systemJs(grunt, coreFileArray('./systemjs/'), 'dist/js/system.js');
return './' + path.join('umd', path.basename(filename))
})
var destFilepath = 'dist/js/npm.js';
generateCommonJSModule(grunt, srcFiles, destFilepath);
}); });
//------ //------
@ -688,8 +680,7 @@ module.exports = function (grunt) {
}); });
}); });
grunt.registerTask('debug', function () { //grunt.registerTask('debug', function () {
// console.log(coreFileArray('dist/js/demoduled/'));
console.log(coreFileArray('dist/js/demoduled/')); //});
});
}; };

View File

@ -1,29 +0,0 @@
/*!
* Bootstrap Grunt task for the CommonJS module generation
* http://getbootstrap.com
* Copyright 2014-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
'use strict';
var fs = require('fs');
var path = require('path');
var COMMONJS_BANNER = '// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.\n';
module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) {
var destDir = path.dirname(destFilepath);
function srcPathToDestRequire(srcFilepath) {
return 'require(\'' + srcFilepath.replace(/\\/g, '/') + '\')';
}
var moduleOutputJs = COMMONJS_BANNER + srcFiles.map(srcPathToDestRequire).join('\n');
try {
fs.writeFileSync(destFilepath, moduleOutputJs);
} catch (err) {
grunt.fail.warn(err);
}
grunt.log.writeln('File ' + destFilepath.cyan + ' created.');
};

45
grunt/module-generator.js Normal file
View File

@ -0,0 +1,45 @@
module.exports = (function () {
'use strict';
var fs = require('fs');
var path = require('path');
var COMMONJS_BANNER = '// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.\n';
var commonJs = function (grunt, srcFiles, destFilepath) {
function moduleRequireStatement(srcFilepath) {
return 'require(\'' + srcFilepath.replace(/\\/g, '/') + '\')';
}
var moduleOutputJs = COMMONJS_BANNER + srcFiles.map(moduleRequireStatement).join('\n');
try {
fs.writeFileSync(destFilepath, moduleOutputJs);
} catch (err) {
grunt.fail.warn(err);
}
grunt.log.writeln('File ' + destFilepath.cyan + ' created.');
}
var SYSTEMJS_BANNER = '// This file is autogenerated via the `systemjs` Grunt task. You can import this file in a SystemJS environment.\n';
var systemJs = function (grunt, srcFiles, destFilepath) {
function moduleRequireStatement(srcFilepath) {
return 'System.import(\'' + srcFilepath.replace(/\\/g, '/') + '\')';
}
var moduleOutputJs = SYSTEMJS_BANNER + srcFiles.map(moduleRequireStatement).join('\n');
try {
fs.writeFileSync(destFilepath, moduleOutputJs);
} catch (err) {
grunt.fail.warn(err);
}
grunt.log.writeln('File ' + destFilepath.cyan + ' created.');
}
return {
'commonJs': commonJs,
'systemJs': systemJs
}
}())

View File

@ -34,11 +34,10 @@
"devDependencies": { "devDependencies": {
"autoprefixer": "^6.0.3", "autoprefixer": "^6.0.3",
"babel-eslint": "^4.1.6", "babel-eslint": "^4.1.6",
"babel-preset-es2015": "^6.0",
"babel-plugin-transform-es2015-modules-umd": "^6.0", "babel-plugin-transform-es2015-modules-umd": "^6.0",
"babel-plugin-transform-es2015-modules-systemjs": "^6.0",
"babel-polyfill": "^6.0", "babel-polyfill": "^6.0",
"babel-preset-es2015-rollup": "*",
"btoa": "~1.1.2", "btoa": "~1.1.2",
"glob": "~6.0.1", "glob": "~6.0.1",
"grunt": "~0.4.5", "grunt": "~0.4.5",
@ -60,7 +59,6 @@
"grunt-html": "~5.0.0", "grunt-html": "~5.0.0",
"grunt-jekyll": "~0.4.2", "grunt-jekyll": "~0.4.2",
"grunt-jscs": "~2.4.0", "grunt-jscs": "~2.4.0",
"grunt-line-remover": "0.0.2",
"grunt-postcss": "^0.7.0", "grunt-postcss": "^0.7.0",
"grunt-sass": "^1.0.0", "grunt-sass": "^1.0.0",
"grunt-saucelabs": "~8.6.1", "grunt-saucelabs": "~8.6.1",