diff --git a/Gruntfile.js b/Gruntfile.js index 0e5627d9ec..f1430e0265 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -26,6 +26,7 @@ module.exports = function (grunt) { return { sections: parser.parseFile() }; }; var generateRawFiles = require('./grunt/bs-raw-files-generator.js'); + var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js'); // Project configuration. grunt.initConfig({ @@ -444,7 +445,7 @@ module.exports = function (grunt) { grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'qunit']); // JS distribution task. - grunt.registerTask('dist-js', ['concat', 'uglify:core']); + grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']); // CSS distribution task. grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']); @@ -469,6 +470,12 @@ module.exports = function (grunt) { generateRawFiles(grunt, banner); }); + grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () { + var srcFiles = grunt.config.get('concat.bootstrap.src'); + var destFilepath = 'dist/js/npm.js'; + generateCommonJSModule(grunt, srcFiles, destFilepath); + }); + // Docs task. grunt.registerTask('docs-css', ['autoprefixer:docs', 'autoprefixer:examples', 'csscomb:docs', 'csscomb:examples', 'cssmin:docs']); grunt.registerTask('lint-docs-css', ['csslint:docs', 'csslint:examples']); diff --git a/dist/js/npm.js b/dist/js/npm.js new file mode 100644 index 0000000000..d5e4f476d5 --- /dev/null +++ b/dist/js/npm.js @@ -0,0 +1,13 @@ +// This file is generated. You can require() it in a CommonJS environment. +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..18542cc74f --- /dev/null +++ b/grunt/bs-commonjs-generator.js @@ -0,0 +1,23 @@ +'use strict'; +var fs = require('fs'); +var path = require('path'); + +var COMMONJS_BANNER = '// This file is generated. You can require() it in a CommonJS environment.\n'; + +module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) { + var destDir = path.dirname(destFilepath); + + function srcPathToDestRequire(srcFilepath) { + var requirePath = path.relative(destDir, srcFilepath); + return 'require(\'' + requirePath + '\')'; + } + + 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.'); +}; diff --git a/package.json b/package.json index da10c6e309..624625ce26 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "style": "dist/css/bootstrap.css", "less": "less/bootstrap.less", + "main": "./dist/js/npm", "repository": { "type": "git", "url": "https://github.com/twbs/bootstrap.git"