From 9657bda937a8cc389082d8aa5f571d271f674da2 Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 8 Mar 2018 14:00:58 +0400 Subject: [PATCH] add commonjs support to architect-build --- .../architect-build/module-deps.js | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/plugins/node_modules/architect-build/module-deps.js b/plugins/node_modules/architect-build/module-deps.js index 4779c6b0..a11f5825 100644 --- a/plugins/node_modules/architect-build/module-deps.js +++ b/plugins/node_modules/architect-build/module-deps.js @@ -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;