Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All CommonJS and AMD modules to be transpiled #1513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that .transpile() is called on all formats, prependDeps needs to check the load.format.

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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the right option. In the PR we said transpile would be it.

Also this has to default to false or it will be a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

steal.loader.transpile is a function that's used to do transpilation. We could support the transpile: true option, but this would be tricky to implement. I don't think a slightly better name is actually worth it and transpileAllFormats is just fine.

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