From e7991a9a1e2f474c8f1d8a2e0ed113816f1c5e82 Mon Sep 17 00:00:00 2001 From: James Friend Date: Thu, 28 Aug 2014 09:24:23 +0800 Subject: [PATCH] generate commonjs/npm entrypoint module via grunt task --- Gruntfile.js | 6 ++++++ dist/js/npm.js | 24 ++++++++++++------------ grunt/bs-commonjs-generator.js | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 grunt/bs-commonjs-generator.js diff --git a/Gruntfile.js b/Gruntfile.js index ca9d43eec4..3fc645fafe 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -20,6 +20,7 @@ module.exports = function (grunt) { var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js'); var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js'); var generateRawFiles = require('./grunt/bs-raw-files-generator.js'); + var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js'); var updateShrinkwrap = require('./grunt/shrinkwrap.js'); // Project configuration. @@ -459,6 +460,11 @@ module.exports = function (grunt) { generateRawFiles(grunt, banner); }); + grunt.registerTask('build-commonjs', 'Build CommonJS entrypoint module for JS.', function () { + var files = grunt.config.get('concat.bootstrap.src'); + generateCommonJSModule(grunt, files); + }); + // Task for updating the npm packages used by the Travis build. grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', 'exec:npmShrinkWrap', '_update-shrinkwrap']); grunt.registerTask('_update-shrinkwrap', function () { updateShrinkwrap.call(this, grunt); }); diff --git a/dist/js/npm.js b/dist/js/npm.js index 17b0feda33..c7c20adfe1 100644 --- a/dist/js/npm.js +++ b/dist/js/npm.js @@ -1,12 +1,12 @@ -require('../../js/transition') -require('../../js/alert') -require('../../js/button') -require('../../js/carousel') -require('../../js/collapse') -require('../../js/dropdown') -require('../../js/modal') -require('../../js/tooltip') -require('../../js/popover') -require('../../js/scrollspy') -require('../../js/tab') -require('../../js/affix') +require('../../js/transition.js') +require('../../js/alert.js') +require('../../js/button.js') +require('../../js/carousel.js') +require('../../js/collapse.js') +require('../../js/dropdown.js') +require('../../js/modal.js') +require('../../js/tooltip.js') +require('../../js/popover.js') +require('../../js/scrollspy.js') +require('../../js/tab.js') +require('../../js/affix.js') \ No newline at end of file diff --git a/grunt/bs-commonjs-generator.js b/grunt/bs-commonjs-generator.js new file mode 100644 index 0000000000..0a76333d92 --- /dev/null +++ b/grunt/bs-commonjs-generator.js @@ -0,0 +1,23 @@ +'use strict'; +var fs = require('fs'); +var path = require('path'); + +var destDir = 'dist/js'; +var destFilename = 'npm.js'; +var destFilepath = path.join(destDir, destFilename); + +function srcPathToDestRequire(srcFilepath) { + var requirePath = path.relative(destDir, srcFilepath); + return "require('"+requirePath+"')"; +} + +module.exports = function generateCommonJSModule(grunt, files) { + var moduleOutputJs = files.map(srcPathToDestRequire).join('\n'); + try { + fs.writeFileSync(destFilepath, moduleOutputJs); + } + catch (err) { + grunt.fail.warn(err); + } + grunt.log.writeln('File ' + destFilepath.cyan + ' created.'); +};