Skip to content

Commit

Permalink
Merge branch 'v2' into wbinnssmith/css-sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Binns-Smith committed Apr 15, 2020
2 parents d55c33f + f9f7320 commit a0c4200
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 46 deletions.
22 changes: 13 additions & 9 deletions packages/core/core/src/BundleGraph.js
Expand Up @@ -647,15 +647,9 @@ export default class BundleGraph {
}

// Resolve the export `symbol` of `asset` to the source,
// stopping at the first asset after leaving `bundle` (symbol is null in that case)
// stopping at the first asset after leaving `bundle` (symbol is nullish in that case)
resolveSymbol(asset: Asset, symbol: Symbol, boundary: ?Bundle) {
if (boundary && !this.bundleHasAsset(boundary, asset)) {
return {
asset,
exportSymbol: symbol,
symbol: undefined,
};
}
let assetOutside = boundary && !this.bundleHasAsset(boundary, asset);

let identifier = asset.symbols.get(symbol);
if (symbol === '*') {
Expand All @@ -676,6 +670,11 @@ export default class BundleGraph {
break;
}

if (assetOutside) {
// We found the symbol, but `asset` is outside, return `asset` and the original symbol
break;
}

let {
asset: resolvedAsset,
symbol: resolvedSymbol,
Expand All @@ -701,7 +700,12 @@ export default class BundleGraph {
let resolved = this.getDependencyResolution(dep);
if (!resolved) continue;
let result = this.resolveSymbol(resolved, symbol, boundary);
if (result.symbol != null) {
if (result.symbol != undefined) {
if (assetOutside) {
// We found the symbol, but `asset` is outside, return `asset` and the original symbol
break;
}

return {
asset: result.asset,
symbol: result.symbol,
Expand Down
11 changes: 9 additions & 2 deletions packages/core/core/src/public/Asset.js
Expand Up @@ -20,7 +20,7 @@ import type {
MutableAsset as IMutableAsset,
PackageJSON,
Stats,
Symbol,
Symbol as ISymbol,
} from '@parcel/types';
import type {Asset as AssetValue, ParcelOptions} from '../types';

Expand All @@ -31,6 +31,8 @@ import UncommittedAsset from '../UncommittedAsset';
import CommittedAsset from '../CommittedAsset';
import {createEnvironment} from '../Environment';

const inspect = Symbol.for('nodejs.util.inspect.custom');

const assetValueToAsset: WeakMap<AssetValue, Asset> = new WeakMap();
const assetValueToMutableAsset: WeakMap<
AssetValue,
Expand Down Expand Up @@ -76,6 +78,11 @@ class BaseAsset {
_assetToAssetValue.set(this, asset.value);
}

// $FlowFixMe
[inspect]() {
return `Asset(${this.filePath})`;
}

get id(): string {
return this.#asset.value.id;
}
Expand Down Expand Up @@ -120,7 +127,7 @@ class BaseAsset {
return this.#asset.value.sideEffects;
}

get symbols(): Map<Symbol, Symbol> {
get symbols(): Map<ISymbol, ISymbol> {
return this.#asset.value.symbols;
}

Expand Down
11 changes: 9 additions & 2 deletions packages/core/core/src/public/Dependency.js
Expand Up @@ -4,13 +4,15 @@ import type {
Environment as IEnvironment,
SourceLocation,
Meta,
Symbol,
Symbol as ISymbol,
} from '@parcel/types';
import type {Dependency as InternalDependency} from '../types';
import Environment from './Environment';
import Target from './Target';
import nullthrows from 'nullthrows';

const inspect = Symbol.for('nodejs.util.inspect.custom');

const internalDependencyToDependency: WeakMap<
InternalDependency,
Dependency,
Expand Down Expand Up @@ -39,6 +41,11 @@ export default class Dependency implements IDependency {
internalDependencyToDependency.set(dep, this);
}

// $FlowFixMe
[inspect]() {
return `Dependency(${String(this.sourcePath)} -> ${this.moduleSpecifier})`;
}

get id(): string {
return this.#dep.id;
}
Expand Down Expand Up @@ -98,7 +105,7 @@ export default class Dependency implements IDependency {
return this.#dep.sourcePath;
}

get symbols(): Map<Symbol, Symbol> {
get symbols(): Map<ISymbol, ISymbol> {
return this.#dep.symbols;
}

Expand Down
@@ -0,0 +1,3 @@
.index {
behavior: url(#default#VML);
}
@@ -0,0 +1 @@
module.exports = window;
@@ -0,0 +1,3 @@
const foo = require('./a');
foo.bar = 42;
output = window.bar;
@@ -0,0 +1,4 @@
import { layers } from './library/index.js';

export default layers.a;

@@ -0,0 +1,3 @@
import t from "./library/components.js";

output = import("./async.js").then((c) => [c.default, t().a]);
@@ -0,0 +1,4 @@
import { layers } from './constants.js';
export default function(){
return layers;
}
@@ -0,0 +1,3 @@
export const layers = {
a: "foo",
};
@@ -0,0 +1 @@
export * from "./constants.js";
@@ -0,0 +1,3 @@
import { themed } from './theme/components.js';

output = import('./media-card/index.js').then(m => [m.default, themed()])
@@ -0,0 +1,3 @@
import { layers } from '../theme/index.js';

export default Object.keys(layers);
@@ -0,0 +1,4 @@
export { createTheme } from "./createTheme.js";
export function themed() {
return "themed";
}
@@ -0,0 +1,3 @@
export function createTheme() {
return "foo";
}
@@ -0,0 +1,5 @@
export var layers = {
a: 1,
b: 2
};
export * from './createTheme.js';
@@ -0,0 +1 @@
export const foo = window;
@@ -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
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)'));
});
});
51 changes: 48 additions & 3 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -201,7 +201,7 @@ describe('scope hoisting', function() {
assert.equal(output, 15);
});

it('can import from a different bundle via a re-export', async function() {
it('can import from a different bundle via a re-export (1)', async function() {
let b = await bundle(
path.join(
__dirname,
Expand All @@ -225,6 +225,30 @@ describe('scope hoisting', function() {
assert.deepEqual(output, ['operational', 'ui']);
});

it('can import from a different bundle via a re-export (2)', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/re-export-bundle-boundary2/index.js',
),
);

let output = await run(b);
assert.deepEqual(output, ['foo', 'foo']);
});

it('can import from its own bundle with a split package', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/re-export-bundle-boundary3/index.js',
),
);

let output = await run(b);
assert.deepEqual(output, [['a', 'b'], 'themed']);
});

it('supports importing all exports re-exported from multiple modules deep', async function() {
let b = await bundle(
path.join(
Expand Down Expand Up @@ -904,8 +928,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 @@ -930,6 +953,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 @@ -1879,6 +1913,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
20 changes: 10 additions & 10 deletions packages/dev/babel-preset/index.js
Expand Up @@ -4,17 +4,17 @@ module.exports = () => ({
require('@babel/preset-env'),
{
targets: {
node: 8
}
}
node: 10,
},
},
],
require('@babel/preset-react'),
require('@babel/preset-flow')
require('@babel/preset-flow'),
],
plugins: [
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-proposal-nullish-coalescing-operator'),
require('@babel/plugin-proposal-optional-chaining')
require('@babel/plugin-proposal-optional-chaining'),
],
env: {
production: {
Expand All @@ -23,10 +23,10 @@ module.exports = () => ({
// it can be removed through dead code elimination below
[
'babel-plugin-transform-inline-environment-variables',
{include: ['PARCEL_BUILD_ENV']}
{include: ['PARCEL_BUILD_ENV']},
],
'babel-plugin-minify-dead-code-elimination'
]
}
}
'babel-plugin-minify-dead-code-elimination',
],
},
},
});
2 changes: 1 addition & 1 deletion packages/shared/scope-hoisting/src/link.js
Expand Up @@ -697,7 +697,7 @@ export function link({
verifyScopeState(path.scope);
}

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

0 comments on commit a0c4200

Please sign in to comment.