From bd9fd9f6193c3f18efa03f897a33906869808b96 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Sun, 14 Jan 2018 07:51:55 +0100 Subject: [PATCH] Use browser-resolve to ignore fs inlining (#523) --- src/visitors/fs.js | 8 +++++++- test/fs.js | 20 ++++++++++++++++++- test/integration/resolve-entries/ignore-fs.js | 5 +++++ .../resolve-entries/pkg-ignore-fs/index.js | 9 +++++++++ .../pkg-ignore-fs/package.json | 7 +++++++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/integration/resolve-entries/ignore-fs.js create mode 100644 test/integration/resolve-entries/pkg-ignore-fs/index.js create mode 100644 test/integration/resolve-entries/pkg-ignore-fs/package.json diff --git a/src/visitors/fs.js b/src/visitors/fs.js index 32f13b9bedd..aa83a2fbd2f 100644 --- a/src/visitors/fs.js +++ b/src/visitors/fs.js @@ -20,7 +20,13 @@ module.exports = { }, CallExpression(path, asset) { - if (referencesImport(path, 'fs', 'readFileSync')) { + // See https://github.com/defunctzombie/node-browser-resolve#skip + let ignore = + asset.package && + asset.package.browser && + asset.package.browser.fs === false; + + if (!ignore && referencesImport(path, 'fs', 'readFileSync')) { let vars = { __dirname: Path.dirname(asset.name), __filename: asset.basename diff --git a/test/fs.js b/test/fs.js index 557271bc3ae..aa03448bc51 100644 --- a/test/fs.js +++ b/test/fs.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const {bundle, run} = require('./utils'); +const {bundle, run, assertBundleTree} = require('./utils'); describe('fs', function() { it('should inline a file as a string', async function() { @@ -52,4 +52,22 @@ describe('fs', function() { let output = run(b); assert.equal(output, 'hello'); }); + + it('should not evaluate fs calls when package.browser.fs is false', async function() { + let b = await bundle( + __dirname + '/integration/resolve-entries/ignore-fs.js' + ); + + assertBundleTree(b, { + name: 'ignore-fs.js', + // empty.js is generated by require('fs'), it gets mocked with an empty module + assets: ['empty.js', 'ignore-fs.js', 'index.js'], + childBundles: [] + }); + + let output = run(b); + + assert.equal(typeof output.test, 'function'); + assert.equal(output.test(), 'test-pkg-ignore-fs-ok'); + }); }); diff --git a/test/integration/resolve-entries/ignore-fs.js b/test/integration/resolve-entries/ignore-fs.js new file mode 100644 index 00000000000..3d3e9c56de0 --- /dev/null +++ b/test/integration/resolve-entries/ignore-fs.js @@ -0,0 +1,5 @@ +import file from './pkg-ignore-fs' + +export function test() { + return file() +} diff --git a/test/integration/resolve-entries/pkg-ignore-fs/index.js b/test/integration/resolve-entries/pkg-ignore-fs/index.js new file mode 100644 index 00000000000..52c1f8227af --- /dev/null +++ b/test/integration/resolve-entries/pkg-ignore-fs/index.js @@ -0,0 +1,9 @@ +export default () => { + try { + // We expect browser-resolve to replace fs with an empty module, so readFileSync will be undefined + return require('fs').readFileSync(__dirname + '/package.json') + } + catch(_) { + return 'test-pkg-ignore-fs-ok' + } +} diff --git a/test/integration/resolve-entries/pkg-ignore-fs/package.json b/test/integration/resolve-entries/pkg-ignore-fs/package.json new file mode 100644 index 00000000000..34d9be289ab --- /dev/null +++ b/test/integration/resolve-entries/pkg-ignore-fs/package.json @@ -0,0 +1,7 @@ +{ + "name": "pkg-ignore-fs", + "main": "index.js", + "browser": { + "fs": false + } +}