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

Include babel syntax plugins when tree shaking #1475

Merged
merged 1 commit into from
Nov 12, 2018
Merged
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
24 changes: 21 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,25 @@ addStealExtension(function(loader) {
};
}

// Collect syntax plugins, because we need to always include these.
var getSyntaxPlugins = (function(){
var plugins;
return function(babel) {
if(!plugins) {
plugins = [];
for(var p in babel.availablePlugins) {
if(p.indexOf("syntax-") === 0) {
plugins.push(babel.availablePlugins[p]);
}
}
}
return plugins;
};
})();




function applyBabelPlugin(load) {
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
Expand All @@ -1225,9 +1244,8 @@ addStealExtension(function(loader) {
var babel = transpiler.Babel || transpiler.babel || transpiler;

try {
var babelPlugins = [
loader._getImportSpecifierPositionsPlugin.bind(null, load)
];
var babelPlugins = [].concat(getSyntaxPlugins(babel));
babelPlugins.push(loader._getImportSpecifierPositionsPlugin.bind(null, load));
if(treeShakingEnabled(loader, load)) {
babelPlugins.push(treeShakePlugin.bind(null, loader, load));
}
Expand Down
24 changes: 21 additions & 3 deletions src/extension-tree-shaking.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,25 @@ addStealExtension(function(loader) {
};
}

// Collect syntax plugins, because we need to always include these.
var getSyntaxPlugins = (function(){
var plugins;
return function(babel) {
if(!plugins) {
plugins = [];
for(var p in babel.availablePlugins) {
if(p.indexOf("syntax-") === 0) {
plugins.push(babel.availablePlugins[p]);
}
}
}
return plugins;
};
})();




function applyBabelPlugin(load) {
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
Expand All @@ -282,9 +301,8 @@ addStealExtension(function(loader) {
var babel = transpiler.Babel || transpiler.babel || transpiler;

try {
var babelPlugins = [
loader._getImportSpecifierPositionsPlugin.bind(null, load)
];
var babelPlugins = [].concat(getSyntaxPlugins(babel));
babelPlugins.push(loader._getImportSpecifierPositionsPlugin.bind(null, load));
if(treeShakingEnabled(loader, load)) {
babelPlugins.push(treeShakePlugin.bind(null, loader, load));
}
Expand Down
24 changes: 21 additions & 3 deletions steal-with-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -7380,6 +7380,25 @@ addStealExtension(function(loader) {
};
}

// Collect syntax plugins, because we need to always include these.
var getSyntaxPlugins = (function(){
var plugins;
return function(babel) {
if(!plugins) {
plugins = [];
for(var p in babel.availablePlugins) {
if(p.indexOf("syntax-") === 0) {
plugins.push(babel.availablePlugins[p]);
}
}
}
return plugins;
};
})();




function applyBabelPlugin(load) {
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
Expand All @@ -7389,9 +7408,8 @@ addStealExtension(function(loader) {
var babel = transpiler.Babel || transpiler.babel || transpiler;

try {
var babelPlugins = [
loader._getImportSpecifierPositionsPlugin.bind(null, load)
];
var babelPlugins = [].concat(getSyntaxPlugins(babel));
babelPlugins.push(loader._getImportSpecifierPositionsPlugin.bind(null, load));
if(treeShakingEnabled(loader, load)) {
babelPlugins.push(treeShakePlugin.bind(null, loader, load));
}
Expand Down
2 changes: 1 addition & 1 deletion steal-with-promises.production.js

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions steal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6110,6 +6110,25 @@ addStealExtension(function(loader) {
};
}

// Collect syntax plugins, because we need to always include these.
var getSyntaxPlugins = (function(){
var plugins;
return function(babel) {
if(!plugins) {
plugins = [];
for(var p in babel.availablePlugins) {
if(p.indexOf("syntax-") === 0) {
plugins.push(babel.availablePlugins[p]);
}
}
}
return plugins;
};
})();




function applyBabelPlugin(load) {
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
Expand All @@ -6119,9 +6138,8 @@ addStealExtension(function(loader) {
var babel = transpiler.Babel || transpiler.babel || transpiler;

try {
var babelPlugins = [
loader._getImportSpecifierPositionsPlugin.bind(null, load)
];
var babelPlugins = [].concat(getSyntaxPlugins(babel));
babelPlugins.push(loader._getImportSpecifierPositionsPlugin.bind(null, load));
if(treeShakingEnabled(loader, load)) {
babelPlugins.push(treeShakePlugin.bind(null, loader, load));
}
Expand Down
2 changes: 1 addition & 1 deletion steal.production.js

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions test/steal-with-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -7380,6 +7380,25 @@ addStealExtension(function(loader) {
};
}

// Collect syntax plugins, because we need to always include these.
var getSyntaxPlugins = (function(){
var plugins;
return function(babel) {
if(!plugins) {
plugins = [];
for(var p in babel.availablePlugins) {
if(p.indexOf("syntax-") === 0) {
plugins.push(babel.availablePlugins[p]);
}
}
}
return plugins;
};
})();




function applyBabelPlugin(load) {
var loader = this;
var pluginLoader = loader.pluginLoader || loader;
Expand All @@ -7389,9 +7408,8 @@ addStealExtension(function(loader) {
var babel = transpiler.Babel || transpiler.babel || transpiler;

try {
var babelPlugins = [
loader._getImportSpecifierPositionsPlugin.bind(null, load)
];
var babelPlugins = [].concat(getSyntaxPlugins(babel));
babelPlugins.push(loader._getImportSpecifierPositionsPlugin.bind(null, load));
if(treeShakingEnabled(loader, load)) {
babelPlugins.push(treeShakePlugin.bind(null, loader, load));
}
Expand Down
2 changes: 1 addition & 1 deletion test/steal-with-promises.production.js

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions test/tree_shake/syntax.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tree shaking with advanced syntax</title>
</head>
<body>
<script>
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script
src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"></script>
<script>
function assertSyntax() {
var mod = steal.loader.get("shake@1.0.0#mod");
var modB = steal.loader.get("shake@1.0.0#mod_b");

if(typeof window.assert !== "undefined") {
window.assert.equal(mod.a, "a", "a was not tree shaken");
window.assert.equal(mod.b, undefined, "b was tree shaken");
window.assert.equal(mod.d, "d", "d was not tree shaken");

window.assert.equal(modB, undefined, "b was not loaded at all");
} else {
console.log("mod", mod);
console.log("mod_b", modB);
}
}

steal.done()
.then(function() {
return steal.import("~/syntax_main");
})
.then(assertSyntax)
.then(function(){
if(window.assert) {
window.done();
}
});
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions test/tree_shake/syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { a, c } from "./mod";

const obj = { a: 'b' };
const another = { ...obj };

export default function() {

}
4 changes: 4 additions & 0 deletions test/tree_shake/syntax_main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { a } from "./mod";
import s from "./syntax";

window.SYNTAX_APP = { a, s };