Skip to content

Commit b8c19fd

Browse files
committed
test(rollup): validate preserved import dedupe
1 parent 183ce42 commit b8c19fd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { LightningElement } from 'lwc';
2+
import { renderer as a } from 'lwc';
3+
import { renderer as b } from 'lwc';
4+
5+
export default class extends LightningElement {
6+
prop = [a, b];
7+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2022, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
import path from 'node:path';
8+
import { it, expect } from 'vitest';
9+
import { rollup } from 'rollup';
10+
11+
import lwc from '../../index';
12+
import type { RollupLwcOptions } from '../../index';
13+
import type { RollupLog } from 'rollup';
14+
15+
async function runRollup(
16+
pathname: string,
17+
options: RollupLwcOptions
18+
): Promise<{ code: string; warnings: RollupLog[] }> {
19+
const warnings: RollupLog[] = [];
20+
const bundle = await rollup({
21+
input: path.resolve(import.meta.dirname, pathname),
22+
plugins: [lwc(options)],
23+
external: ['lwc'],
24+
onwarn(warning) {
25+
warnings.push(warning);
26+
},
27+
});
28+
29+
const { output } = await bundle.generate({
30+
format: 'esm',
31+
});
32+
33+
return {
34+
code: output[0].code,
35+
warnings,
36+
};
37+
}
38+
39+
// This test validates the behavior of rollup because import deduping was handled by
40+
// @lwc/babel-plugin-component prior to v9 and we want to ensure there are no regressions
41+
it('merges duplicate imports', async () => {
42+
const { code, warnings } = await runRollup('fixtures/basic/basic.js', {});
43+
const lwcImport = /import \{ ([^}]+?) \} from 'lwc'/g;
44+
const imports = Array.from(code.matchAll(lwcImport));
45+
expect(warnings).toHaveLength(0);
46+
expect(imports).toHaveLength(1);
47+
const specifiers = imports[0][1];
48+
expect(specifiers).toContain('renderer');
49+
expect(specifiers).not.toContain('renderer as');
50+
});

0 commit comments

Comments
 (0)