Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rollup-plugin): allow any implicit HTML import from any kind of script file @W-15941938 #4261

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@lwc/compiler/src/transformers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ function transformFile(
case '.jsx':
case '.ts':
case '.js':
case '.mts':
case '.mjs':
transformer = options.targetSSR ? compileComponentForSSR : scriptTransformer;
break;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"modules": [
{ "dir" : "src/modules" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "inherited-template-tests",
"private": true,
"version": "0.0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Extension from 'x/ext-js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
all your base
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Base from "x/base";

export default class extends Base {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Base from "x/base";

export default class extends Base {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Extension from 'x/ext-ts'
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,32 @@ describe('resolver', () => {
// Alias name
expect(code).not.toContain(`sel: "foo-bar"`);
});

it('should resolve inherited template for JavaScript component [#4233]', async () => {
const bundle = await rollup({
input: path.resolve(__dirname, 'fixtures/inherited-templates/src/javascript.js'),
plugins: [lwc()],
});

const result = await bundle.generate({
format: 'esm',
});
const { code } = result.output[0];

expect(code).toContain('all your base');
});

it('should resolve inherited template for TypeScript component [#4233]', async () => {
const bundle = await rollup({
input: path.resolve(__dirname, 'fixtures/inherited-templates/src/typescript.ts'),
plugins: [lwc()],
});

const result = await bundle.generate({
format: 'esm',
});
const { code } = result.output[0];

expect(code).toContain('all your base');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they belong to us?

});
});
13 changes: 9 additions & 4 deletions packages/@lwc/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ const IMPLICIT_DEFAULT_HTML_PATH = '@lwc/resources/empty_html.js';
const EMPTY_IMPLICIT_HTML_CONTENT = 'export default void 0';
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'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set / has may be faster, but maybe not since this is a tiny list anyway.


function isImplicitHTMLImport(importee: string, importer: string): boolean {
function isImplicitHTMLImport(importee: string, importer: string, importerExt: string): boolean {
return (
path.extname(importer) === '.js' &&
SCRIPT_FILE_EXTENSIONS.includes(importerExt) &&
path.extname(importee) === '.html' &&
path.dirname(importer) === path.dirname(importee) &&
path.basename(importer, '.js') === path.basename(importee, '.html')
path.basename(importer, importerExt) === path.basename(importee, '.html')
);
}

Expand Down Expand Up @@ -227,7 +228,11 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
parseDescriptorFromFilePath(importeeAbsPath);

if (
isImplicitHTMLImport(importeeNormalizedFilename, importerFilename) &&
isImplicitHTMLImport(
importeeNormalizedFilename,
importerFilename,
importerExt
) &&
!fs.existsSync(importeeNormalizedFilename)
) {
return IMPLICIT_DEFAULT_HTML_PATH;
Expand Down
Loading