|
1 |
| -import webpack from 'webpack'; |
2 |
| -import * as parser from "@babel/parser"; |
3 |
| -import traverse from "@babel/traverse"; |
| 1 | +import * as webpack from 'webpack'; |
| 2 | +import * as hash from 'hash-sum'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as loaderUtils from 'loader-utils'; |
| 5 | +import * as t from '@babel/types'; |
| 6 | +import traverse from '@babel/traverse'; |
| 7 | +import { File } from '@babel/types' |
| 8 | +import { parse } from "@babel/parser"; |
| 9 | +import { isDefineComponentCall, parseComponentDecls } from './utils'; |
| 10 | + |
| 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 | +}; |
4 | 26 |
|
5 | 27 | export default function loader(
|
6 | 28 | this: webpack.loader.LoaderContext,
|
7 |
| - source: string |
8 |
| -): string { |
| 29 | + source: string, |
| 30 | + sourceMap: string |
| 31 | +) { |
9 | 32 | const loaderContext = this;
|
10 |
| - |
11 |
| - return source; |
| 33 | + loaderContext.cacheable?.(); |
| 34 | + const isDev = process.env.NODE_ENV === 'development'; |
| 35 | + |
| 36 | + if (isDev) { |
| 37 | + loaderContext.callback(null, source, sourceMap); |
| 38 | + } |
| 39 | + |
| 40 | + const file = parse(source); |
| 41 | + |
| 42 | + if (!hasJSX(file)) { |
| 43 | + loaderContext.callback(null, source, sourceMap); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const webpackRemainingChain = loaderUtils.getRemainingRequest(loaderContext).split('!'); |
| 48 | + const fullPath = webpackRemainingChain[webpackRemainingChain.length - 1]; |
| 49 | + const filename = path.relative(process.cwd(), fullPath); |
| 50 | + |
| 51 | + const declaredComponents: { name: string }[] = []; |
| 52 | + const hotComponents: { |
| 53 | + local: string; |
| 54 | + exported: string; |
| 55 | + id: string; |
| 56 | + }[] = []; |
| 57 | + let defaultIdentifier: t.Identifier | null = null; |
| 58 | + |
| 59 | + traverse(file, { |
| 60 | + VariableDeclaration(nodePath) { |
| 61 | + declaredComponents.push(...parseComponentDecls(nodePath.node)); |
| 62 | + }, |
| 63 | + ExportNamedDeclaration(nodePath) { |
| 64 | + const { specifiers = [], declaration } = nodePath.node; |
| 65 | + if (t.isVariableDeclaration(declaration)) { |
| 66 | + hotComponents.push(...parseComponentDecls(declaration).map(({ name }) => ({ |
| 67 | + local: name, |
| 68 | + exported: name, |
| 69 | + id: hash(`${filename}-${name}`), |
| 70 | + }))); |
| 71 | + } else if (specifiers.length) { |
| 72 | + for (const spec of specifiers) { |
| 73 | + if (t.isExportSpecifier(spec) && t.isIdentifier(spec.exported)) { |
| 74 | + if (declaredComponents.find(d => d.name === spec.local.name)) { |
| 75 | + hotComponents.push({ |
| 76 | + local: spec.local.name, |
| 77 | + exported: spec.exported.name, |
| 78 | + id: hash(`${filename}-${spec.exported.name}`) |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + ExportDefaultDeclaration(nodePath) { |
| 86 | + const { declaration } = nodePath.node; |
| 87 | + if (t.isIdentifier(declaration)) { |
| 88 | + if (declaredComponents.find(d => d.name === declaration.name)) { |
| 89 | + hotComponents.push({ |
| 90 | + local: declaration.name, |
| 91 | + exported: 'default', |
| 92 | + id: hash(`${filename}-default`) |
| 93 | + }) |
| 94 | + } |
| 95 | + } else if (isDefineComponentCall(declaration)) { |
| 96 | + defaultIdentifier = nodePath.scope.generateUidIdentifier('default') |
| 97 | + hotComponents.push({ |
| 98 | + local: defaultIdentifier.name, |
| 99 | + exported: 'default', |
| 100 | + id: hash(`${filename}-default`) |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + if (hotComponents.length) { |
| 107 | + if (defaultIdentifier) { |
| 108 | + const { name } = defaultIdentifier as t.Identifier; |
| 109 | + source.replace( |
| 110 | + /export default defineComponent/g, |
| 111 | + `const ${name} = defineComponent` |
| 112 | + ) + `\nexport default ${name}` |
| 113 | + } |
| 114 | + |
| 115 | + let callbackCode = ''; |
| 116 | + for (const { local, exported, id } of hotComponents) { |
| 117 | + source += |
| 118 | + `\n${local}.__hmrId = '${id}'` + |
| 119 | + `\n__VUE_HMR_RUNTIME__.createRecord('${id}', ${local})` |
| 120 | + callbackCode += `\n__VUE_HMR_RUNTIME__.reload("${id}", __${exported})` |
| 121 | + } |
| 122 | + |
| 123 | + source += ` |
| 124 | + /* hot reload */ |
| 125 | + if (module.hot) { |
| 126 | + module.hot.accept() |
| 127 | + ${callbackCode} |
| 128 | + } |
| 129 | + ` |
| 130 | + } |
| 131 | + |
| 132 | + loaderContext.callback(null, source, sourceMap); |
12 | 133 | };
|
0 commit comments