Skip to content

Commit

Permalink
Merged in wbinnssmith/tree-shake (pull request #34)
Browse files Browse the repository at this point in the history
Restore tree shaking

Approved-by: Maia Teegarden
  • Loading branch information
Will Binns-Smith committed Apr 14, 2020
2 parents 1f33b21 + 19535ed commit 4accd9e
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.index {
behavior: url(#default#VML);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = window;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const foo = require('./a');
foo.bar = 42;
output = window.bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = window;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {foo} from './a';

foo.bar = 42;

output = window.bar;
17 changes: 17 additions & 0 deletions packages/core/integration-tests/test/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,21 @@ describe('less', function() {
assert(css.includes('.a'));
assert(css.includes('.b'));
});

it('should ignore url() with IE behavior specifiers', async function() {
let b = await bundle(
path.join(__dirname, '/integration/less-url-behavior/index.less'),
);

assertBundles(b, [
{
name: 'index.css',
assets: ['index.less'],
},
]);

let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');

assert(css.includes('url(#default#VML)'));
});
});
21 changes: 7 additions & 14 deletions packages/core/integration-tests/test/output-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const bundle = (name, opts = {}) =>

describe('output formats', function() {
describe('commonjs', function() {
// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support commonjs output (exports)', async function() {
it('should support commonjs output (exports)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/commonjs/exports.js'),
);
Expand All @@ -21,8 +20,7 @@ describe('output formats', function() {
assert.equal((await run(b)).bar, 5);
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support commonjs output (module.exports)', async function() {
it('should support commonjs output (module.exports)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/commonjs/module-exports.js'),
);
Expand All @@ -33,8 +31,7 @@ describe('output formats', function() {
assert.equal(await run(b), 5);
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support commonjs output from esmodule input', async function() {
it('should support commonjs output from esmodule input', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm-commonjs/a.js'),
);
Expand Down Expand Up @@ -75,8 +72,7 @@ describe('output formats', function() {
assert.equal((await run(b)).bar, 3);
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support commonjs output with external modules (named import)', async function() {
it('should support commonjs output with external modules (named import)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/commonjs-external/named.js'),
);
Expand Down Expand Up @@ -414,8 +410,7 @@ describe('output formats', function() {
});

describe('esmodule', function() {
// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support esmodule output (named export)', async function() {
it('should support esmodule output (named export)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm/named.js'),
);
Expand All @@ -426,8 +421,7 @@ describe('output formats', function() {
assert(dist.includes('export const bar = foo + 3'));
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support esmodule output (default identifier)', async function() {
it('should support esmodule output (default identifier)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm/default-value.js'),
);
Expand Down Expand Up @@ -824,8 +818,7 @@ describe('output formats', function() {
assert(!async.includes('.css"'));
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should support building esmodules with split bundles', async function() {
it('should support building esmodules with split bundles', async function() {
let b = await bundle(
path.join(
__dirname,
Expand Down
49 changes: 33 additions & 16 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ describe('scope hoisting', function() {
assert.equal(output, 6);
});

// ATLASSIAN: Unskip when treeshaking is used again
// ATLASSIAN: Warn and return an empty object expression for unresolvable
// imports.
it.skip('excludes default when re-exporting a module', async function() {
let threw = false;
try {
Expand Down Expand Up @@ -737,8 +738,7 @@ describe('scope hoisting', function() {
assert.deepEqual(await run(b), 4);
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('supports importing a namespace from a wrapped module', async function() {
it('supports importing a namespace from a wrapped module', async function() {
let b = await bundle(
path.join(
__dirname,
Expand Down Expand Up @@ -771,8 +771,7 @@ describe('scope hoisting', function() {
});
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('removes unused exports', async function() {
it('removes unused exports', async function() {
let b = await bundle(
path.join(
__dirname,
Expand All @@ -791,8 +790,7 @@ describe('scope hoisting', function() {
assert(!contents.includes('bar'));
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('removes unused function exports when minified', async function() {
it('removes unused function exports when minified', async function() {
let b = await bundle(
path.join(
__dirname,
Expand All @@ -812,8 +810,7 @@ describe('scope hoisting', function() {
assert(!/.-./.test(contents));
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('removes unused transpiled classes using terser when minified', async function() {
it('removes unused transpiled classes using terser when minified', async function() {
let b = await bundle(
path.join(
__dirname,
Expand Down Expand Up @@ -894,8 +891,7 @@ describe('scope hoisting', function() {
assert.deepEqual(output, 'bar');
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('should shake pure property assignments', async function() {
it('should shake pure property assignments', async function() {
let b = await bundle(
path.join(
__dirname,
Expand All @@ -910,8 +906,7 @@ describe('scope hoisting', function() {
b.getBundles()[0].filePath,
'utf8',
);
assert(!/bar/.test(contents));
assert(!/displayName/.test(contents));
assert(!contents.includes('exports.bar ='));
});

it('should correctly rename references to default exported classes', async function() {
Expand All @@ -936,6 +931,17 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output.foo, 'bar');
});

it('does not tree-shake assignments to unknown objects', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/tree-shaking-no-unknown-objects/index.js',
),
);

assert.equal(await run(b), 42);
});
});

describe('commonjs', function() {
Expand Down Expand Up @@ -1467,7 +1473,8 @@ describe('scope hoisting', function() {
assert.equal(output, 6);
});

// ATLASSIAN: Unskip when treeshaking is used again
// ATLASSIAN: Don't inline `require` as `'function'` -- this confuses
// browserify-built bundles like Bitbucket connect-js.
it.skip('supports module object properties', async function() {
let b = await bundle(
path.join(
Expand Down Expand Up @@ -1734,8 +1741,7 @@ describe('scope hoisting', function() {
assert.deepEqual(output, {b: {}});
});

// ATLASSIAN: Unskip when treeshaking is used again
it.skip('removes unused exports', async function() {
it('removes unused exports', async function() {
let b = await bundle(
path.join(
__dirname,
Expand Down Expand Up @@ -1887,6 +1893,17 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output, [4, 2]);
});

it('does not tree-shake assignments to unknown objects', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/tree-shaking-no-unknown-objects/index.js',
),
);

assert.equal(await run(b), 42);
});
});

it('should not throw with JS included from HTML', async function() {
Expand Down
14 changes: 6 additions & 8 deletions packages/shared/scope-hoisting/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
isStringLiteral,
} from '@babel/types';
import traverse from '@babel/traverse';
// import treeShake from './shake';
import treeShake from './shake';
import {assertString, getName, getIdentifier, verifyScopeState} from './utils';
import OutputFormats from './formats/index.js';

Expand Down Expand Up @@ -690,8 +690,6 @@ export function link({
}

// Generate exports
// ATLASSIAN: Disable treeShaking for now. This is used below.
// eslint-disable-next-line no-unused-vars
let exported = format.generateExports(
bundleGraph,
bundle,
Expand All @@ -701,11 +699,11 @@ export function link({
options,
);

// ATLASSIAN: Disable treeShaking for now.
// if (process.env.PARCEL_BUILD_ENV !== 'production') {
// verifyScopeState(path.scope);
// }
// treeShake(path.scope, exported);
if (process.env.PARCEL_BUILD_ENV !== 'production') {
verifyScopeState(path.scope);
}

treeShake(path.scope, exported, exportsMap);
},
},
});
Expand Down
Loading

0 comments on commit 4accd9e

Please sign in to comment.