Skip to content

Commit

Permalink
Inline process.browser for better code elimination (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
garthenweb authored and DeMoorJasper committed Feb 6, 2019
1 parent eb759a4 commit 4c5993f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
@@ -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));
}
}
}
};

0 comments on commit 4c5993f

Please sign in to comment.