add support for commonjs and es6

pull/483/merge
nightwing 2018-03-08 14:03:41 +04:00
rodzic 9657bda937
commit 1d88695453
6 zmienionych plików z 109 dodań i 14 usunięć

Wyświetl plik

@ -1,5 +1,4 @@
/*global apf*/
define(function(require, exports, module) {
"use strict";
main.consumes = [
"Editor", "editors", "commands", "menus", "Menu", "MenuItem", "Divider",
@ -2930,4 +2929,3 @@ define(function(require, exports, module) {
ace: handle
});
}
});

Wyświetl plik

@ -22,6 +22,7 @@ function main(options, imports, register) {
var atomic = require("c9/atomic");
var error = require("http-error");
var frontdoor = require("frontdoor");
var transform = require("architect-build/transform");
var cacheFiles = options.cacheFiles;
@ -32,6 +33,7 @@ function main(options, imports, register) {
var resolveModulePath = require("architect-build/module-deps").resolveModulePath;
connectStatic.getRequireJsConfig().useCache = options.useBrowserCache;
connectStatic.getRequireJsConfig().transform = "amd";
section.post("/__check__", [function(req, res, next) {
req.params.hash = "any";
next();
@ -152,6 +154,10 @@ function main(options, imports, register) {
section.use(imports["connect.cors"].cors("*"));
section.use(connect.getModule().compress());
section.get("/~/:transform/:path*", [prepare, function(req, res, next) {
transform.sendFile(req, res, next);
}]);
section.get("/:hash/config/:name", [prepare, function(req, res, next) {
var name = req.params.name.replace(/\.js$/, "");
var file = path.join(build.cacheDir, req.params.hash, "config", name + ".js");
@ -198,7 +204,7 @@ function main(options, imports, register) {
.on('error', onSendError(next))
.pipe(res);
}]);
register();
function sendCached(filename, req, res, next, loader) {
@ -259,11 +265,7 @@ function main(options, imports, register) {
}
function prepare(req, res, next) {
var hash = req.params.hash;
if (!hash.match(/^[a-z0-9]+$/))
return next(new error.NotFound());
build.getPathConfig(hash, function(err, pathConfig) {
build.getPathConfig(req.params.hash, function(err, pathConfig) {
if (err) return next(err);
req.pathConfig = pathConfig;

Wyświetl plik

@ -169,7 +169,7 @@
}
});
require.config({ useCache: true });
require.config({ useCache: false, transform: "~es5/" });
mocha.bail(false);
mocha.ignoreLeaks(true);
mocha.fullTrace && mocha.fullTrace();

Wyświetl plik

@ -286,11 +286,14 @@ var config = require.config = function(cfg) {
config.paths[p] = cfg.paths[p];
});
if (cfg.useCache && global.caches && location.protocol === "https:") {
if (cfg.useCache && global.caches && (location.protocol === "https:" || location.hostname == "localhost")) {
config.useCache = true;
checkCache();
}
if (cfg.transform)
config.transform = cfg.transform;
if (cfg.baseUrlLoadBalancers)
config.baseUrlLoadBalancers = cfg.baseUrlLoadBalancers;
};
@ -374,11 +377,13 @@ require.toUrl = function(moduleName, ext, skipExt, skipBalancers) {
var url = moduleName + ext;
if (!absRe.test(url)) {
url = (config.baseUrl || require.MODULE_LOAD_URL + "/") + url;
if (ext == ".js" && require.config.transform)
url = ("~/" + require.config.transform + "/" + url).replace("//", "/");
url = (config.baseUrl || require.MODULE_LOAD_URL + "/") + url;
}
if (url[0] === "/" && config.baseUrlLoadBalancers && !skipBalancers && !config.useCache) {
var n = Math.abs(hashCode(url)) % config.baseUrlLoadBalancers.length;
url = config.baseUrlLoadBalancers[n] + url;
url = config.baseUrlLoadBalancers[n] + url;
}
return url;
};
@ -415,7 +420,7 @@ var loadScriptWithTag = function(path, id, callback) {
};
s.onerror = function(e) {
processLoadQueue({
message: "Error loading script " + id + ":" + path,
message: "Error loading script " + id + ":" + path,
id: id,
path: path
});
@ -437,6 +442,7 @@ function loadText(path, cb) {
}
/*** cache ***/
/*global Response, Request*/
var host = location.protocol + "//" + location.hostname + (location.port ? ":" + location.port : "");
var loadScript = function(path, id, callback) {
if (!config.useCache)

Wyświetl plik

@ -426,7 +426,7 @@ function wrapCJS(module) {
return;
module.source = module.source.replace(/^#.*\n/, "");
if (isCJS(module.source))
if (!isCJS(module.source))
return;
console.log("wrapping module " + module.id);

89
plugins/node_modules/architect-build/transform.js wygenerowano vendored 100644
Wyświetl plik

@ -0,0 +1,89 @@
var moduleDeps = require("./module-deps");
var fs = require("fs");
var send = require("send");
exports.transform = function(code, options) {
if (/^"disable compress"/.test(code))
return code;
if (options.mode == "babel") {
var babel = require("babel");
return babel.transform(code, {
"presets": [
["env", {
"targets": {
"browsers": ["last 2 versions", "ie >= 11"]
}
}]
]
}).code;
}
else if (options.mode == "ts") {
var ts = require("typescript");
return ts.transpileModule(code, {
compilerOptions: {
downlevelIteration: true,
suppressExcessPropertyErrors: true,
removeComments: true,
module: ts.ModuleKind.CommonJS
}
}).outputText;
}
else if (options.mode == "buble") {
return require("buble").transform(code).code;
}
else {
if (moduleDeps.isCJS(code)) {
code = "define(function(require, exports, module) {" + code + "\n})";
}
return code;
}
};
var cache = Object.create(null);
exports.sendFile = function(req, res, next) {
var path = req.params.path;
var filePath = moduleDeps.resolveModulePath(path, req.pathConfig.pathMap);
if (!/\.js$/.test(filePath) || /(browserified|\.min|test\d)\.js$/.test(filePath)) {
return send(req, filePath.substr(req.pathConfig.root.length))
.root(req.pathConfig.root)
.on('error', next)
.pipe(res);
}
fs.stat(filePath, function(err, stat) {
if (err) return next(err);
var mtime = stat.mtime.valueOf();
var etag = 'W/"' + stat.size.toString(16) + "-" + mtime.toString(16) + '"';
var noneMatch = req.headers && req.headers['if-none-match'];
if (noneMatch && noneMatch == etag) {
res.statusCode = 304;
res.end();
return;
}
if (cache[path] && cache[path].etag == etag)
return res.end(cache[path].value);
res.setHeader("ETag", etag);
cache[path] = null;
fs.readFile(filePath, "utf8", function(err, value) {
if (err)
return next(err);
var t = Date.now();
try {
value = exports.transform(value, { path: filePath });
} catch (e) {
return next(e);
}
var delta = Date.now() - t;
res.setHeader('Server-Timing', 'transform=' + delta);
cache[path] = { value: value, etag: etag };
res.end(value);
});
});
};