Skip to content

Commit 3d355af

Browse files
fix: suppress unresolvable dynamic import errors (#105)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent e1b3036 commit 3d355af

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/utils/get-rollup-configs.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ const getConfig = {
138138
json(),
139139
esbuildTransform(esbuildConfig),
140140
createRequire(),
141-
dynamicImportVars(),
141+
dynamicImportVars({
142+
warnOnError: true,
143+
}),
142144
...(
143145
options.minify
144146
? [esbuildMinify(esbuildConfig)]

tests/fixtures.ts

+18
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,21 @@ export const fixtureDynamicImports: FileTree = {
257257
},
258258
},
259259
};
260+
261+
// https://github.com/privatenumber/pkgroll/issues/104
262+
export const fixtureDynamicImportUnresolvable: FileTree = {
263+
'package.json': createPackageJson({
264+
main: './dist/dynamic-imports.js',
265+
}),
266+
src: {
267+
'dynamic-imports.js': outdent`
268+
function importModule(path) {
269+
// who knows what will be imported here?
270+
return import(path);
271+
}
272+
273+
importModule('./too-dynamic.js');
274+
`,
275+
'too-dynamic.js': 'console.log(123)',
276+
},
277+
};

tests/specs/builds/output-commonjs.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createFixture } from 'fs-fixture';
44
import { pkgroll } from '../../utils.js';
55
import {
66
packageFixture, createPackageJson, createTsconfigJson, fixtureDynamicImports,
7+
fixtureDynamicImportUnresolvable,
78
} from '../../fixtures.js';
89

910
export default testSuite(({ describe }, nodePath: string) => {
@@ -204,5 +205,26 @@ export default testSuite(({ describe }, nodePath: string) => {
204205
expect(files[1]).toMatch(/^bbb-/);
205206
expect(files[2]).toMatch(/^ccc-/);
206207
});
208+
209+
// https://github.com/privatenumber/pkgroll/issues/104
210+
test('unresolvable dynamic import should not fail', async () => {
211+
await using fixture = await createFixture({
212+
...fixtureDynamicImportUnresolvable,
213+
'package.json': createPackageJson({
214+
exports: './dist/dynamic-imports.cjs',
215+
}),
216+
});
217+
218+
const pkgrollProcess = await pkgroll([], {
219+
cwd: fixture.path,
220+
nodePath,
221+
});
222+
223+
expect(pkgrollProcess.exitCode).toBe(0);
224+
expect(pkgrollProcess.stderr).toContain('[plugin rollup-plugin-dynamic-import-variables]');
225+
226+
const content = await fixture.readFile('dist/dynamic-imports.cjs', 'utf8');
227+
expect(content).toMatch('import(');
228+
});
207229
});
208230
});

tests/specs/builds/output-module.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createFixture } from 'fs-fixture';
44
import { pkgroll } from '../../utils.js';
55
import {
66
packageFixture, createPackageJson, createTsconfigJson, fixtureDynamicImports,
7+
fixtureDynamicImportUnresolvable,
78
} from '../../fixtures.js';
89

910
export default testSuite(({ describe }, nodePath: string) => {
@@ -272,5 +273,26 @@ export default testSuite(({ describe }, nodePath: string) => {
272273
expect(files[1]).toMatch(/^bbb-/);
273274
expect(files[2]).toMatch(/^ccc-/);
274275
});
276+
277+
// https://github.com/privatenumber/pkgroll/issues/104
278+
test('unresolvable dynamic import should not fail', async () => {
279+
await using fixture = await createFixture({
280+
...fixtureDynamicImportUnresolvable,
281+
'package.json': createPackageJson({
282+
exports: './dist/dynamic-imports.mjs',
283+
}),
284+
});
285+
286+
const pkgrollProcess = await pkgroll([], {
287+
cwd: fixture.path,
288+
nodePath,
289+
});
290+
291+
expect(pkgrollProcess.exitCode).toBe(0);
292+
expect(pkgrollProcess.stderr).toContain('[plugin rollup-plugin-dynamic-import-variables]');
293+
294+
const content = await fixture.readFile('dist/dynamic-imports.mjs', 'utf8');
295+
expect(content).toMatch('import(');
296+
});
275297
});
276298
});

0 commit comments

Comments
 (0)