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(esm-shim): pattern matching #1560

Merged
merged 4 commits into from
Aug 26, 2023
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
30 changes: 16 additions & 14 deletions packages/esm-shim/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ function matchAllPolyfill(input: string, pattern: string | RegExp): RegExpMatchA
return [];
}

let idx = 0;
for (let i = 0; i < result.length; i++) {
output.push(result[i].match(new RegExp(pattern)) || []);
const match: RegExpMatchArray = [result[i]];
match.index = idx;
idx += result[i].length;
output.push(match);
}
return output;
}

export function matchAll(regex: RegExp, input: string, addition: Record<string, any>) {
const matches = [];
for (const match of matchAllPolyfill(input, regex)) {
matches.push({
...addition,
...match.groups,
code: match[0],
start: match.index,
end: (match.index || 0) + match[0].length
});
function findPositionToInsertShim(input: string, pattern: RegExp) {
let lastImport;
// mimicking behavior of `String.matchAll` as it returns an iterator, not an array
for (const match of matchAllPolyfill(input, pattern)) {
lastImport = match;
}
return matches;
if (!lastImport) {
return 0;
}

return (lastImport.index || 0) + lastImport[0].length;
}

export function provideCJSSyntax(code: string): Output | null {
if (code.includes(ESMShim) || !CJSyntaxRegex.test(code)) {
return null;
}

const lastESMImport = matchAll(ESMStaticImportRegex, code, { type: 'static' }).pop();
const indexToAppend = lastESMImport ? lastESMImport.end : 0;
const indexToAppend = findPositionToInsertShim(code, ESMStaticImportRegex);
const s = new MagicString(code);
s.appendRight(indexToAppend, ESMShim);

Expand Down
10 changes: 10 additions & 0 deletions packages/esm-shim/test/fixtures/cjs-multiple-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { constants } from 'node:crypto';

import MagicString from 'magic-string';

const child = require('child');

const s = new MagicString('');
const c = constants.SEP;

export { child, s, c };
7 changes: 7 additions & 0 deletions packages/esm-shim/test/fixtures/cjs-single-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import MagicString from 'magic-string';

const child = require('child');

const s = new MagicString('');

export { child, s };
44 changes: 44 additions & 0 deletions packages/esm-shim/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,47 @@ Generated by [AVA](https://avajs.dev).
exports.child = child;␊
`

## inject cjs shim for esm output with a single import statement

> Snapshot 1

`import MagicString from 'magic-string';␊
// -- Shims --␊
import cjsUrl from 'url';␊
import cjsPath from 'path';␊
import cjsModule from 'module';␊
const __filename = cjsUrl.fileURLToPath(import.meta.url);␊
const __dirname = cjsPath.dirname(__filename);␊
const require = cjsModule.createRequire(import.meta.url);␊
const child = require('child');␊
const s = new MagicString('');␊
export { child, s };␊
`

## inject cjs shim for esm output with multiple import statements

> Snapshot 1

`import { constants } from 'node:crypto';␊
import MagicString from 'magic-string';␊
// -- Shims --␊
import cjsUrl from 'url';␊
import cjsPath from 'path';␊
import cjsModule from 'module';␊
const __filename = cjsUrl.fileURLToPath(import.meta.url);␊
const __dirname = cjsPath.dirname(__filename);␊
const require = cjsModule.createRequire(import.meta.url);␊
const child = require('child');␊
const s = new MagicString('');␊
const c = constants.SEP;␊
export { c, child, s };␊
`
Binary file modified packages/esm-shim/test/snapshots/test.js.snap
Binary file not shown.
26 changes: 26 additions & 0 deletions packages/esm-shim/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,29 @@ test.serial('not inject cjs shim for cjs output', async (t) => {
t.snapshot(output.code);
t.falsy(output.map);
});

test.serial('inject cjs shim for esm output with a single import statement', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/cjs-single-import.js',
plugins: [esmShim()],
external: ['magic-string']
});
const result = await bundle.generate({ format: 'es' });
t.is(result.output.length, 1);
const [output] = result.output;
t.snapshot(output.code);
t.falsy(output.map);
});

test.serial('inject cjs shim for esm output with multiple import statements', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/cjs-multiple-imports.js',
plugins: [esmShim()],
external: ['magic-string', 'node:crypto']
});
const result = await bundle.generate({ format: 'es' });
t.is(result.output.length, 1);
const [output] = result.output;
t.snapshot(output.code);
t.falsy(output.map);
});
Loading