From 10cfca994ab300534d1236b3535ac27ee0140b2f Mon Sep 17 00:00:00 2001 From: Lex Alexander Date: Wed, 17 Oct 2018 17:58:32 -0700 Subject: [PATCH] feat(autoinstall): Add disabling of autoinstall globally via environment variable -Closes #2130. --- packages/core/parcel-bundler/src/Bundler.js | 11 ++++++---- .../dont-autoinstall-if-env-var-is-false/.env | 1 + .../index.js | 4 ++++ .../package.json | 4 ++++ .../core/parcel-bundler/test/javascript.js | 20 +++++++++++++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/.env create mode 100644 packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/index.js create mode 100644 packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/package.json diff --git a/packages/core/parcel-bundler/src/Bundler.js b/packages/core/parcel-bundler/src/Bundler.js index 97064a08b9e..e478a590cc3 100644 --- a/packages/core/parcel-bundler/src/Bundler.js +++ b/packages/core/parcel-bundler/src/Bundler.js @@ -103,6 +103,12 @@ class Bundler extends EventEmitter { : watch; const scopeHoist = options.scopeHoist !== undefined ? options.scopeHoist : false; + const autoInstall = + typeof options.autoinstall === 'boolean' + ? options.autoinstall + : typeof process.env.PARCEL_AUTOINSTALL === 'boolean' + ? process.env.PARCEL_AUTOINSTALL + : !isProduction; return { production: isProduction, outDir: Path.resolve(options.outDir || 'dist'), @@ -134,10 +140,7 @@ class Bundler extends EventEmitter { (options.target === 'electron' ? 'localhost' : ''), detailedReport: options.detailedReport || false, global: options.global, - autoinstall: - typeof options.autoinstall === 'boolean' - ? options.autoinstall - : !isProduction, + autoinstall: autoInstall, scopeHoist: scopeHoist, contentHash: typeof options.contentHash === 'boolean' diff --git a/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/.env b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/.env new file mode 100644 index 00000000000..ae52a618025 --- /dev/null +++ b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/.env @@ -0,0 +1 @@ +PARCEL_AUTOINSTALL=false diff --git a/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/index.js b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/index.js new file mode 100644 index 00000000000..0eb870aa590 --- /dev/null +++ b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/index.js @@ -0,0 +1,4 @@ +const _ = require('lodash'); +module.exports = function() { + return 'hello world'; +} diff --git a/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/package.json b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/package.json new file mode 100644 index 00000000000..70f7cadc900 --- /dev/null +++ b/packages/core/parcel-bundler/test/integration/dont-autoinstall-if-env-var-is-false/package.json @@ -0,0 +1,4 @@ +{ + "name": "dont-autoinstall-if-env-var-is-false", + "dependencies": {} +} diff --git a/packages/core/parcel-bundler/test/javascript.js b/packages/core/parcel-bundler/test/javascript.js index d620fc634f6..3daa2805a93 100644 --- a/packages/core/parcel-bundler/test/javascript.js +++ b/packages/core/parcel-bundler/test/javascript.js @@ -42,6 +42,26 @@ describe('javascript', function() { assert.equal(output.default(), 3); }); + it('should not autoinstall if PARCEL_AUTOINSTALL is set to false', async function() { + const inputDir = path.join( + __dirname, + '/integration/dont-autoinstall-if-env-var-is-false/' + ); + try { + let a = await bundle(path.resolve(inputDir, './index.js')); + await run(a); + } catch (e) { + let pkg = await fs.readFile( + path.resolve(inputDir, 'package.json'), + 'utf8' + ); + const pkgName = 'lodash'; + pkg = JSON.parse(pkg); + assert(pkgName in pkg.dependencies === false); + assert(e.message.includes("Cannot resolve dependency 'lodash'")); + } + }); + it('should auto install babel-core v6', async function() { let originalPkg = await fs.readFile( __dirname + '/integration/babel-6-autoinstall/package.json'