Skip to content

Commit

Permalink
fix: mark createRequire as pure function
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 2, 2022
1 parent 392096c commit 0d2c6bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/rollup-plugins/create-require.ts
Expand Up @@ -43,7 +43,7 @@ export const createRequire = (): Plugin => ({
export default (
${isEsmVariableName}
? createRequire(import.meta.url)
? /* @__PURE__ */ createRequire(import.meta.url)
: require
);
`;
Expand Down
21 changes: 21 additions & 0 deletions tests/specs/builds/output-module.ts
Expand Up @@ -116,5 +116,26 @@ export default testSuite(({ describe }, nodePath: string) => {

await fixture.cleanup();
});

test('require() & createRequire gets completely removed on conditional', async () => {
const fixture = await createFixture('./tests/fixture-package');

await fixture.writeJson('package.json', {
main: './dist/conditional-require.mjs',
});

const pkgrollProcess = await pkgroll(['--env.NODE_ENV=development', '--minify'], { cwd: fixture.path, nodePath });

expect(pkgrollProcess.exitCode).toBe(0);
expect(pkgrollProcess.stderr).toBe('');

const content = await fixture.readFile('dist/conditional-require.mjs', 'utf8');
expect(content).not.toMatch('\tconsole.log(\'side effect\');');

const [, createRequireMangledVariable] = content.toString().match(/createRequire as (\w+)/)!;
expect(content).not.toMatch(`${createRequireMangledVariable}(`);

await fixture.cleanup();
});
});
});

0 comments on commit 0d2c6bb

Please sign in to comment.