Skip to content

Commit e31bfd9

Browse files
committed
wip(vue2): share tests between vue2/vue3 and improve error handling
1 parent 19c6a37 commit e31bfd9

File tree

8 files changed

+658
-1163
lines changed

8 files changed

+658
-1163
lines changed

src/createVue2SFCModule.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,26 @@ import {
2424
import babelPluginTransformModulesCommonjs from '@babel/plugin-transform-modules-commonjs'
2525

2626

27-
import { formatError, withCache, hash, renameDynamicImport, parseDeps, interopRequireDefault, transformJSCode, loadDeps, createModule } from './tools.ts'
27+
import {
28+
formatError,
29+
formatErrorStartEnd,
30+
withCache,
31+
hash,
32+
renameDynamicImport,
33+
parseDeps,
34+
interopRequireDefault,
35+
transformJSCode,
36+
loadDeps,
37+
createModule,
38+
formatErrorLineColumn
39+
} from './tools'
2840

29-
import { Options, LoadModule, ModuleExport, CustomBlockCallback } from './types.ts'
41+
import {
42+
Options,
43+
LoadModule,
44+
ModuleExport,
45+
CustomBlockCallback
46+
} from './types'
3047

3148
/**
3249
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
@@ -114,7 +131,7 @@ export async function createSFCModule(source : string, filename : string, option
114131
});
115132

116133
} catch(ex) {
117-
log?.('error', 'SFC script', formatError(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
134+
log?.('error', 'SFC script', formatErrorLineColumn(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
118135
throw ex;
119136
}
120137

@@ -148,20 +165,36 @@ export async function createSFCModule(source : string, filename : string, option
148165

149166
const template = sfc_compileTemplate(compileTemplateOptions);
150167
// "@vue/component-compiler-utils" does NOT assume any module system, and expose render in global scope.
151-
template.code += `\nexport { render, staticRenderFns }`
168+
template.code += `\nmodule.exports = { render, staticRenderFns }`
152169

153170
if ( template.errors.length ) {
154171

155172
preventCache();
156-
for ( const err of template.errors ) {
173+
for ( let err of template.errors ) {
174+
if (typeof err !== 'object') {
175+
err = {
176+
msg: err,
177+
start: undefined,
178+
end: undefined
179+
}
180+
}
157181

158182
// @ts-ignore (Property 'message' does not exist on type 'string | CompilerError')
159-
log?.('error', 'SFC template', err );
183+
log?.('error', 'SFC template', formatErrorStartEnd(err.msg, filename, compileTemplateOptions.source.trim(), err.start, err.end ));
160184
}
161185
}
162186

163-
for ( const err of template.tips )
164-
log?.('info', 'SFC template', err);
187+
for ( let err of template.tips ) {
188+
if (typeof err !== 'object') {
189+
err = {
190+
msg: err,
191+
start: undefined,
192+
end: undefined
193+
}
194+
}
195+
196+
log?.('info', 'SFC template', formatErrorStartEnd(err.msg, filename, source, err.start, err.end ));
197+
}
165198

166199
return await transformJSCode(template.code, true, filename, options);
167200
});
@@ -185,7 +218,7 @@ export async function createSFCModule(source : string, filename : string, option
185218
filename,
186219
id: scopeId,
187220
scoped: descStyle.scoped,
188-
trim: false, // Should we enable it, as it requires postcss trimPlugin ?
221+
trim: false,
189222
preprocessLang: descStyle.lang
190223
});
191224

@@ -195,7 +228,7 @@ export async function createSFCModule(source : string, filename : string, option
195228
for ( const err of compiledStyle.errors ) {
196229

197230
// @ts-ignore (Property 'line' does not exist on type 'Error' and Property 'column' does not exist on type 'Error')
198-
log?.('error', 'SFC style', formatError(err.message, filename, source, err.line, err.column) );
231+
log?.('error', 'SFC style', formatError(err, filename, descStyle.content));
199232
}
200233
}
201234

src/createVue3SFCModule.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,24 @@ import {
2828
// @ts-ignore (Could not find a declaration file for module '@babel/plugin-transform-modules-commonjs')
2929
import babelPluginTransformModulesCommonjs from '@babel/plugin-transform-modules-commonjs'
3030

31+
import {
32+
formatErrorLineColumn,
33+
withCache,
34+
hash,
35+
renameDynamicImport,
36+
parseDeps,
37+
interopRequireDefault,
38+
transformJSCode,
39+
loadDeps,
40+
createModule
41+
} from './tools'
3142

32-
import { formatError, withCache, hash, renameDynamicImport, parseDeps, interopRequireDefault, transformJSCode, loadDeps, createModule } from './tools.ts'
33-
34-
import { Options, LoadModule, ModuleExport, CustomBlockCallback } from './types.ts'
43+
import {
44+
Options,
45+
LoadModule,
46+
ModuleExport,
47+
CustomBlockCallback
48+
} from './types'
3549

3650

3751
/**
@@ -140,7 +154,7 @@ export async function createSFCModule(source : string, filename : string, option
140154

141155
} catch(ex) {
142156

143-
log?.('error', 'SFC script', formatError(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
157+
log?.('error', 'SFC script', formatErrorLineColumn(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
144158
throw ex;
145159
}
146160
} else {
@@ -188,7 +202,7 @@ export async function createSFCModule(source : string, filename : string, option
188202
for ( const err of template.errors ) {
189203

190204
// @ts-ignore (Property 'message' does not exist on type 'string | CompilerError')
191-
log?.('error', 'SFC template', formatError(err.message, filename, source, err.loc.start.line + descriptor.template.loc.start.line - 1, err.loc.start.column) );
205+
log?.('error', 'SFC template', formatErrorLineColumn(err.message, filename, source, err.loc.start.line + descriptor.template.loc.start.line - 1, err.loc.start.column) );
192206
}
193207
}
194208

@@ -229,7 +243,7 @@ export async function createSFCModule(source : string, filename : string, option
229243
for ( const err of compiledStyle.errors ) {
230244

231245
// @ts-ignore (Property 'line' does not exist on type 'Error' and Property 'column' does not exist on type 'Error')
232-
log?.('error', 'SFC style', formatError(err.message, filename, source, err.line + descStyle.loc.start.line - 1, err.column) );
246+
log?.('error', 'SFC style', formatErrorLineColumn(err.message, filename, source, err.line + descStyle.loc.start.line - 1, err.column) );
233247
}
234248
}
235249

src/tools.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
import {
1717
codeFrameColumns,
18+
SourceLocation,
1819
} from '@babel/code-frame';
1920

2021
// @ts-ignore (Could not find a declaration file for module '@babel/plugin-transform-modules-commonjs')
@@ -24,9 +25,12 @@ import babelPluginTransformModulesCommonjs from '@babel/plugin-transform-modules
2425
// @ts-ignore (TS7016: Could not find a declaration file for module 'spark-md5')
2526
import SparkMD5 from 'spark-md5'
2627

27-
28-
import { Cache, ValueFactory, Options, LoadModule, ModuleExport } from './types.ts'
29-
28+
import {
29+
Cache,
30+
LoadModule,
31+
Options,
32+
ValueFactory
33+
} from './types'
3034

3135
/**
3236
* @internal
@@ -37,20 +41,45 @@ const version : string = process.env.VERSION;
3741

3842

3943
// tools
44+
/**
45+
* @internal
46+
*/
47+
export function formatError(message : string, path : string, source : string) : string {
48+
return path + '\n' + message;
49+
}
4050

4151

4252
/**
4353
* @internal
4454
*/
45-
export function formatError(message : string, path : string, source : string, line? : number, column? : number) : string {
55+
export function formatErrorLineColumn(message : string, path : string, source : string, line? : number, column? : number) : string {
56+
if (!line) {
57+
return formatError(message, path, source)
58+
}
59+
60+
const location = {
61+
start: { line, column },
62+
};
4663

47-
const location = {
48-
start: { line, column },
49-
};
64+
return formatError(codeFrameColumns(source, location, { message }), path, source)
65+
}
5066

51-
return '\n' + path + '\n' + codeFrameColumns(source, location, {
52-
message,
53-
}) + '\n';
67+
/**
68+
* @internal
69+
*/
70+
export function formatErrorStartEnd(message : string, path : string, source : string, start : number, end? : number) : string {
71+
if (!start) {
72+
return formatError(message, path, source)
73+
}
74+
75+
const location: SourceLocation = {
76+
start: { line: 1, column: start }
77+
};
78+
if (end) {
79+
location.end = {line: 1, column: end}
80+
}
81+
82+
return formatError(codeFrameColumns(source, location, { message }), path, source)
5483
}
5584

5685

@@ -171,7 +200,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
171200
});
172201
} catch(ex) {
173202

174-
log?.('error', 'parse script', formatError(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
203+
log?.('error', 'parse script', formatErrorLineColumn(ex.message, filename, source, ex.loc.line, ex.loc.column + 1) );
175204
throw ex;
176205
}
177206

test/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"scripts": {
33
"start": "node ./serve.js",
4-
"startVue2": "cross-env VUE_VERSION=2 node ./serveVue2.js"
4+
"startVue2": "cross-env VUE_VERSION=2 node ./serve.js"
55
},
66
"dependencies": {
77
"express": "^4.17.1",
88
"mime-db": "^1.45.0",
99
"open": "^7.3.0",
1010
"puppeteer": "^5.5.0"
11+
},
12+
"devDependencies": {
13+
"cross-env": "^7.0.3"
1114
}
1215
}

test/yarn.lock

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ [email protected]:
125125
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
126126
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
127127

128+
cross-env@^7.0.3:
129+
version "7.0.3"
130+
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
131+
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
132+
dependencies:
133+
cross-spawn "^7.0.1"
134+
135+
cross-spawn@^7.0.1:
136+
version "7.0.3"
137+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
138+
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
139+
dependencies:
140+
path-key "^3.1.0"
141+
shebang-command "^2.0.0"
142+
which "^2.0.1"
143+
128144
129145
version "2.6.9"
130146
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -372,6 +388,11 @@ is-wsl@^2.1.1:
372388
dependencies:
373389
is-docker "^2.0.0"
374390

391+
isexe@^2.0.0:
392+
version "2.0.0"
393+
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
394+
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
395+
375396
locate-path@^5.0.0:
376397
version "5.0.0"
377398
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
@@ -504,6 +525,11 @@ path-is-absolute@^1.0.0:
504525
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
505526
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
506527

528+
path-key@^3.1.0:
529+
version "3.1.1"
530+
resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
531+
integrity sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==
532+
507533
508534
version "0.1.7"
509535
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
@@ -650,6 +676,18 @@ [email protected]:
650676
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"
651677
integrity sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==
652678

679+
shebang-command@^2.0.0:
680+
version "2.0.0"
681+
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
682+
integrity sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==
683+
dependencies:
684+
shebang-regex "^3.0.0"
685+
686+
shebang-regex@^3.0.0:
687+
version "3.0.0"
688+
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
689+
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
690+
653691
"statuses@>= 1.5.0 < 2", statuses@~1.5.0:
654692
version "1.5.0"
655693
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
@@ -729,6 +767,13 @@ vary@~1.1.2:
729767
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
730768
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
731769

770+
which@^2.0.1:
771+
version "2.0.2"
772+
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
773+
integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==
774+
dependencies:
775+
isexe "^2.0.0"
776+
732777
wrappy@1:
733778
version "1.0.2"
734779
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

0 commit comments

Comments
 (0)