add commonjs support to architect-build

pull/483/merge
nightwing 2018-03-08 14:00:58 +04:00
rodzic 3d654fc187
commit 9657bda937
1 zmienionych plików z 24 dodań i 15 usunięć

Wyświetl plik

@ -30,10 +30,7 @@ module.exports = function(mains, opts) {
if (!opts.transforms)
opts.transforms = [];
if (opts.node)
opts.transforms.push(wrapCJS);
opts.transforms.push(removeLicenceComments, wrapUMD);
opts.transforms.push(removeLicenceComments, wrapCJS, wrapUMD);
if (opts.pathConfig) {
opts.paths = opts.paths || opts.pathConfig.paths;
@ -254,7 +251,9 @@ module.exports = function(mains, opts) {
return output;
};
function removeComments(src) {
return src.replace(/^\s*\/\/.+|^\s*\/\*[\s\S]*?\*\//gm, "")
}
function normalizeModule(parentId, moduleName) {
// normalize plugin requires
if (moduleName.indexOf("!") !== -1) {
@ -274,7 +273,7 @@ function normalizeModule(parentId, moduleName) {
return moduleName;
}
function getSubmodules(src, name) {
var m = src.replace(/^\s*\/\/.+|^\s*\/\*[\s\S]*?\*\//gm, "")
var m = removeComments(src)
.match(/require\(\[([^\]]+)\]/gm);
if (!m)
return [];
@ -290,7 +289,7 @@ function getSubmodules(src, name) {
return deps;
}
function getReqDeps(src, name) {
var m = src.replace(/^\s*\/\/.+|^\s*\/\*[\s\S]*?\*\//gm, "")
var m = removeComments(src)
.match(/require\s*\(\s*(["'][^"'\n\r]+["'])\s*\)/gm);
if (!m)
return [];
@ -427,14 +426,8 @@ function wrapCJS(module) {
return;
module.source = module.source.replace(/^#.*\n/, "");
var firstDefineCall = module.source.match(/define\(\s*[^)]*/);
if (firstDefineCall) {
// check if it is a normal define or some crazy umd trick
if (/define\(\s*function\s*\(/.test(firstDefineCall[0]))
return;
if (/define\(\s*\[[^\]]*\],\s*function\(/.test(firstDefineCall[0]))
return;
}
if (isCJS(module.source))
return;
console.log("wrapping module " + module.id);
@ -443,6 +436,21 @@ function wrapCJS(module) {
+ '});';
}
function isCJS(source) {
source = removeComments(source);
var firstDefineCall = source.match(/define\(\s*[^)]*/);
if (firstDefineCall) {
// check if it is a normal define or some crazy umd trick
if (/define\(\s*function\s*\(/.test(firstDefineCall[0]))
return;
if (/define\(\s*\[[^\]]*\],\s*\(?function\(/.test(firstDefineCall[0]))
return;
if (/typeof define/.test(source))
return;
}
return true;
}
function debugSrc(module) {
if (module.loaderModule)
return;
@ -457,6 +465,7 @@ function quote(str) {
}
module.exports.isCJS = isCJS;
module.exports.getDeps = getDeps;
module.exports.getSubmodules = getSubmodules;
module.exports.resolveModulePath = resolveModulePath;