diff --git a/bin/spm-build b/bin/spm-build index 1e1ff0aa..1725dcbf 100755 --- a/bin/spm-build +++ b/bin/spm-build @@ -53,8 +53,9 @@ var args = { zip: commander.zip, force: commander.force, withDeps: commander.withDeps, - install: true, - registry: commander.registry + install: commander.install, + registry: commander.registry, + umd: commander.umd }; if (args.ignore) { diff --git a/lib/build.js b/lib/build.js index 669abe82..fb21ed0d 100644 --- a/lib/build.js +++ b/lib/build.js @@ -8,6 +8,7 @@ var umi = require('umi'); var gulp = require('gulp'); var spmrc = require('spmrc'); var multipipe = require('multipipe'); +var standalonify = require('spm-standalonify'); var $ = require('gulp-load-plugins')({ config: require('../package.json') }); @@ -16,7 +17,7 @@ module.exports = function(options, cb) { var args = umi.buildArgs(options); logArgs(args); - if (args.install === true) { + if (args.install) { var opt = { cwd: args.cwd, registry: args.registry @@ -127,10 +128,17 @@ function defineBuildTask(files, args) { function buildFiles(files, args) { var beautifyOpt = {indent_size: 2, preserve_newlines: false}; - var isStandalone = args.include === 'standalone'; - if (isStandalone) { + var isStandalone; + if (args.include === 'standalone') { + isStandalone = true; args.include = 'all'; } + if (args.include === 'umd') { + isStandalone = true; + args.include = 'all'; + // use package name as global name for umd + args.umd = camelCase(args.pkg.name); + } var opt = umi.util.extendOption(args); var optDebug = umi.util.extendOption(args); @@ -147,7 +155,7 @@ function buildFiles(files, args) { pipe( $.transport(opt), $.if(isJS, pipe( - $.if(isStandalone, umi.standalonify(args)), + $.if(isStandalone, standalonify(args)), $.uglify() ), $.cssmin()), $.size2({ @@ -164,7 +172,7 @@ function buildFiles(files, args) { pipe( $.transport(optDebug), $.if(isJS, pipe( - $.if(isStandalone, umi.standalonify(args)), + $.if(isStandalone, standalonify(args)), $.jsbeautify(beautifyOpt) )) ) @@ -258,3 +266,9 @@ function getExtDeps(files, pkg) { }); return Object.keys(extDeps); } + +function camelCase(str) { + return str.replace(/[_.-](\w|$)/g, function (_,x) { + return x.toUpperCase(); + }); +} diff --git a/package.json b/package.json index 349b53ed..ab5cbd6a 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "semver": "~2.2.1", "serve-spm": "~0.5.0", "spm-client": "~0.2.0", + "spm-standalonify": "^0.1.0", "spmrc": "~1.2.0", "tar": "~0.1.19", "umi": "~0.6.0", diff --git a/tests/build.js b/tests/build.js index 011021b5..7a7009b0 100644 --- a/tests/build.js +++ b/tests/build.js @@ -86,6 +86,19 @@ describe('build', function() { }); }); + it('js package include umd', function(done) { + var opt = { + cwd: join(base, 'build-js'), + dest: dest, + include: 'umd' + }; + build(opt, function(err) { + should.not.exist(err); + assets('build-js-umd', dest); + done(); + }); + }); + it('js require css', function(done) { var opt = { cwd: join(base, 'build-css'), diff --git a/tests/build/expect/build-js-standalone/a/1.0.0/index-debug.js b/tests/build/expect/build-js-standalone/a/1.0.0/index-debug.js index c6de349f..d25f9caf 100644 --- a/tests/build/expect/build-js-standalone/a/1.0.0/index-debug.js +++ b/tests/build/expect/build-js-standalone/a/1.0.0/index-debug.js @@ -1,110 +1,39 @@ +; (function() { - /** - * Sea.js mini 2.3.0 | seajs.org/LICENSE.md - */ - var define; - var require; - (function(global, undefined) { - /** - * util-lang.js - The minimal language enhancement - */ - function isType(type) { - return function(obj) { - return {}.toString.call(obj) == "[object " + type + "]" - } - } - var isFunction = isType("Function") - /** - * module.js - The core of module loader - */ - var cachedMods = {} - - function Module() {} - // Execute a module - Module.prototype.exec = function() { - var mod = this - // When module is executed, DO NOT execute it again. When module - // is being executed, just return `module.exports` too, for avoiding - // circularly calling - if (this.execed) { - return mod.exports - } - this.execed = true; - - function require(id) { - return Module.get(id).exec() - } - // Exec factory - var factory = mod.factory - var exports = isFunction(factory) ? factory(require, mod.exports = {}, mod) : factory - if (exports === undefined) { - exports = mod.exports - } - // Reduce memory leak - delete mod.factory - mod.exports = exports - return exports - } - // Define a module - define = function(id, deps, factory) { - var meta = { - id: id, - deps: deps, - factory: factory - } - Module.save(meta) - } - // Save meta data to cachedMods - Module.save = function(meta) { - var mod = Module.get(meta.id) - mod.id = meta.id - mod.dependencies = meta.deps - mod.factory = meta.factory - } - // Get an existed module or create a new one - Module.get = function(id) { - return cachedMods[id] || (cachedMods[id] = new Module()) - } - // Public API - require = function(id) { - var mod = Module.get(id) - if (!mod.execed) { - mod.exec() - } - return mod.exports - } - })(this); - define("a/1.0.0/index-debug", [], function(require, exports, module) { - require("a/1.0.0/relative-debug"); - require("d/0.1.1/index-debug"); - }); - define("a/1.0.0/relative-debug", [], function(require, exports, module) { - console.log('relative'); - require("b/1.1.0/src/b-debug"); - require("b/1.1.0/src/extra-debug"); - }); - define("b/1.1.0/src/b-debug", [], function(require, exports, module) { - require("c/1.1.1/index-debug"); - require("b/1.1.0/src/b-debug.tpl"); - }); - define("c/1.1.1/index-debug", [], function(require, exports, module) { - require("d/0.1.0/index-debug"); - }); - define("d/0.1.0/index-debug", [], function(require, exports, module) { + var d_010_index_debug, b_110_src_b_debugtpl, b_110_src_extra_debug, d_011_index_debug, c_111_index_debug, b_110_src_b_debug, a_100_relative_debug, a_100_index_debug; + d_010_index_debug = function(exports) { exports.d = function() { console.log('0.1.0'); }; - }); - define("b/1.1.0/src/b-debug.tpl", [], function(require, exports, module) { - module.exports = '
'; - }); - define("b/1.1.0/src/extra-debug", [], function(require, exports, module) { + return exports; + }({}); + b_110_src_b_debugtpl = function(exports) { + exports = ''; + return exports; + }(); + b_110_src_extra_debug = function() { console.log('b-extra'); - }); - define("d/0.1.1/index-debug", [], function(require, exports, module) { + }(); + d_011_index_debug = function(exports) { exports.d = function() { console.log('0.1.1'); }; - }); - require("a/1.0.0/index-debug"); -})(); \ No newline at end of file + return exports; + }({}); + c_111_index_debug = function() { + d_010_index_debug; + }(); + b_110_src_b_debug = function() { + c_111_index_debug; + b_110_src_b_debugtpl; + }(); + a_100_relative_debug = function() { + console.log('relative'); + b_110_src_b_debug; + b_110_src_extra_debug; + }(); + a_100_index_debug = function() { + a_100_relative_debug; + d_011_index_debug; + }(); +}()); \ No newline at end of file diff --git a/tests/build/expect/build-js-standalone/a/1.0.0/index.js b/tests/build/expect/build-js-standalone/a/1.0.0/index.js index b5adc838..896b40f3 100644 --- a/tests/build/expect/build-js-standalone/a/1.0.0/index.js +++ b/tests/build/expect/build-js-standalone/a/1.0.0/index.js @@ -1 +1 @@ -!function(){var e,n;!function(t,o){function c(e){return function(n){return{}.toString.call(n)=="[object "+e+"]"}}function i(){}var r=c("Function"),u={};i.prototype.exec=function(){function e(e){return i.get(e).exec()}var n=this;if(this.execed)return n.exports;this.execed=!0;var t=n.factory,c=r(t)?t(e,n.exports={},n):t;return c===o&&(c=n.exports),delete n.factory,n.exports=c,c},e=function(e,n,t){var o={id:e,deps:n,factory:t};i.save(o)},i.save=function(e){var n=i.get(e.id);n.id=e.id,n.dependencies=e.deps,n.factory=e.factory},i.get=function(e){return u[e]||(u[e]=new i)},n=function(e){var n=i.get(e);return n.execed||n.exec(),n.exports}}(this),e("a/1.0.0/index",[],function(e){e("a/1.0.0/relative"),e("d/0.1.1/index")}),e("a/1.0.0/relative",[],function(e){console.log("relative"),e("b/1.1.0/src/b"),e("b/1.1.0/src/extra")}),e("b/1.1.0/src/b",[],function(e){e("c/1.1.1/index"),e("b/1.1.0/src/b.tpl")}),e("c/1.1.1/index",[],function(e){e("d/0.1.0/index")}),e("d/0.1.0/index",[],function(e,n){n.d=function(){console.log("0.1.0")}}),e("b/1.1.0/src/b.tpl",[],function(e,n,t){t.exports=""}),e("b/1.1.0/src/extra",[],function(){console.log("b-extra")}),e("d/0.1.1/index",[],function(e,n){n.d=function(){console.log("0.1.1")}}),n("a/1.0.0/index")}(); \ No newline at end of file +!function(){var n,o,i,t,c,u,e,l;n=function(n){return n.d=function(){console.log("0.1.0")},n}({}),o=function(n){return n=""}(),i=function(){console.log("b-extra")}(),t=function(n){return n.d=function(){console.log("0.1.1")},n}({}),c=void 0,u=void 0,e=function(){console.log("relative")}(),l=void 0}(); \ No newline at end of file diff --git a/tests/build/expect/build-js-umd/a/1.0.0/index-debug.js b/tests/build/expect/build-js-umd/a/1.0.0/index-debug.js new file mode 100644 index 00000000..fef0ace4 --- /dev/null +++ b/tests/build/expect/build-js-umd/a/1.0.0/index-debug.js @@ -0,0 +1,48 @@ +; +(function() { + var d_010_index_debug, b_110_src_b_debugtpl, b_110_src_extra_debug, d_011_index_debug, c_111_index_debug, b_110_src_b_debug, a_100_relative_debug, a_100_index_debug; + d_010_index_debug = function(exports) { + exports.d = function() { + console.log('0.1.0'); + }; + return exports; + }({}); + b_110_src_b_debugtpl = function(exports) { + exports = ''; + return exports; + }(); + b_110_src_extra_debug = function() { + console.log('b-extra'); + }(); + d_011_index_debug = function(exports) { + exports.d = function() { + console.log('0.1.1'); + }; + return exports; + }({}); + c_111_index_debug = function() { + d_010_index_debug; + }(); + b_110_src_b_debug = function() { + c_111_index_debug; + b_110_src_b_debugtpl; + }(); + a_100_relative_debug = function() { + console.log('relative'); + b_110_src_b_debug; + b_110_src_extra_debug; + }(); + a_100_index_debug = function() { + a_100_relative_debug; + d_011_index_debug; + }(); + if (typeof exports == "object") { + module.exports = a_100_index_debug; + } else if (typeof define == "function" && define.amd) { + define([], function() { + return a_100_index_debug + }); + } else { + this["a"] = a_100_index_debug; + } +}()); \ No newline at end of file diff --git a/tests/build/expect/build-js-umd/a/1.0.0/index.js b/tests/build/expect/build-js-umd/a/1.0.0/index.js new file mode 100644 index 00000000..9f77d552 --- /dev/null +++ b/tests/build/expect/build-js-umd/a/1.0.0/index.js @@ -0,0 +1 @@ +!function(){var n,o,e,t,i,c,f,u;n=function(n){return n.d=function(){console.log("0.1.0")},n}({}),o=function(n){return n=""}(),e=function(){console.log("b-extra")}(),t=function(n){return n.d=function(){console.log("0.1.1")},n}({}),i=void 0,c=void 0,f=function(){console.log("relative")}(),u=void 0,"object"==typeof exports?module.exports=u:"function"==typeof define&&define.amd?define([],function(){return u}):this.a=u}(); \ No newline at end of file