Skip to content

Commit

Permalink
fix(rollup-plugin): add default modules back (#5234)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
cardoso and wjhsf authored Feb 26, 2025
1 parent bdc65bd commit e4aaacc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import { describe, it, expect } from 'vitest';
import { rollup, type RollupLog, type Plugin, type RollupBuild } from 'rollup';
import nodeResolve from '@rollup/plugin-node-resolve';

import lwc from '../../index';
import lwc, { type RollupLwcOptions } from '../../index';

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

async function runRollup(
pathname: string,
{ plugins = [] as Plugin[] } = {}
{
plugins = [] as Plugin[],
external = ['lwc', '@lwc/synthetic-shadow', '@lwc/wire-service'],
options = undefined as RollupLwcOptions | undefined,
} = {}
): Promise<{ bundle: RollupBuild; warnings: RollupLog[] }> {
const warnings: RollupLog[] = [];

const bundle = await rollup({
input: path.resolve(fixturesdir, pathname),
plugins: [lwc(), ...plugins],
external: ['lwc', '@lwc/synthetic-shadow', '@lwc/wire-service'],
plugins: [lwc(options), ...plugins],
external,
onwarn(warning) {
warnings.push(warning);
},
Expand All @@ -36,9 +40,19 @@ async function runRollup(
}

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

it('should be capable to resolve all the base LWC modules using @rollup/plugin-node-resolve', async () => {
const { warnings } = await runRollup('lwc-modules/lwc-modules.js', {
external: [],
plugins: [nodeResolve()],
options: {
defaultModules: [],
},
});
expect(warnings).toHaveLength(0);
});

Expand Down Expand Up @@ -75,7 +89,7 @@ describe('resolver', () => {
});
});

it('should properly resolve modules with @rollup/rollup-node-resolve and third-party package', async () => {
it('should properly resolve modules with @rollup/plugin-node-resolve and third-party package', async () => {
const { warnings } = await runRollup('third-party-import/src/main.js', {
plugins: [nodeResolve()],
});
Expand Down
15 changes: 14 additions & 1 deletion packages/@lwc/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export interface RollupLwcOptions {
sourcemap?: boolean | 'inline';
/** The [module resolution](https://lwc.dev/guide/es_modules#module-resolution) overrides passed to the `@lwc/module-resolver`. */
modules?: ModuleRecord[];
/**
* Default modules passed to the `@lwc/module-resolver`.
* If unspecified, defaults to `["@lwc/engine-dom", "@lwc/synthetic-shadow", "@lwc/wire-service"]`.
*/
defaultModules?: ModuleRecord[];
/** The stylesheet compiler configuration to pass to the `@lwc/style-compiler` */
stylesheetConfig?: StylesheetConfig;
/** The configuration to pass to the `@lwc/template-compiler`. */
Expand Down Expand Up @@ -69,6 +74,12 @@ const IMPLICIT_DEFAULT_CSS_PATH = '@lwc/resources/empty_css.css';
const EMPTY_IMPLICIT_CSS_CONTENT = '';
const SCRIPT_FILE_EXTENSIONS = ['.js', '.mjs', '.jsx', '.ts', '.mts', '.tsx'];

const DEFAULT_MODULES = [
{ npm: '@lwc/engine-dom' },
{ npm: '@lwc/synthetic-shadow' },
{ npm: '@lwc/wire-service' },
];

function isImplicitHTMLImport(importee: string, importer: string, importerExt: string): boolean {
return (
SCRIPT_FILE_EXTENSIONS.includes(importerExt) &&
Expand Down Expand Up @@ -158,6 +169,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
const filter = pluginUtils.createFilter(pluginOptions.include, pluginOptions.exclude);

let { rootDir, modules = [] } = pluginOptions;

const {
targetSSR,
ssrMode,
Expand All @@ -172,6 +184,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
experimentalComplexExpressions,
disableSyntheticShadowSupport,
apiVersion,
defaultModules = DEFAULT_MODULES,
} = pluginOptions;

return {
Expand Down Expand Up @@ -199,7 +212,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
rootDir = path.resolve(rootDir);
}

modules = [...modules, { dir: rootDir }];
modules = [...modules, ...defaultModules, { dir: rootDir }];
},

resolveId(importee, importer) {
Expand Down

0 comments on commit e4aaacc

Please sign in to comment.