Skip to content

Commit 840d0fd

Browse files
authored
Merge pull request #86 from CKGrafico/patch-1
Add options to wildcard
2 parents ff3ade3 + 7adf65e commit 840d0fd

File tree

5 files changed

+855
-88
lines changed

5 files changed

+855
-88
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ import Hello from '@/components/hello.vue'
210210
}
211211
```
212212

213-
6. The commonly used `@` path wildcard will work if you set up a `baseUrl` and `paths` (in `compilerOptions`) to include `@/*`. If you don't set this, then
214-
the fallback for the `@` wildcard will be `[tsconfig directory]/src` (we hope to make this more flexible on future releases):
213+
6. It accepts any wildcard in your TypeScript configuration:
215214
```
216215
// tsconfig.json
217216
{
@@ -223,6 +222,9 @@ the fallback for the `@` wildcard will be `[tsconfig directory]/src` (we hope to
223222
"paths": {
224223
"@/*": [
225224
"src/*"
225+
],
226+
"~/*": [
227+
"src/*"
226228
]
227229
}
228230
}

src/VueProgram.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,43 @@ class VueProgram {
3131
}
3232

3333
/**
34-
* Since 99.9% of Vue projects use the wildcard '@/*', we only search for that in tsconfig CompilerOptions.paths.
34+
* Search for default wildcard or wildcard from options, we only search for that in tsconfig CompilerOptions.paths.
3535
* The path is resolved with thie given substitution and includes the CompilerOptions.baseUrl (if given).
3636
* If no paths given in tsconfig, then the default substitution is '[tsconfig directory]/src'.
3737
* (This is a fast, simplified inspiration of what's described here: https://github.com/Microsoft/TypeScript/issues/5039)
3838
*/
3939
public static resolveNonTsModuleName(moduleName: string, containingFile: string, basedir: string, options: ts.CompilerOptions) {
4040
const baseUrl = options.baseUrl ? options.baseUrl : basedir;
41-
const pattern = options.paths ? options.paths['@/*'] : undefined;
42-
const substitution = pattern ? options.paths['@/*'][0].replace('*', '') : 'src';
43-
const isWildcard = moduleName.substr(0, 2) === '@/';
41+
const discardedSymbols = ['.', '..', '/'];
42+
const wildcards: string[] = [];
43+
44+
if (options.paths) {
45+
Object.keys(options.paths).forEach(key => {
46+
const pathSymbol = key[0];
47+
if (discardedSymbols.indexOf(pathSymbol) < 0 && wildcards.indexOf(pathSymbol) < 0) {
48+
wildcards.push(pathSymbol);
49+
}
50+
});
51+
} else {
52+
wildcards.push('@');
53+
}
54+
4455
const isRelative = !path.isAbsolute(moduleName);
56+
let correctWildcard;
4557

46-
if (isWildcard) {
58+
wildcards.forEach(wildcard => {
59+
if (moduleName.substr(0, 2) === `${wildcard}/`) {
60+
correctWildcard = wildcard;
61+
}
62+
});
63+
64+
if (correctWildcard) {
65+
const pattern = options.paths ? options.paths[`${correctWildcard}/*`] : undefined;
66+
const substitution = pattern ? options.paths[`${correctWildcard}/*`][0].replace('*', '') : 'src';
4767
moduleName = path.resolve(baseUrl, substitution, moduleName.substr(2));
4868
} else if (isRelative) {
4969
moduleName = path.resolve(path.dirname(containingFile), moduleName);
5070
}
51-
5271
return moduleName;
5372
}
5473

test/integration/vue.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('[INTEGRATION] vue', function () {
134134
createCompiler({ tslint: true, vue: true });
135135

136136
compiler.run(function(error, stats) {
137-
expect(stats.compilation.errors.length).to.be.equal(2);
137+
expect(stats.compilation.errors.length).to.be.equal(1);
138138
callback();
139139
});
140140
});
@@ -143,8 +143,8 @@ describe('[INTEGRATION] vue', function () {
143143
createCompiler({ tslint: true, vue: true, checkSyntacticErrors: true });
144144

145145
compiler.run(function(error, stats) {
146-
expect(stats.compilation.errors.length).to.be.equal(3);
146+
expect(stats.compilation.errors.length).to.be.equal(2);
147147
callback();
148148
});
149149
});
150-
});
150+
});

test/unit/VueProgram.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('[UNIT] VueProgram', function () {
99
expect(VueProgram.isVue('../test.vue')).to.be.true;
1010
expect(VueProgram.isVue('../../test.vue')).to.be.true;
1111
expect(VueProgram.isVue('@/test.vue')).to.be.true;
12+
expect(VueProgram.isVue('~/test.vue')).to.be.true;
1213
expect(VueProgram.isVue('../../.vue')).to.be.false;
1314
expect(VueProgram.isVue('./test.css')).to.be.false;
1415
expect(VueProgram.isVue('./')).to.be.false;
@@ -63,4 +64,4 @@ describe('[UNIT] VueProgram', function () {
6364
resolvedModuleName = VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, options);
6465
expect(resolvedModuleName).to.be.equal('/baseurl3/src1/src2/test.vue');
6566
});
66-
});
67+
});

0 commit comments

Comments
 (0)