Skip to content

Commit 8d88d62

Browse files
committed
feat: narrow target files in lazy vite plugin
1 parent 978933f commit 8d88d62

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/start/src/config/lazy.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,37 @@ const importTransform = (): PluginItem => {
4343
};
4444
};
4545

46+
const fileEndingRegex = /(ts|js)x(\?.*)?$/;
47+
4648
const lazy = (): PluginOption => {
4749
const cwd = process.cwd().replaceAll(osSep, sep);
4850
return {
4951
name: "solid-lazy-css",
5052
enforce: "pre",
5153
async transform(src, id) {
52-
// TODO: Try to exclude files by dynamicImport info in moduleGraph
53-
54-
if (!id.match(/(ts|js)x(\?.*)?$/)) return;
54+
if (!id.match(fileEndingRegex)) return;
5555

5656
// The transformed files either import "lazy" or css files
5757
// Therefore we skip, if the src doesn't have any import
5858
if (src.indexOf("import") === -1) return;
5959

60-
const hasLazy = src.indexOf("lazy") !== -1;
61-
const localId = relative(cwd, id);
60+
const plugins: PluginItem[] = [];
61+
62+
const hasDefaultExport = src.indexOf("export default") !== -1;
63+
if (hasDefaultExport) {
64+
const localId = relative(cwd, id);
65+
plugins.push(idTransform(localId));
66+
}
67+
68+
const hasLazy = src.indexOf("lazy(") !== -1;
69+
if (hasLazy) plugins.push(importTransform());
70+
71+
if (!plugins.length) {
72+
return;
73+
}
6274

6375
const transformed = await babel.transformAsync(src, {
64-
plugins:
65-
hasLazy
66-
? [idTransform(localId), importTransform()]
67-
: [idTransform(localId)],
76+
plugins,
6877
parserOpts: {
6978
plugins: ["jsx", "typescript"]
7079
},

0 commit comments

Comments
 (0)