Skip to content

Commit e4aaacc

Browse files
cardosowjhsf
andauthored
fix(rollup-plugin): add default modules back (#5234)
* fix(rollup-plugin): resolve default modules * test(rollup-plugin): fix lwc-modules test * test(rollup-plugin): add test for overriding defaultModules * chore: improve test descriptions * Update packages/@lwc/rollup-plugin/src/index.ts Co-authored-by: Will Harney <[email protected]> --------- Co-authored-by: Will Harney <[email protected]>
1 parent bdc65bd commit e4aaacc

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

packages/@lwc/rollup-plugin/src/__tests__/resolver/resolver.spec.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ import { describe, it, expect } from 'vitest';
1010
import { rollup, type RollupLog, type Plugin, type RollupBuild } from 'rollup';
1111
import nodeResolve from '@rollup/plugin-node-resolve';
1212

13-
import lwc from '../../index';
13+
import lwc, { type RollupLwcOptions } from '../../index';
1414

1515
const fixturesdir = path.resolve(__dirname, 'fixtures');
1616

1717
async function runRollup(
1818
pathname: string,
19-
{ plugins = [] as Plugin[] } = {}
19+
{
20+
plugins = [] as Plugin[],
21+
external = ['lwc', '@lwc/synthetic-shadow', '@lwc/wire-service'],
22+
options = undefined as RollupLwcOptions | undefined,
23+
} = {}
2024
): Promise<{ bundle: RollupBuild; warnings: RollupLog[] }> {
2125
const warnings: RollupLog[] = [];
2226

2327
const bundle = await rollup({
2428
input: path.resolve(fixturesdir, pathname),
25-
plugins: [lwc(), ...plugins],
26-
external: ['lwc', '@lwc/synthetic-shadow', '@lwc/wire-service'],
29+
plugins: [lwc(options), ...plugins],
30+
external,
2731
onwarn(warning) {
2832
warnings.push(warning);
2933
},
@@ -36,9 +40,19 @@ async function runRollup(
3640
}
3741

3842
describe('resolver', () => {
39-
it('should be capable to resolve all the base LWC module imports', async () => {
40-
const { warnings } = await runRollup('lwc-modules/lwc-modules.js');
43+
it('should be capable to resolve all the base LWC module imports without @rollup/plugin-node-resolve', async () => {
44+
const { warnings } = await runRollup('lwc-modules/lwc-modules.js', { external: [] });
45+
expect(warnings).toHaveLength(0);
46+
});
4147

48+
it('should be capable to resolve all the base LWC modules using @rollup/plugin-node-resolve', async () => {
49+
const { warnings } = await runRollup('lwc-modules/lwc-modules.js', {
50+
external: [],
51+
plugins: [nodeResolve()],
52+
options: {
53+
defaultModules: [],
54+
},
55+
});
4256
expect(warnings).toHaveLength(0);
4357
});
4458

@@ -75,7 +89,7 @@ describe('resolver', () => {
7589
});
7690
});
7791

78-
it('should properly resolve modules with @rollup/rollup-node-resolve and third-party package', async () => {
92+
it('should properly resolve modules with @rollup/plugin-node-resolve and third-party package', async () => {
7993
const { warnings } = await runRollup('third-party-import/src/main.js', {
8094
plugins: [nodeResolve()],
8195
});

packages/@lwc/rollup-plugin/src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface RollupLwcOptions {
3434
sourcemap?: boolean | 'inline';
3535
/** The [module resolution](https://lwc.dev/guide/es_modules#module-resolution) overrides passed to the `@lwc/module-resolver`. */
3636
modules?: ModuleRecord[];
37+
/**
38+
* Default modules passed to the `@lwc/module-resolver`.
39+
* If unspecified, defaults to `["@lwc/engine-dom", "@lwc/synthetic-shadow", "@lwc/wire-service"]`.
40+
*/
41+
defaultModules?: ModuleRecord[];
3742
/** The stylesheet compiler configuration to pass to the `@lwc/style-compiler` */
3843
stylesheetConfig?: StylesheetConfig;
3944
/** The configuration to pass to the `@lwc/template-compiler`. */
@@ -69,6 +74,12 @@ const IMPLICIT_DEFAULT_CSS_PATH = '@lwc/resources/empty_css.css';
6974
const EMPTY_IMPLICIT_CSS_CONTENT = '';
7075
const SCRIPT_FILE_EXTENSIONS = ['.js', '.mjs', '.jsx', '.ts', '.mts', '.tsx'];
7176

77+
const DEFAULT_MODULES = [
78+
{ npm: '@lwc/engine-dom' },
79+
{ npm: '@lwc/synthetic-shadow' },
80+
{ npm: '@lwc/wire-service' },
81+
];
82+
7283
function isImplicitHTMLImport(importee: string, importer: string, importerExt: string): boolean {
7384
return (
7485
SCRIPT_FILE_EXTENSIONS.includes(importerExt) &&
@@ -158,6 +169,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
158169
const filter = pluginUtils.createFilter(pluginOptions.include, pluginOptions.exclude);
159170

160171
let { rootDir, modules = [] } = pluginOptions;
172+
161173
const {
162174
targetSSR,
163175
ssrMode,
@@ -172,6 +184,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
172184
experimentalComplexExpressions,
173185
disableSyntheticShadowSupport,
174186
apiVersion,
187+
defaultModules = DEFAULT_MODULES,
175188
} = pluginOptions;
176189

177190
return {
@@ -199,7 +212,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
199212
rootDir = path.resolve(rootDir);
200213
}
201214

202-
modules = [...modules, { dir: rootDir }];
215+
modules = [...modules, ...defaultModules, { dir: rootDir }];
203216
},
204217

205218
resolveId(importee, importer) {

0 commit comments

Comments
 (0)