Skip to content

Commit

Permalink
feat: support dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Dec 24, 2024
1 parent 18e525b commit e1b3036
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-dynamic-import-vars": "^2.1.5",
"@rollup/plugin-inject": "^5.0.5",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.0",
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/utils/get-rollup-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import type { PackageJson } from 'type-fest';
import type { TsConfigResult } from 'get-tsconfig';
import type { ExportEntry, AliasMap } from '../types.js';
Expand Down Expand Up @@ -137,6 +138,7 @@ const getConfig = {
json(),
esbuildTransform(esbuildConfig),
createRequire(),
dynamicImportVars(),
...(
options.minify
? [esbuildMinify(esbuildConfig)]
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,26 @@ export const fixtureDependencyImportsMap: FileTree = {
}),
},
};

export const fixtureDynamicImports: FileTree = {
'package.json': createPackageJson({
main: './dist/dynamic-imports.js',
}),
src: {
'dynamic-imports.js': outdent`
const files = [
'aaa',
'bbb',
'ccc',
];
const randomFile = files[Math.floor(Math.random() * files.length)];
import(\`./files/\${randomFile}.js\`)
`,

files: {
'aaa.js': 'console.log(111)',
'bbb.js': 'console.log(222)',
'ccc.js': 'console.log(333)',
},
},
};
31 changes: 30 additions & 1 deletion tests/specs/builds/output-commonjs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'node:fs/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { pkgroll } from '../../utils.js';
import { packageFixture, createPackageJson, createTsconfigJson } from '../../fixtures.js';
import {
packageFixture, createPackageJson, createTsconfigJson, fixtureDynamicImports,
} from '../../fixtures.js';

export default testSuite(({ describe }, nodePath: string) => {
describe('output: commonjs', ({ test }) => {
Expand Down Expand Up @@ -175,5 +178,31 @@ export default testSuite(({ describe }, nodePath: string) => {
const content = await fixture.readFile('dist/nested/index.js', 'utf8');
expect(content).toMatch('nested entry point');
});

test('dynamic imports', async () => {
await using fixture = await createFixture({
...fixtureDynamicImports,
'package.json': createPackageJson({
exports: './dist/dynamic-imports.cjs',
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

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

const content = await fixture.readFile('dist/dynamic-imports.cjs', 'utf8');
expect(content).toMatch('require(');

const files = await fs.readdir(fixture.getPath('dist'));
files.sort();
expect(files[0]).toMatch(/^aaa-/);
expect(files[1]).toMatch(/^bbb-/);
expect(files[2]).toMatch(/^ccc-/);
});
});
});
31 changes: 30 additions & 1 deletion tests/specs/builds/output-module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'node:fs/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { pkgroll } from '../../utils.js';
import { packageFixture, createPackageJson, createTsconfigJson } from '../../fixtures.js';
import {
packageFixture, createPackageJson, createTsconfigJson, fixtureDynamicImports,
} from '../../fixtures.js';

export default testSuite(({ describe }, nodePath: string) => {
describe('output: module', ({ test }) => {
Expand Down Expand Up @@ -243,5 +246,31 @@ export default testSuite(({ describe }, nodePath: string) => {
const [, createRequireMangledVariable] = content.toString().match(/createRequire as (\w+)/)!;
expect(content).not.toMatch(`${createRequireMangledVariable}(`);
});

test('dynamic imports', async () => {
await using fixture = await createFixture({
...fixtureDynamicImports,
'package.json': createPackageJson({
exports: './dist/dynamic-imports.mjs',
}),
});

const pkgrollProcess = await pkgroll([], {
cwd: fixture.path,
nodePath,
});

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

const content = await fixture.readFile('dist/dynamic-imports.mjs', 'utf8');
expect(content).toMatch('import(');

const files = await fs.readdir(fixture.getPath('dist'));
files.sort();
expect(files[0]).toMatch(/^aaa-/);
expect(files[1]).toMatch(/^bbb-/);
expect(files[2]).toMatch(/^ccc-/);
});
});
});

0 comments on commit e1b3036

Please sign in to comment.