Skip to content

Commit 2fd1263

Browse files
committed
fix(vitest):🐞@import "~@/styles/theme/vars"
TEST FAILED OUTPUT: src/components/XmWallet/__test__/XmSimpleCard.spec.ts [ src/components/XmWallet/__test__/XmSimpleCard.spec.ts ] ReferenceError: ~/xmiles/taro-weapp/src/components/XmWallet/src/xmWallet.module.styl:1:9 1| @import "~@/styles/theme/vars" --------------^ 2| 3| .wrapper 4| position relative aliases is not defined // RESOLUTION: // @ see vuejs/component-compiler-utils#49
1 parent 31b8f33 commit 2fd1263

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

vitest.config.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,56 @@
33
import path from 'path';
44
import { defineConfig } from 'vitest/config';
55
import vue from '@vitejs/plugin-vue';
6+
import { Evaluator } from 'stylus';
7+
8+
// aliases
9+
const aliases = {
10+
'@/': `${path.resolve(__dirname, 'src')}/`,
11+
'@/styles': `${path.resolve(__dirname, 'src')}/styles`,
12+
};
13+
14+
// ===================================================================
15+
/* TEST FAILED OUTPUT:
16+
src/components/XmWallet/__test__/XmSimpleCard.spec.ts [ src/components/XmWallet/__test__/XmSimpleCard.spec.ts ]
17+
ReferenceError: ~/xmiles/taro-weapp/src/components/XmWallet/src/xmWallet.module.styl:1:9
18+
1| @import "~@/styles/theme/vars"
19+
--------------^
20+
2|
21+
3| .wrapper
22+
4| position relative
23+
24+
aliases is not defined */
25+
26+
// RESOLUTION:
27+
// @ see https://github.com/vuejs/component-compiler-utils/issues/49
28+
const visitImport = Evaluator.prototype.visitImport;
29+
Evaluator.prototype.visitImport = function (imported) {
30+
const path2 = imported.path.first;
31+
32+
if (path2.string.startsWith('~')) {
33+
const alias = Object.keys(aliases).find((entry) => path2.string.startsWith(`~${entry}`));
34+
35+
if (alias) path2.string = path2.string.substr(1).replace(alias, aliases[alias]);
36+
}
37+
38+
return visitImport.call(this, imported);
39+
};
40+
41+
// ===================================================================
642

743
export default defineConfig({
844
resolve: {
9-
alias: {
10-
'@/': `${path.resolve(__dirname, 'src')}/`,
11-
},
45+
alias: aliases,
1246
},
13-
plugins: [vue()],
47+
plugins: [
48+
vue({
49+
style: {
50+
preprocessOptions: {
51+
stylus: { Evaluator },
52+
},
53+
},
54+
}),
55+
],
1456
test: {
1557
// 启用类似 jest 的全局测试 API
1658
globals: true,

0 commit comments

Comments
 (0)