Skip to content
This repository was archived by the owner on Aug 15, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bin/spm-build
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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({
Expand All @@ -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)
))
)
Expand Down Expand Up @@ -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();
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions tests/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
133 changes: 31 additions & 102 deletions tests/build/expect/build-js-standalone/a/1.0.0/index-debug.js
Original file line number Diff line number Diff line change
@@ -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 = '<div></div>';
});
define("b/1.1.0/src/extra-debug", [], function(require, exports, module) {
return exports;
}({});
b_110_src_b_debugtpl = function(exports) {
exports = '<div></div>';
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");
})();
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;
}();
}());
2 changes: 1 addition & 1 deletion tests/build/expect/build-js-standalone/a/1.0.0/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions tests/build/expect/build-js-umd/a/1.0.0/index-debug.js
Original file line number Diff line number Diff line change
@@ -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 = '<div></div>';
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;
}
}());
1 change: 1 addition & 0 deletions tests/build/expect/build-js-umd/a/1.0.0/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.