Merge pull request +16009 from c9/commonjs

Add support for commonjs and es6
This commit is contained in:
Matthijs van Henten 2018-03-12 10:24:29 +01:00 committed by GitHub
commit 2af1c7c68e
8 changed files with 233 additions and 109 deletions

View File

@ -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
});
}
});

View File

@ -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;

View File

@ -94,6 +94,11 @@ function plugin(options, imports, register) {
source: "query",
optional: true
},
settings: {
type: "number",
source: "query",
optional: true
},
}
}, function(req, res, next) {
@ -116,11 +121,18 @@ function plugin(options, imports, register) {
});
opts.options.debug = req.params.debug !== undefined;
var workspaceSettings = getSettings(configName, options);
res.setHeader("Cache-Control", "no-cache, no-store");
if (req.params.settings == 1)
return res.json(workspaceSettings);
if (req.params.config == 1)
return res.json(getConfig(configName, opts));
return res.json(getConfig(configName, workspaceSettings));
res.render(__dirname + "/views/standalone.html.ejs", {
architectConfig: getConfig(configName, opts),
architectConfig: getConfig(configName, workspaceSettings),
configName: configName,
packed: opts.packed,
standalone: true,
@ -326,9 +338,7 @@ function getConfigName(requested, options) {
return name;
}
function getConfig(configName, options) {
var filename = __dirname + "/../../configs/client-" + configName + ".js";
function getSettings(configName, options) {
var installPath = options.settingDir || options.installPath || "";
var workspaceDir = options.options.workspaceDir;
var settings = {
@ -347,6 +357,14 @@ function getConfig(configName, options) {
settings[type] = data;
}
options.options.settings = settings;
return require(filename)(options.options);
options.options.configName = configName;
options.options.manifest = {
version: options.options.manifest.version
};
return options.options;
}
function getConfig(configName, options) {
var filename = __dirname + "/../../configs/client-" + configName + ".js";
return require(filename)(options);
}

View File

@ -23,6 +23,7 @@
</div>
</div>
<script>
var start = Date.now();
var loadingIde = document.querySelector("#loadingide");
document.body.className = "loading " + loadingIde.className;
@ -95,88 +96,89 @@
<script src="/static/mini_require.js"></script>
<script src="/configs/require_config.js"></script>
<script>
var start = Date.now();
var plugins;
require(["lib/architect/architect", "text!/ide.html?config=1"], function (architect, pluginJSON) {
plugins = JSON.parse(pluginJSON);
plugins.push({
consumes: [],
provides: ["auth.bootstrap"],
setup: function(options, imports, register) {
register(null, {
"auth.bootstrap": {
login: function(callback) { callback(); }
require(["text!/ide.html?settings=1"], function(workspaceSettings) {
workspaceSettings = JSON.parse(workspaceSettings);
require(["lib/architect/architect", "configs/ide/" + workspaceSettings.configName], function(architect, config) {
plugins = config(workspaceSettings);
plugins.push({
consumes: [],
provides: ["auth.bootstrap"],
setup: function(options, imports, register) {
register(null, {
"auth.bootstrap": {
login: function(callback) { callback(); }
}
});
}
});
architect.resolveConfig(plugins, function (err, config) {
if (err) throw err;
var errored;
var app = architect.createApp(config, function(err, app){
if (err) {
errored = true;
console.error(err.stack);
alert(err);
}
});
}
});
architect.resolveConfig(plugins, function (err, config) {
if (err) throw err;
var errored;
var app = architect.createApp(config, function(err, app){
if (err) {
errored = true;
app.on("error", function(err){
console.error(err.stack);
alert(err);
}
});
app.on("error", function(err){
console.error(err.stack);
if (!errored)
alert(err);
});
app.on("service", function(name, plugin, options) {
if (!plugin.name)
plugin.name = name;
});
app.on("ready", function() {
window.app = app.services;
window.app.__defineGetter__("_ace", function(){
return this.tabManager.focussedTab.editor.ace;
});
Object.keys(window.app).forEach(function(n) {
if (/[^\w]/.test(n))
window.app[n.replace(/[^\w]/, "_") + "_"] = window.app[n];
if (!errored)
alert(err);
});
done();
});
// For Development only
function done() {
var vfs = app.services.vfs;
var c9 = app.services.c9;
var settings = app.services.settings;
app.on("service", function(name, plugin, options) {
if (!plugin.name)
plugin.name = name;
});
c9.ready();
c9.totalLoadTime = Date.now() - start;
console.warn("Total Load Time: ", Date.now() - start);
if (window.hideLoader) {
var waitVfs = function(fn) {
vfs.connected ? fn() : vfs.once("connect", fn);
};
var waitSettings = function(fn) {
settings.inited ? fn() : settings.once("read", fn);
};
var waitTheme = function(fn) {
var layout = app.services.layout;
if (!layout || layout.hasTheme) return fn();
layout.once("eachTheme", fn);
};
waitTheme(waitSettings.bind(null, window.hideLoader));
app.on("ready", function() {
window.app = app.services;
window.app.__defineGetter__("_ace", function(){
return this.tabManager.focussedTab.editor.ace;
});
Object.keys(window.app).forEach(function(n) {
if (/[^\w]/.test(n))
window.app[n.replace(/[^\w]/, "_") + "_"] = window.app[n];
});
done();
});
// For Development only
function done() {
var vfs = app.services.vfs;
var c9 = app.services.c9;
var settings = app.services.settings;
c9.ready();
c9.totalLoadTime = Date.now() - start;
console.warn("Total Load Time: ", Date.now() - start);
if (window.hideLoader) {
var waitVfs = function(fn) {
vfs.connected ? fn() : vfs.once("connect", fn);
};
var waitSettings = function(fn) {
settings.inited ? fn() : settings.once("read", fn);
};
var waitTheme = function(fn) {
var layout = app.services.layout;
if (!layout || layout.hasTheme) return fn();
layout.once("eachTheme", fn);
};
waitTheme(waitSettings.bind(null, window.hideLoader));
}
}
}
}, function loadError(mod) {
if (mod.id === "plugins/c9.ide.clipboard/html5")
return alert("Unable to load html5.js.\n\nThis may be caused by a false positive in your virus scanner. Please try reloading with ?packed=1 added to the URL.");
}, function loadError(mod) {
if (mod.id === "plugins/c9.ide.clipboard/html5")
return alert("Unable to load html5.js.\n\nThis may be caused by a false positive in your virus scanner. Please try reloading with ?packed=1 added to the URL.");
});
});
});
</script>

View File

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

View File

@ -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)

View File

@ -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;

89
plugins/node_modules/architect-build/transform.js generated vendored Normal file
View File

@ -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);
});
});
};