Skip to content

Commit

Permalink
All CommonJS and AMD modules to be transpiled
Browse files Browse the repository at this point in the history
This enables AMD and CommonJS modules to also be transpiled (run through
		Babel). Still a little bit of a WIP. Closes #1276
  • Loading branch information
matthewp committed Nov 5, 2019
1 parent c9dd1eb commit b1426b2
Show file tree
Hide file tree
Showing 21 changed files with 382 additions and 57 deletions.
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = function (grunt) {
"src/extension-pretty-name.js",
"src/extension-tree-shaking.js",
"src/extension-mjs.js",
"src/extension-forward-metadata.js",
"src/trace/trace.js",
"src/json/json.js",
"src/cache-bust/cache-bust.js",
Expand Down Expand Up @@ -134,6 +135,7 @@ module.exports = function (grunt) {
"src/extension-pretty-name.js",
"src/extension-tree-shaking.js",
"src/extension-mjs.js",
"src/extension-forward-metadata.js",
"src/trace/trace.js",
"src/json/json.js",
"src/cache-bust/cache-bust.js",
Expand Down Expand Up @@ -163,6 +165,7 @@ module.exports = function (grunt) {
"src/extension-pretty-name.js",
"src/extension-tree-shaking.js",
"src/extension-mjs.js",
"src/extension-forward-metadata.js",
"src/trace/trace.js",
"src/json/json.js",
"src/cache-bust/cache-bust.js",
Expand Down
31 changes: 30 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,11 @@ addStealExtension(function addMetaDeps(loader) {
}

loader.transpile = function (load) {
// TODO this needs to change
prependDeps(this, load, createImport);
var result = superTranspile.apply(this, arguments);
return result;
}
};

loader._determineFormat = function (load) {
if(load.metadata.format === 'cjs') {
Expand Down Expand Up @@ -1356,6 +1357,31 @@ addStealExtension(function addMJS(loader){
};
});

// This extension allows you to define metadata that should appear on
// Any subdependencies. For example we can add the { foo: true } bool
// to a module's metadata, and it will be forwarded to any subdependency.
addStealExtension(function forwardMetadata(loader){
loader._forwardedMetadata = {};
loader.setForwardedMetadata = function(prop) {
loader._forwardedMetadata[prop] = true;
};

loader.forwardMetadata = function(load, parentName) {
if(parentName) {
var parentLoad = this.getModuleLoad(parentName);

if(parentLoad) {
// TODO use Object.assign instead?
for(var p in this._forwardedMetadata) {
if(p in parentLoad.metadata) {
load.metadata[p] = parentLoad.metadata[p];
}
}
}
}
};
});

addStealExtension(function applyTraceExtension(loader) {
loader._traceData = {
loads: {},
Expand Down Expand Up @@ -1735,6 +1761,7 @@ addStealExtension(function addCacheBust(loader) {
System.ext = Object.create(null);
System.logLevel = 0;
System.forceES5 = true;
System.transpileAllFormats = true;
var cssBundlesNameGlob = "bundles/*.css",
jsBundlesNameGlob = "bundles/*";
setIfNotPresent(System.paths,cssBundlesNameGlob, "dist/bundles/*css");
Expand Down Expand Up @@ -1988,9 +2015,11 @@ addStealExtension(function addCacheBust(loader) {
this.paths["@@babel-code-frame"] = dirname+"/ext/babel-code-frame.js";
setIfNotPresent(this.meta,"traceur",{"exports":"traceur"});
setIfNotPresent(this.meta, "@@babel-code-frame", {"format":"global","exports":"BabelCodeFrame"});
setIfNotPresent(this.meta, "babel", {"shouldTranspile": false});

// steal-clone is contextual so it can override modules using relative paths
this.setContextual('steal-clone', 'steal-clone');
this.setForwardedMetadata("shouldTranspile");

if(isNode) {
if(this.configMain === "@config" && last(parts) === "steal") {
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
System.ext = Object.create(null);
System.logLevel = 0;
System.forceES5 = true;
System.transpileAllFormats = true;
var cssBundlesNameGlob = "bundles/*.css",
jsBundlesNameGlob = "bundles/*";
setIfNotPresent(System.paths,cssBundlesNameGlob, "dist/bundles/*css");
Expand Down Expand Up @@ -293,9 +294,11 @@
this.paths["@@babel-code-frame"] = dirname+"/ext/babel-code-frame.js";
setIfNotPresent(this.meta,"traceur",{"exports":"traceur"});
setIfNotPresent(this.meta, "@@babel-code-frame", {"format":"global","exports":"BabelCodeFrame"});
setIfNotPresent(this.meta, "babel", {"shouldTranspile": false});

// steal-clone is contextual so it can override modules using relative paths
this.setContextual('steal-clone', 'steal-clone');
this.setForwardedMetadata("shouldTranspile");

if(isNode) {
if(this.configMain === "@config" && last(parts) === "steal") {
Expand Down
24 changes: 24 additions & 0 deletions src/extension-forward-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This extension allows you to define metadata that should appear on
// Any subdependencies. For example we can add the { foo: true } bool
// to a module's metadata, and it will be forwarded to any subdependency.
addStealExtension(function forwardMetadata(loader){
loader._forwardedMetadata = {};
loader.setForwardedMetadata = function(prop) {
loader._forwardedMetadata[prop] = true;
};

loader.forwardMetadata = function(load, parentName) {
if(parentName) {
var parentLoad = this.getModuleLoad(parentName);

if(parentLoad) {
// TODO use Object.assign instead?
for(var p in this._forwardedMetadata) {
if(p in parentLoad.metadata) {
load.metadata[p] = parentLoad.metadata[p];
}
}
}
}
};
});
3 changes: 2 additions & 1 deletion src/extension-no-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ addStealExtension(function addNoMainWarn(loader) {
"package.json!npm": true,
"npm": true,
"@empty": true,
"@dev": true
"@dev": true,
"babel": true
};

var loaderImport = loader.import;
Expand Down
23 changes: 20 additions & 3 deletions src/loader/lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ function logloads(loads) {
load = createLoad(name);
loader.loads.push(load);

loader.loaderObj.forwardMetadata(load, refererName);

proceedToLocate(loader, load);

return load;
Expand Down Expand Up @@ -245,6 +247,10 @@ function logloads(loads) {
);
}

function transpilableFormat(format) {
return format === "cjs" || format === "amd";
}

var anonCnt = 0;

// 15.2.4.5
Expand Down Expand Up @@ -300,9 +306,20 @@ function logloads(loads) {
});
}
else if (typeof instantiateResult == 'object') {
load.depsList = instantiateResult.deps || [];
load.execute = instantiateResult.execute;
load.isDeclarative = false;
function addToLoad() {
load.depsList = instantiateResult.deps || [];
load.execute = instantiateResult.execute;
load.isDeclarative = false;
}

if(!loader.loaderObj.transpileAllFormats ||
load.metadata.shouldTranspile === false ||
!transpilableFormat(load.metadata.format)) {
return addToLoad();
}

return loader.loaderObj.transpile(load).then(addToLoad);

}
else
throw TypeError('Invalid instantiate return value');
Expand Down
19 changes: 15 additions & 4 deletions src/loader/lib/transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
}
catch(e) {
// traceur throws an error array
throw e[0];
var error = e[0] || e.errors[0];
throw error;
}
}

Expand Down Expand Up @@ -217,7 +218,12 @@
var npmPluginNameOrPath = getNpmPluginNameOrPath(name);

// import the plugin!
promises.push(this["import"](npmPluginNameOrPath, { name: parent })
promises.push(this["import"](npmPluginNameOrPath, {
name: parent,
metadata: {
shouldTranspile: false
}
})
.then(function(mod) {
var exported = mod.__esModule ? mod["default"] : mod;

Expand Down Expand Up @@ -298,7 +304,7 @@
function getBabelPresets(current, loader) {
var presets = current || [];
var forceES5 = loader.forceES5 !== false;
var defaultPresets = forceES5
var defaultPresets = forceES5
? [babelES2015Preset, "react", "stage-0"]
: ["react"];

Expand Down Expand Up @@ -438,7 +444,12 @@
var npmPresetNameOrPath = getNpmPresetNameOrPath(name);

// import the preset!
promises.push(this["import"](npmPresetNameOrPath, { name: parent })
promises.push(this["import"](npmPresetNameOrPath, {
name: parent,
metadata: {
shouldTranspile: false
}
})
.then(function(mod) {
var exported = mod.__esModule ? mod["default"] : mod;

Expand Down
42 changes: 35 additions & 7 deletions src/loader/loader-with-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,8 @@ function logloads(loads) {
load = createLoad(name);
loader.loads.push(load);

loader.loaderObj.forwardMetadata(load, refererName);

proceedToLocate(loader, load);

return load;
Expand Down Expand Up @@ -1555,6 +1557,10 @@ function logloads(loads) {
);
}

function transpilableFormat(format) {
return format === "cjs" || format === "amd";
}

var anonCnt = 0;

// 15.2.4.5
Expand Down Expand Up @@ -1610,9 +1616,20 @@ function logloads(loads) {
});
}
else if (typeof instantiateResult == 'object') {
load.depsList = instantiateResult.deps || [];
load.execute = instantiateResult.execute;
load.isDeclarative = false;
function addToLoad() {
load.depsList = instantiateResult.deps || [];
load.execute = instantiateResult.execute;
load.isDeclarative = false;
}

if(!loader.loaderObj.transpileAllFormats ||
load.metadata.shouldTranspile === false ||
!transpilableFormat(load.metadata.format)) {
return addToLoad();
}

return loader.loaderObj.transpile(load).then(addToLoad);

}
else
throw TypeError('Invalid instantiate return value');
Expand Down Expand Up @@ -2648,7 +2665,8 @@ function logloads(loads) {
}
catch(e) {
// traceur throws an error array
throw e[0];
var error = e[0] || e.errors[0];
throw error;
}
}

Expand Down Expand Up @@ -2768,7 +2786,12 @@ function logloads(loads) {
var npmPluginNameOrPath = getNpmPluginNameOrPath(name);

// import the plugin!
promises.push(this["import"](npmPluginNameOrPath, { name: parent })
promises.push(this["import"](npmPluginNameOrPath, {
name: parent,
metadata: {
shouldTranspile: false
}
})
.then(function(mod) {
var exported = mod.__esModule ? mod["default"] : mod;

Expand Down Expand Up @@ -2849,7 +2872,7 @@ function logloads(loads) {
function getBabelPresets(current, loader) {
var presets = current || [];
var forceES5 = loader.forceES5 !== false;
var defaultPresets = forceES5
var defaultPresets = forceES5
? [babelES2015Preset, "react", "stage-0"]
: ["react"];

Expand Down Expand Up @@ -2989,7 +3012,12 @@ function logloads(loads) {
var npmPresetNameOrPath = getNpmPresetNameOrPath(name);

// import the preset!
promises.push(this["import"](npmPresetNameOrPath, { name: parent })
promises.push(this["import"](npmPresetNameOrPath, {
name: parent,
metadata: {
shouldTranspile: false
}
})
.then(function(mod) {
var exported = mod.__esModule ? mod["default"] : mod;

Expand Down
Loading

0 comments on commit b1426b2

Please sign in to comment.