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

Inline process.browser for better code elimination #2583

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,8 @@
process.browser = false
module.exports = function () {
return process.browser && test(process.browser);
};

function test(val) {
return val;
}
39 changes: 39 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -856,6 +856,45 @@ describe('javascript', function() {
assert.equal(output, 'bartest');
});

it('should replace process.browser on --target=browser', async function() {
let b = await bundle(
path.join(__dirname, '/integration/process/index.js'),
{
target: 'browser'
}
);

let output = await run(b);
assert.ok(output.toString().indexOf('process.browser') === -1);
assert.equal(output(), true);
});

it('should not replace process.browser on --target=node', async function() {
let b = await bundle(
path.join(__dirname, '/integration/process/index.js'),
{
target: 'node'
}
);

let output = await run(b);
assert.ok(output.toString().indexOf('process.browser') !== -1);
assert.equal(output(), false);
});

it('should not replace process.browser on --target=electron', async function() {
let b = await bundle(
path.join(__dirname, '/integration/process/index.js'),
{
target: 'electron'
}
);

let output = await run(b);
assert.ok(output.toString().indexOf('process.browser') !== -1);
assert.equal(output(), false);
});

it('should support adding implicit dependencies', async function() {
let b = await bundle(path.join(__dirname, '/integration/json/index.js'), {
delegate: {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/parcel-bundler/src/assets/JSAsset.js
Expand Up @@ -7,6 +7,7 @@ const babelParser = require('@babel/parser');
const insertGlobals = require('../visitors/globals');
const fsVisitor = require('../visitors/fs');
const envVisitor = require('../visitors/env');
const processVisitor = require('../visitors/process');
const babel = require('../transforms/babel/transform');
const babel7 = require('../transforms/babel/babel7');
const generate = require('@babel/generator').default;
Expand All @@ -20,6 +21,7 @@ const isAccessedVarChanged = require('../utils/isAccessedVarChanged');

const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/;
const ENV_RE = /\b(?:process\.env)\b/;
const BROWSER_RE = /\b(?:process\.browser)\b/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer|define)\b/;
const FS_RE = /\breadFileSync\b/;
const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/;
Expand Down Expand Up @@ -155,6 +157,13 @@ class JSAsset extends Asset {
await this.parseIfNeeded();
this.traverseFast(envVisitor);
}

// Inline process.browser
if (this.options.target === 'browser' && BROWSER_RE.test(this.contents)) {
await this.parseIfNeeded();
this.traverse(processVisitor);
this.isAstDirty = true;
}
}

async transform() {
Expand Down
17 changes: 17 additions & 0 deletions packages/core/parcel-bundler/src/visitors/process.js
@@ -0,0 +1,17 @@
const t = require('@babel/types');

module.exports = {
MemberExpression(path) {
// Inline process.browser
const isProcess = path.node.object.name === 'process';
const isBrowser = path.node.property.name === 'browser';
const isAssignment = path.parentPath.type === 'AssignmentExpression';
if (isProcess && isBrowser) {
if (isAssignment) {
path.parentPath.remove();
} else {
path.replaceWith(t.booleanLiteral(true));
}
}
}
};