add node support to architect-build

pull/468/merge
nightwing 2017-07-20 18:50:11 +04:00
rodzic ce4864646d
commit d365f8e1f9
3 zmienionych plików z 170 dodań i 21 usunięć

36
node_modules/architect-build/build.js wygenerowano vendored
Wyświetl plik

@ -40,16 +40,11 @@ function build(config, opts, callback){
if (!mains.length)
return callback(new Error("Config contains no packages"));
// Add Architect
if (!opts.noArchitect) {
mains.push("lib/architect/architect");
}
// Add RequireJS
if (opts.withRequire) {
mains.unshift({
id: null,
path: path.resolve(__dirname + "/build_support/mini_require.js"),
path: path.resolve(__dirname + "/build_support/" + (opts.node ? "node" : "mini") + "_require.js"),
order: -1000,
noRequire: true,
mini_require: true
@ -105,7 +100,7 @@ function build(config, opts, callback){
stripLess(sources);
var afterRead = typeof opts.afterRead == "function"
var afterRead = typeof opts.afterRead == "function"
? [opts.afterRead]
: opts.afterRead || [];
afterRead.push(rewriteDefines);
@ -115,10 +110,11 @@ function build(config, opts, callback){
});
// Include the architect config at the end in the same way as the tests
if (!opts.noArchitect)
if (opts.autoload)
includeAutoload(opts, sources);
else if (!opts.noArchitect)
includeArchitect(opts, sources);
opts.quiet || console.log("Processing " + sources.length + " files.");
opts.quiet || console.log("Compressing: ", opts.compress);
@ -369,22 +365,26 @@ function includeArchitect(opts, sources) {
+ ' architect.createApp(config);\n'
+ ' });\n'
+ '});\n';
var output = (opts.outputFolder || ".") + "/" + (opts.outputFile || "architect-config.js");
fs.writeFile(output, opts.configFile, function(err){
if (!err)
console.log("Written config file in '" + output + "'.");
});
}
sources.push({
id : "bootstrap",
file : "bootstrap",
source : source
id: "bootstrap",
file: "bootstrap",
source: source
});
}
function includeAutoload(opts, sources) {
var source = 'require("' + opts.autoload + '");\n';
sources.push({
id: "bootstrap",
file: "bootstrap",
source: source
});
}
function writeToFile(result, opts, callback) {
// Write output code

Wyświetl plik

@ -0,0 +1,110 @@
// Simple require shim for packaged node apps
(function () {
if (global.define) return;
var fs = require("fs");
var path = require("path");
var Module = require("module");
var fp = Module._findPath;
Module._findPath = function(request, paths) {
if (Module._cache[request])
return request;
var id = path.resolve(paths[0], request);
if (Module._cache[id])
return id;
return fp(request, paths);
}
var moduleStack = [];
var defaultCompile = module.constructor.prototype._compile;
module.constructor.prototype._compile = function(content, filename){
moduleStack.push(this);
try {
return defaultCompile.call(this, content, filename);
}
finally {
moduleStack.pop();
}
};
global.define = function(id, injects, factory) {
var DEFAULT_INJECTS = ["require", "exports", "module"];
// infer the module
var currentModule = moduleStack[moduleStack.length-1];
var mod = currentModule || module.parent || require.main;
// assign arguments
if (arguments.length === 1) {
factory = id;
injects = DEFAULT_INJECTS;
id = null;
}
else if (arguments.length === 2) {
factory = injects;
injects = id;
id = null;
}
if (injects.length == 0) {
injects = DEFAULT_INJECTS;
}
if (typeof id === "string" && id !== mod.id) {
var fullId = path.resolve(__filename, id);
mod = new Module(fullId, mod);
mod.filename = fullId;
Module._cache[id] = Module._cache[fullId] = mod;
}
var req = function(module, relativeId, callback) {
if (Array.isArray(relativeId)) {
// async require
return callback.apply(this, relativeId.map(req))
}
var chunks = relativeId.split("!");
var prefix;
if (chunks.length >= 2) {
prefix = chunks[0];
relativeId = chunks.slice(1).join("!");
}
var fileName = Module._resolveFilename(relativeId, module);
if (Array.isArray(fileName))
fileName = fileName[0];
if (prefix && prefix.indexOf("text") !== -1) {
return fs.readFileSync(fileName, "utf8");
} else
return require(fileName);
}.bind(this, mod);
id = mod.id;
if (typeof factory !== "function") {
// we can just provide a plain object
return mod.exports = factory;
}
var returned = factory.apply(mod.exports, injects.map(function (injection) {
switch (injection) {
// check for CommonJS injection variables
case "require": return req;
case "exports": return mod.exports;
case "module": return mod;
default:
// a module dependency
return req(injection);
}
}));
if (returned) {
// since AMD encapsulates a function/callback, it can allow the factory to return the exports.
mod.exports = returned;
}
};
}());

Wyświetl plik

@ -1,5 +1,6 @@
var fs = require('fs');
var pathLib = require('path');
var fs = require("fs");
var pathLib = require("path");
var Module = require("module");
var through = require('through');
@ -29,6 +30,9 @@ module.exports = function(mains, opts) {
if (!opts.transforms)
opts.transforms = [];
if (opts.node)
opts.transforms.push(wrapCJS);
opts.transforms.push(removeLicenceComments, wrapUMD);
if (opts.pathConfig) {
@ -59,10 +63,24 @@ module.exports = function(mains, opts) {
id = resolveModuleId(id, paths);
if (pathMap)
id = resolveModulePath(id, pathMap);
if (!mod.loaderModule && !/\.js$/.test(id))
if (opts.node)
id = idToPathNode(mod);
else if (!mod.loaderModule && !/\.js$/.test(id))
id += ".js";
cb(null, id);
}
function idToPathNode(mod) {
var packagePath = mod.id;
var filepath = mod.parent && mod.parent.file || root;
return Module._resolveFilename(packagePath, {
paths: Module._nodeModulePaths(filepath),
filename: filepath,
id: filepath,
});
}
function readModule(mod, cb) {
if (typeof mod == "string")
@ -400,6 +418,27 @@ function wrapUMD(module) {
+ '});';
}
function wrapCJS(module) {
if (module.loaderModule || module.noRequire)
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;
}
console.log("wrapping module " + module.id);
module.source = 'define(function(require, exports, module) {\n'
+ module.source + '\n'
+ '});';
}
function debugSrc(module) {
if (module.loaderModule)
return;