Skip to content

Commit

Permalink
feat(ssr): add LWC version comment to compiled components (#5186)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf authored Feb 4, 2025
1 parent 4cab9e6 commit 3056753
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/@lwc/ssr-compiler/src/__tests__/compilation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path';
import { describe, test, expect } from 'vitest';
import { CompilerError } from '@lwc/errors';
import { LWC_VERSION_COMMENT_REGEX } from '@lwc/shared';
import { compileComponentForSSR } from '../index';

expect.addSnapshotSerializer({
Expand Down Expand Up @@ -42,6 +43,15 @@ describe('component compilation', () => {
const { code } = compileComponentForSSR(src, filename, {});
expect(code).toContain('import explicit from "./explicit.html"');
});
test('components include LWC version comment', () => {
const src = `
import { LightningElement } from 'lwc';
export default class extends LightningElement {}
`;
const filename = path.resolve('component.js');
const { code } = compileComponentForSSR(src, filename, {});
expect(code).toMatch(LWC_VERSION_COMMENT_REGEX);
});
test('supports .ts file imports', () => {
const src = `
import { LightningElement } from 'lwc';
Expand Down
23 changes: 21 additions & 2 deletions packages/@lwc/ssr-compiler/src/compile-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { traverse, builders as b, is } from 'estree-toolkit';
import { parseModule } from 'meriyah';

import { DecoratorErrors } from '@lwc/errors';
import { LWC_VERSION_COMMENT, type CompilationMode } from '@lwc/shared';
import { transmogrify } from '../transmogrify';
import { ImportManager } from '../imports';
import { replaceLwcImport, replaceNamedLwcExport, replaceAllLwcExport } from './lwc-import';
Expand All @@ -25,9 +26,9 @@ import type {
Identifier as EsIdentifier,
Program as EsProgram,
Decorator as EsDecorator,
Comment as EsComment,
} from 'estree';
import type { Visitors, ComponentMetaState } from './types';
import type { CompilationMode } from '@lwc/shared';

const visitors: Visitors = {
$: { scope: true },
Expand Down Expand Up @@ -89,6 +90,20 @@ const visitors: Visitors = {
node.id = b.identifier('DefaultComponentName');
state.lwcClassName = 'DefaultComponentName';
}

// There's no builder for comment nodes :\
const lwcVersionComment: EsComment = {
type: 'Block',
value: LWC_VERSION_COMMENT,
};

// Add LWC version comment to end of class body
const { body } = node;
if (body.trailingComments) {
body.trailingComments.push(lwcVersionComment);
} else {
body.trailingComments = [lwcVersionComment];
}
}
},
PropertyDefinition(path, state) {
Expand Down Expand Up @@ -324,6 +339,10 @@ export default function compileJS(
}

return {
code: generate(ast, {}),
code: generate(ast, {
// The AST generated by meriyah doesn't seem to include comments,
// so this just preserves the LWC version comment we added
comments: true,
}),
};
}

0 comments on commit 3056753

Please sign in to comment.