11import {
22 CodeOptions ,
33 RecordInfo ,
4+ createVueInspectorNodeTransform ,
45 getCodeWithWebComponent ,
56 getProjectRecord ,
67 isDev ,
@@ -14,10 +15,111 @@ const compatibleDirname = path.dirname(fileURLToPath(import.meta.url));
1415
1516interface LoaderOptions extends CodeOptions {
1617 record : RecordInfo ;
18+ vueCompilerNodeTransform ?: boolean ;
1719}
1820
1921const baseLoaderPath = path . resolve ( compatibleDirname , './loader.js' ) ;
2022const injectLoaderPath = path . resolve ( compatibleDirname , './inject-loader.js' ) ;
23+ const codeInspectorVueNodeTransform = Symbol . for (
24+ 'code-inspector.vueNodeTransform' ,
25+ ) ;
26+
27+ function getUseItems ( rule : any ) : any [ ] {
28+ if ( ! rule ) {
29+ return [ ] ;
30+ }
31+
32+ if ( Array . isArray ( rule . use ) ) {
33+ return rule . use ;
34+ }
35+
36+ if ( rule . loader ) {
37+ return [ rule ] ;
38+ }
39+
40+ return [ ] ;
41+ }
42+
43+ function walkRules ( rules : any [ ] , visitor : ( rule : any ) => void ) {
44+ rules . forEach ( ( rule ) => {
45+ visitor ( rule ) ;
46+ if ( Array . isArray ( rule . rules ) ) {
47+ walkRules ( rule . rules , visitor ) ;
48+ }
49+ if ( Array . isArray ( rule . oneOf ) ) {
50+ walkRules ( rule . oneOf , visitor ) ;
51+ }
52+ } ) ;
53+ }
54+
55+ function isVueLoader ( loader : string ) {
56+ return / ( ^ | [ \\ / ] ) v u e - l o a d e r ( [ \\ / ] | $ ) / . test ( loader ) ;
57+ }
58+
59+ function isVueTemplateLoader ( loader : string ) {
60+ return / ( ^ | [ \\ / ] ) v u e - l o a d e r [ \\ / ] d i s t [ \\ / ] t e m p l a t e L o a d e r \. j s $ / . test ( loader ) ;
61+ }
62+
63+ function applyVueCompilerNodeTransform ( options : CodeOptions , compiler : any ) {
64+ const _compiler = compiler ?. compiler || compiler ;
65+ const module = _compiler ?. options ?. module ;
66+ const rules = module ?. rules || module ?. loaders || [ ] ;
67+ let applied = false ;
68+
69+ walkRules ( rules , ( rule ) => {
70+ getUseItems ( rule ) . forEach ( ( item ) => {
71+ const loader = typeof item === 'string' ? item : item ?. loader ;
72+ if (
73+ typeof loader !== 'string' ||
74+ ( ! isVueLoader ( loader ) && ! isVueTemplateLoader ( loader ) )
75+ ) {
76+ return ;
77+ }
78+
79+ if ( typeof item === 'string' ) {
80+ return ;
81+ }
82+
83+ if ( ! item . options || typeof item . options !== 'object' ) {
84+ item . options = { } ;
85+ }
86+
87+ const loaderOptions = item . options ;
88+ if (
89+ ! loaderOptions . compilerOptions ||
90+ typeof loaderOptions . compilerOptions !== 'object'
91+ ) {
92+ loaderOptions . compilerOptions = { } ;
93+ }
94+
95+ const compilerOptions = loaderOptions . compilerOptions ;
96+ if ( ! Array . isArray ( compilerOptions . nodeTransforms ) ) {
97+ compilerOptions . nodeTransforms = [ ] ;
98+ }
99+
100+ const nodeTransforms = compilerOptions . nodeTransforms ;
101+ const hasRegistered = nodeTransforms . some (
102+ ( transform : any ) => transform ?. [ codeInspectorVueNodeTransform ] ,
103+ ) ;
104+
105+ if ( ! hasRegistered ) {
106+ const transform = createVueInspectorNodeTransform ( {
107+ escapeTags : options . escapeTags ,
108+ mappings : options . mappings ,
109+ pathType : options . pathType ,
110+ } ) ;
111+ Object . defineProperty ( transform , codeInspectorVueNodeTransform , {
112+ value : true ,
113+ } ) ;
114+ nodeTransforms . push ( transform ) ;
115+ }
116+
117+ applied = true ;
118+ } ) ;
119+ } ) ;
120+
121+ return applied ;
122+ }
21123
22124function hasRegisteredCodeInspectorLoader ( rules : any [ ] ) {
23125 return rules . some ( ( rule ) =>
@@ -165,14 +267,18 @@ class WebpackCodeInspectorPlugin {
165267 if ( this . options . cache ) {
166268 // 用来在 cache 情况下启动 node server
167269 record . port =
168- this . options . port || getProjectRecord ( record ) ?. previousPort ;
270+ this . options . port || getProjectRecord ( record ) ?. previousPort || 0 ;
169271 getPureClientCodeString ( this . options , record , true ) ;
170272 } else {
171273 cache . version = `code-inspector-${ Date . now ( ) } ` ;
172274 }
173275 }
174276
175- applyLoader ( { ...this . options , record } , compiler ) ;
277+ const vueCompilerNodeTransform = applyVueCompilerNodeTransform (
278+ this . options ,
279+ compiler ,
280+ ) ;
281+ applyLoader ( { ...this . options , record, vueCompilerNodeTransform } , compiler ) ;
176282
177283 if (
178284 compiler ?. hooks ?. emit &&
0 commit comments