-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ssr): remove unused imports (#4873)
- Loading branch information
1 parent
09f0355
commit b47e18c
Showing
5 changed files
with
56 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2024, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
import { produce } from 'immer'; | ||
import { traverse } from 'estree-toolkit'; | ||
import { Visitors } from './transmogrify'; | ||
import type { Program as EsProgram } from 'estree'; | ||
|
||
const visitors: Visitors = { | ||
$: { | ||
scope: true, | ||
}, | ||
ImportDeclaration(path) { | ||
const { node, scope } = path; | ||
if (!node || !scope) { | ||
return; | ||
} | ||
if (node.source.type === 'Literal' && node.source.value === '@lwc/ssr-runtime') { | ||
node.specifiers = node.specifiers.filter( | ||
(specifier) => | ||
// There shouldn't be any default imports anyway | ||
specifier.type === 'ImportSpecifier' && | ||
// If there are references, then the import is used. Note that an import will _always_ | ||
// have a binding (of type "module"), but it may not have references. | ||
scope.getBinding(specifier.local.name)?.references.length | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
/** | ||
* Remove any unused imports from `@lwc/ssr-runtime`. | ||
* This avoids any warnings from Rollup about unused imports, and avoids us needing | ||
* to manually track what's imported during AST generation. | ||
* @param compiledComponentAst | ||
*/ | ||
export function optimizeImports(compiledComponentAst: EsProgram): EsProgram { | ||
return produce(compiledComponentAst, (astDraft) => traverse(astDraft, visitors)); | ||
} |