From 03342f1a7e32158e44ed689c6cb2d4d515204813 Mon Sep 17 00:00:00 2001 From: James Friend Date: Sun, 15 Jun 2014 12:50:32 +0800 Subject: [PATCH 1/8] allow require.resolve('bootstrap') to work under node --- index.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000000..5092b4dd8c --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = __dirname; From 33a6932d6fc2df2b5b1d1b34f1ca5343dd8f8868 Mon Sep 17 00:00:00 2001 From: James Friend Date: Sun, 15 Jun 2014 12:53:26 +0800 Subject: [PATCH 2/8] added 'main' field to package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 8105591d7e..c9376e5f36 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "style": "dist/css/bootstrap.css", "less": "less/bootstrap.less", + "main": "./index.js", "repository": { "type": "git", "url": "https://github.com/twbs/bootstrap.git" From 689faaf16f255ae5bc37c6e348fd593ae270a103 Mon Sep 17 00:00:00 2001 From: James Friend Date: Mon, 4 Aug 2014 10:07:21 +0800 Subject: [PATCH 3/8] added a separate npm-specific main file --- dist/js/npm.js | 12 ++++++++++++ index.js | 1 - package.json | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 dist/js/npm.js delete mode 100644 index.js diff --git a/dist/js/npm.js b/dist/js/npm.js new file mode 100644 index 0000000000..17b0feda33 --- /dev/null +++ b/dist/js/npm.js @@ -0,0 +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') diff --git a/index.js b/index.js deleted file mode 100644 index 5092b4dd8c..0000000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = __dirname; diff --git a/package.json b/package.json index c9376e5f36..6f15315b48 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "style": "dist/css/bootstrap.css", "less": "less/bootstrap.less", - "main": "./index.js", + "main": "./dist/js/npm", "repository": { "type": "git", "url": "https://github.com/twbs/bootstrap.git" From e7991a9a1e2f474c8f1d8a2e0ed113816f1c5e82 Mon Sep 17 00:00:00 2001 From: James Friend Date: Thu, 28 Aug 2014 09:24:23 +0800 Subject: [PATCH 4/8] 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.'); +}; From 015ee2ed74bcdc48eea9bb7f6bb453f1c2f182aa Mon Sep 17 00:00:00 2001 From: James Friend Date: Thu, 28 Aug 2014 09:39:41 +0800 Subject: [PATCH 5/8] pulled output path out to gruntfile --- Gruntfile.js | 7 ++++--- grunt/bs-commonjs-generator.js | 17 ++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 60262ab87c..1262a668a2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -438,7 +438,7 @@ module.exports = function (grunt) { grunt.registerTask('test', testSubtasks); // 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']); @@ -464,8 +464,9 @@ module.exports = function (grunt) { }); grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () { - var files = grunt.config.get('concat.bootstrap.src'); - generateCommonJSModule(grunt, files); + var srcFiles = grunt.config.get('concat.bootstrap.src'); + var destFilepath = 'dist/js/npm.js'; + generateCommonJSModule(grunt, srcFiles, destFilepath); }); // Docs task. diff --git a/grunt/bs-commonjs-generator.js b/grunt/bs-commonjs-generator.js index 0a76333d92..8ab309b332 100644 --- a/grunt/bs-commonjs-generator.js +++ b/grunt/bs-commonjs-generator.js @@ -2,17 +2,16 @@ var fs = require('fs'); var path = require('path'); -var destDir = 'dist/js'; -var destFilename = 'npm.js'; -var destFilepath = path.join(destDir, destFilename); +module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) { + var destDir = path.dirname(destFilepath); -function srcPathToDestRequire(srcFilepath) { - var requirePath = path.relative(destDir, srcFilepath); - return "require('"+requirePath+"')"; -} + 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'); + var moduleOutputJs = srcFiles.map(srcPathToDestRequire).join('\n'); + try { fs.writeFileSync(destFilepath, moduleOutputJs); } From 2bc417732c4142091e92fce07f93c4fff11173b1 Mon Sep 17 00:00:00 2001 From: James Friend Date: Thu, 28 Aug 2014 09:54:51 +0800 Subject: [PATCH 6/8] code style fixes --- grunt/bs-commonjs-generator.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/grunt/bs-commonjs-generator.js b/grunt/bs-commonjs-generator.js index 8ab309b332..e5173944ca 100644 --- a/grunt/bs-commonjs-generator.js +++ b/grunt/bs-commonjs-generator.js @@ -7,11 +7,10 @@ module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) function srcPathToDestRequire(srcFilepath) { var requirePath = path.relative(destDir, srcFilepath); - return "require('"+requirePath+"')"; + return 'require(\'' + requirePath + '\')'; } var moduleOutputJs = srcFiles.map(srcPathToDestRequire).join('\n'); - try { fs.writeFileSync(destFilepath, moduleOutputJs); } From 4ba4b7c3cf20b8e9df3ec41af63b68300b402814 Mon Sep 17 00:00:00 2001 From: James Friend Date: Tue, 9 Sep 2014 08:47:23 +0800 Subject: [PATCH 7/8] added banner about generated file --- dist/js/npm.js | 1 + grunt/bs-commonjs-generator.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dist/js/npm.js b/dist/js/npm.js index c7c20adfe1..d5e4f476d5 100644 --- a/dist/js/npm.js +++ b/dist/js/npm.js @@ -1,3 +1,4 @@ +// This file is generated. You can require() it in a CommonJS environment. require('../../js/transition.js') require('../../js/alert.js') require('../../js/button.js') diff --git a/grunt/bs-commonjs-generator.js b/grunt/bs-commonjs-generator.js index e5173944ca..1b0da182f7 100644 --- a/grunt/bs-commonjs-generator.js +++ b/grunt/bs-commonjs-generator.js @@ -10,7 +10,8 @@ module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) return 'require(\'' + requirePath + '\')'; } - var moduleOutputJs = srcFiles.map(srcPathToDestRequire).join('\n'); + var moduleOutputJs = '// This file is generated. You can require() it in a CommonJS environment.\n' + + srcFiles.map(srcPathToDestRequire).join('\n'); try { fs.writeFileSync(destFilepath, moduleOutputJs); } From 3440074bb7db51402e28d17f0da84d3a730bdf49 Mon Sep 17 00:00:00 2001 From: James Friend Date: Tue, 9 Sep 2014 08:50:35 +0800 Subject: [PATCH 8/8] code style improvement --- grunt/bs-commonjs-generator.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grunt/bs-commonjs-generator.js b/grunt/bs-commonjs-generator.js index 1b0da182f7..18542cc74f 100644 --- a/grunt/bs-commonjs-generator.js +++ b/grunt/bs-commonjs-generator.js @@ -2,6 +2,8 @@ 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); @@ -10,8 +12,7 @@ module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) return 'require(\'' + requirePath + '\')'; } - var moduleOutputJs = '// This file is generated. You can require() it in a CommonJS environment.\n' + - srcFiles.map(srcPathToDestRequire).join('\n'); + var moduleOutputJs = COMMONJS_BANNER + srcFiles.map(srcPathToDestRequire).join('\n'); try { fs.writeFileSync(destFilepath, moduleOutputJs); }