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

Fix process.env import skewing env inlining offsets #819

Merged
merged 3 commits into from
Aug 30, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-forks-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wmr": patch
---

Fix complex `process.env` usage (ex: `let {A}=process.env`, but not `process.env.A`) generating invalid code.
15 changes: 6 additions & 9 deletions packages/wmr/src/plugins/process-global-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,22 @@ export default function processGlobalPlugin({ NODE_ENV = 'development', env = {}
});

code = result.code;

const s = new MagicString(code);

// if that wasn't the only way `process.env` was referenced...
if (code.match(/[^a-zA-Z0-9]process\.env/)) {
// hack: avoid injecting imports into commonjs modules
if (/^\s*(import|export)[\s{]/gm.test(code)) {
s.prepend(`import process from '${PREFIX}process.js';\n`);
code = `import process from '${PREFIX}process.js';${code}`;
} else {
s.prepend(`var process=${processObj};\n`);
code = `var process=${processObj};${code}`;
}
}

const reg = /typeof(\s+|\s*\(+\s*)process([^a-zA-Z$_])/g;
let match = null;
while ((match = reg.exec(code)) !== null) {
s.overwrite(match.index, match.index + match[0].length, `typeof${match[1]}undefined${match[2]}`);
} else {
const reg = /typeof(\s+|\s*\(+\s*)process([^a-zA-Z$_.])/g;
let match = null;
while ((match = reg.exec(code)) !== null) {
s.overwrite(match.index, match.index + match[0].length, `typeof${match[1]}undefined${match[2]}`);
}
}

/** @type {*} */
Expand Down
29 changes: 29 additions & 0 deletions packages/wmr/test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,35 @@ describe('fixtures', () => {
expect(output).toMatch(/false/i);
});
});

it('should handle complex / dynamic process.env usage', async () => {
await loadFixture('process-complex', env);
instance = await runWmrFast(env.tmp.path, {
env: {
WMR_A: 'wmr-a',
WMR_B: 'wmr-b',
WMR_C: 'wmr-c'
}
});
await getOutput(env, instance);
const result = JSON.parse((await env.page.$eval('#out', node => node.textContent)) || 'undefined');
expect(result).toEqual({
WMR_A: 'wmr-a',
WMR_B: 'wmr-b',
NODE_ENV: 'development',
keys: ['WMR_A', 'WMR_B', 'WMR_C', 'NODE_ENV'],
withFullAccess: {
type: 'object',
typeofEnv: 'object',
typeofWMR_A: 'string'
},
withoutFullAccess: {
type: 'object',
typeofEnv: 'object',
typeofWMR_A: 'string'
}
});
});
});

describe('import.meta.env', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/wmr/test/fixtures/process-complex/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre id="out"></pre>
<script src="./index.js" type="module"></script>
21 changes: 21 additions & 0 deletions packages/wmr/test/fixtures/process-complex/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import withoutFullAccess from './other.js';

const { WMR_A, WMR_B } = process.env;
const NODE_ENV = process.env.NODE_ENV;
const keys = Object.keys(process.env);
const type = typeof process;
const typeofEnv = typeof process.env;
const typeofWMR_A = typeof process.env.WMR_A;

document.getElementById('out').textContent = JSON.stringify({
WMR_A,
WMR_B,
NODE_ENV,
keys,
withFullAccess: {
type,
typeofEnv,
typeofWMR_A
},
withoutFullAccess
});
4 changes: 4 additions & 0 deletions packages/wmr/test/fixtures/process-complex/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const type = typeof process;
const typeofEnv = typeof process.env;
const typeofWMR_A = typeof process.env.WMR_A;
export default { type, typeofEnv, typeofWMR_A };