diff --git a/packages/core/core/src/resolveOptions.js b/packages/core/core/src/resolveOptions.js index 1ce2e208571..d650aa1d74a 100644 --- a/packages/core/core/src/resolveOptions.js +++ b/packages/core/core/src/resolveOptions.js @@ -49,7 +49,15 @@ export default async function resolveOptions( entries = [path.resolve(inputCwd, initialOptions.entries)]; } - let entryRoot = getRootDir(entries); + let entryRoot = getRootDir( + entries.map(entry => + // if the entry refers entryRootDir, getRootDir returns the parent directory of entryRootDir. + // In this case, each entry should refers some file, but without cli build target entry refers entryRootDir directory. + // So, we add /. to entry to make entry refers some (virtual) file in such case. + path.extname(entry) !== '' ? entry : entry + '/.', + ), + ); + let projectRootFile = (await resolveConfig( inputFS, diff --git a/packages/core/integration-tests/test/integration/env-file-with-package-source/.env b/packages/core/integration-tests/test/integration/env-file-with-package-source/.env new file mode 100644 index 00000000000..3552f1442f9 --- /dev/null +++ b/packages/core/integration-tests/test/integration/env-file-with-package-source/.env @@ -0,0 +1,2 @@ +FOO=bar +BAR=test diff --git a/packages/core/integration-tests/test/integration/env-file-with-package-source/index.js b/packages/core/integration-tests/test/integration/env-file-with-package-source/index.js new file mode 100644 index 00000000000..e81b72112b6 --- /dev/null +++ b/packages/core/integration-tests/test/integration/env-file-with-package-source/index.js @@ -0,0 +1 @@ +module.exports = process.env.FOO + process.env.BAR; diff --git a/packages/core/integration-tests/test/integration/env-file-with-package-source/package.json b/packages/core/integration-tests/test/integration/env-file-with-package-source/package.json new file mode 100644 index 00000000000..2db67e93689 --- /dev/null +++ b/packages/core/integration-tests/test/integration/env-file-with-package-source/package.json @@ -0,0 +1,4 @@ +{ + "name": "env-file-with-package-source", + "source": "index.js" +} \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/env-file-with-package-source/yarn.lock b/packages/core/integration-tests/test/integration/env-file-with-package-source/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/javascript.js b/packages/core/integration-tests/test/javascript.js index dda211e5016..4a2039166aa 100644 --- a/packages/core/integration-tests/test/javascript.js +++ b/packages/core/integration-tests/test/javascript.js @@ -2955,6 +2955,26 @@ describe('javascript', function () { assert.equal(output, 'barbaz'); }); + it('should insert environment variables from a file even if entry file is specified with source value in package.json', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/env-file-with-package-source/index.js', + ), + { + // when the entry file is specified with a source value in package.json and not specified with cli build target value, + // the entry file is resolved to the entry root directory in packages/core/core/src/resolveOptions.js as InitialParcelOptions. + // So, entry root directory is specified as InitialParcelOptions' entries value. + entries: [ + path.join(__dirname, '/integration/env-file-with-package-source'), + ], + }, + ); + + let output = await run(b); + assert.equal(output, 'bartest'); + }); + it('should error on process.env mutations', async function () { let filePath = path.join(__dirname, '/integration/env-mutate/index.js'); await assert.rejects(bundle(filePath), {