Skip to content

Commit 8046cf6

Browse files
committed
feat: remove traverse
1 parent 388c2e8 commit 8046cf6

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/index.ts

+20-44
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,42 @@ import hash from 'hash-sum';
33
import * as path from 'path';
44
import * as loaderUtils from 'loader-utils';
55
import * as t from '@babel/types';
6-
import traverse from '@babel/traverse';
7-
import { File } from '@babel/types'
86
import { parse } from '@babel/parser';
97
import { isDefineComponentCall, parseComponentDecls } from './utils';
108

11-
const hasJSX = (file: File) => {
12-
let fileHasJSX = false;
13-
traverse(file, {
14-
JSXElement(path) {
15-
fileHasJSX = true;
16-
path.stop();
17-
},
18-
JSXFragment(path) {
19-
fileHasJSX = true;
20-
path.stop();
21-
},
22-
});
23-
24-
return fileHasJSX;
25-
};
26-
279
export default function loader(
2810
this: webpack.loader.LoaderContext,
2911
source: string,
3012
) {
3113
const loaderContext = this;
3214
loaderContext.cacheable?.();
3315

34-
const isDev = loaderContext.mode === 'development';
35-
36-
if (!isDev) {
16+
if (!(loaderContext.mode === 'development')) {
3717
return source;
3818
}
3919

20+
const webpackRemainingChain = loaderUtils.getRemainingRequest(loaderContext).split('!');
21+
const fullPath = webpackRemainingChain[webpackRemainingChain.length - 1];
22+
const filename = path.relative(process.cwd(), fullPath);
23+
4024
const file = parse(source, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
4125

42-
if (!hasJSX(file)) {
26+
if (!(filename.endsWith('.jsx') || filename.endsWith('.tsx'))) {
4327
return source;
4428
}
4529

46-
const webpackRemainingChain = loaderUtils.getRemainingRequest(loaderContext).split('!');
47-
const fullPath = webpackRemainingChain[webpackRemainingChain.length - 1];
48-
const filename = path.relative(process.cwd(), fullPath);
49-
5030
const declaredComponents: { name: string }[] = [];
5131
const hotComponents: {
5232
local: string;
5333
id: string;
5434
}[] = [];
55-
let defaultIdentifier: t.Identifier | null = null;
35+
let hasDefault = false;
5636

57-
traverse(file, {
58-
VariableDeclaration(nodePath) {
59-
declaredComponents.push(...parseComponentDecls(nodePath.node));
60-
},
61-
ExportNamedDeclaration(nodePath) {
62-
const { specifiers = [], declaration } = nodePath.node;
37+
for (const node of file.program.body) {
38+
if (t.isVariableDeclaration(node)) {
39+
declaredComponents.push(...parseComponentDecls(node));
40+
} else if (t.isExportNamedDeclaration(node)) {
41+
const { specifiers = [], declaration } = node;
6342
if (t.isVariableDeclaration(declaration)) {
6443
hotComponents.push(...parseComponentDecls(declaration).map(({ name }) => ({
6544
local: name,
@@ -77,33 +56,30 @@ export default function loader(
7756
}
7857
}
7958
}
80-
},
81-
ExportDefaultDeclaration(nodePath) {
82-
const { declaration } = nodePath.node;
59+
} else if (t.isExportDefaultDeclaration(node)) {
60+
const { declaration } = node;
8361
if (t.isIdentifier(declaration)) {
8462
if (declaredComponents.find(d => d.name === declaration.name)) {
8563
hotComponents.push({
86-
local: declaration.name,
64+
local: '__default__',
8765
id: hash(`${filename}-default`)
8866
})
8967
}
9068
} else if (isDefineComponentCall(declaration)) {
91-
defaultIdentifier = nodePath.scope.generateUidIdentifier('default')
9269
hotComponents.push({
93-
local: defaultIdentifier.name,
70+
local: '__default__',
9471
id: hash(`${filename}-default`)
9572
});
9673
}
9774
}
98-
});
75+
}
9976

10077
if (hotComponents.length) {
101-
if (defaultIdentifier) {
102-
const { name } = defaultIdentifier as t.Identifier;
78+
if (hasDefault) {
10379
source = source.replace(
10480
/export default defineComponent/g,
105-
`const ${name} = defineComponent`
106-
) + `\nexport default ${name}`
81+
`const __default__ = defineComponent`
82+
) + `\nexport default __default__`
10783
}
10884

10985
let callbackCode = '';

0 commit comments

Comments
 (0)