Skip to content

Commit c09b16d

Browse files
committed
fix import issue, update tests, update README
1 parent 39c9012 commit c09b16d

File tree

8 files changed

+138
-5
lines changed

8 files changed

+138
-5
lines changed

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ should keep free 1 core for *build* and 1 core for a *system* *(for example syst
104104
node doesn't share memory between workers - keep in mind that memory usage will increase. Be aware that in some scenarios increasing workers
105105
number **can increase checking time**. Default: `ForkTsCheckerWebpackPlugin.ONE_CPU`.
106106

107+
* **vue** `boolean`:
108+
If `true`, the linter and compiler will process VueJs single-file-component (.vue) files. See the
109+
[Vue section](https://github.com/Realytics/fork-ts-checker-webpack-plugin#vue) further down for information on how to correctly setup your project.
110+
107111
### Pre-computed consts:
108112
* `ForkTsCheckerWebpackPlugin.ONE_CPU` - always use one CPU
109113
* `ForkTsCheckerWebpackPlugin.ALL_CPUS` - always use all CPUs (will increase build time)
@@ -141,5 +145,94 @@ This plugin provides some custom webpack hooks (all are sync):
141145
|`fork-ts-checker-emit`| Service will add errors and warnings to webpack compilation ('build' mode) | `diagnostics`, `lints`, `elapsed` |
142146
|`fork-ts-checker-done`| Service finished type checking and webpack finished compilation ('watch' mode) | `diagnostics`, `lints`, `elapsed` |
143147

148+
## Vue
149+
1. Turn on the vue option in the plugin in your webpack config:
150+
151+
```
152+
new ForkTsCheckerWebpackPlugin({
153+
tslint: true,
154+
vue: true
155+
})
156+
```
157+
158+
2. To activate TypeScript in your `.vue` files, you need to ensure your script tag's language attribute is set
159+
to `ts` or `tsx` (also make sure you include the `.vue` extension in all your import statements as shown below):
160+
161+
```html
162+
<script lang="ts">
163+
import Hello from '@/components/hello.vue'
164+
165+
// ...
166+
167+
</script>
168+
```
169+
170+
3. Ideally you are also using `ts-loader` (in transpileOnly mode). Your Webpack config rules may look something like this:
171+
172+
```
173+
{
174+
test: /\.ts$/,
175+
loader: 'ts-loader',
176+
include: [resolve('src'), resolve('test')],
177+
options: {
178+
appendTsSuffixTo: [/\.vue$/],
179+
transpileOnly: true
180+
}
181+
},
182+
{
183+
test: /\.vue$/,
184+
loader: 'vue-loader',
185+
options: vueLoaderConfig
186+
},
187+
```
188+
4. Add rules to your `tslint.json` and they will be applied to Vue files. For example, you could apply the Standard JS rules [tslint-config-standard](https://github.com/blakeembrey/tslint-config-standard) like this:
189+
190+
```json
191+
{
192+
"defaultSeverity": "error",
193+
"extends": [
194+
"tslint-config-standard"
195+
]
196+
}
197+
```
198+
5. Ensure your `tsconfig.json` includes .vue files:
199+
200+
```
201+
// tsconfig.json
202+
{
203+
"include": [
204+
"src/**/*.ts",
205+
"src/**/*.vue"
206+
],
207+
"exclude": [
208+
"node_modules"
209+
]
210+
}
211+
```
212+
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):
215+
```
216+
// tsconfig.json
217+
{
218+
"compilerOptions": {
219+
220+
// ...
221+
222+
"baseUrl": ".",
223+
"paths": {
224+
"@/*": [
225+
"src/*"
226+
]
227+
}
228+
}
229+
}
230+
231+
// In a .ts or .vue file...
232+
import Hello from '@/components/hello.vue'
233+
```
234+
235+
7. If you are working in **VSCode**, you can get extensions [Vetur](https://marketplace.visualstudio.com/items?itemName=octref.vetur) and [TSLint Vue](https://marketplace.visualstudio.com/items?itemName=prograhammer.tslint-vue) to complete the developer workflow.
236+
144237
## License
145238
MIT

src/VueProgram.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class VueProgram {
5252
return moduleName;
5353
}
5454

55+
public static isVue(filePath: string) {
56+
return path.extname(filePath) === '.vue';
57+
}
58+
5559
static createProgram(
5660
programConfig: ts.ParsedCommandLine,
5761
basedir: string,
@@ -86,7 +90,7 @@ class VueProgram {
8690
let source = files.getData(filePath).source;
8791

8892
// get typescript contents from Vue file
89-
if (source && filePath.substr(-4) === '.vue') {
93+
if (source && VueProgram.isVue(filePath)) {
9094
const parsed = vueParser.parse(source.text, 'script', { lang: ['ts', 'tsx', 'js', 'jsx'] });
9195
source = ts.createSourceFile(filePath, parsed, languageVersion, true);
9296
}
@@ -109,10 +113,20 @@ class VueProgram {
109113
resolvedModules.push(result.resolvedModule);
110114
} else {
111115
// For non-ts extensions.
112-
resolvedModules.push({
113-
resolvedFileName: VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, programConfig.options),
114-
extension: '.ts'
115-
} as ts.ResolvedModuleFull);
116+
const absolutePath = VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, programConfig.options);
117+
118+
if (VueProgram.isVue(moduleName)) {
119+
resolvedModules.push({
120+
resolvedFileName: absolutePath,
121+
extension: '.ts'
122+
} as ts.ResolvedModuleFull);
123+
} else {
124+
resolvedModules.push({
125+
// If the file does exist, return an empty string (because we assume user has provided a ".d.ts" file for it).
126+
resolvedFileName: host.fileExists(absolutePath) ? '' : absolutePath,
127+
extension: '.ts'
128+
} as ts.ResolvedModuleFull);
129+
}
116130
}
117131
}
118132

test/integration/vue.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe('[INTEGRATION] vue', function () {
4545
transpileOnly: true,
4646
silent: true
4747
}
48+
},
49+
{
50+
test: /\.css$/,
51+
loader: 'css-loader'
4852
}
4953
]
5054
},

test/integration/vue/src/css.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '*.css' {
2+
const css = '';
3+
4+
export default css;
5+
}

test/integration/vue/src/example-wild.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
<script lang="ts">
88
import Vue from "vue";
99
import Component from "vue-class-component";
10+
import exampleCss from "./example.css";
1011
1112
@Component
1213
export default class Example extends Vue {
1314
public msg: string = "Hello World"
1415
public x: string = "1";
16+
public y: string = exampleCss;
1517
}
1618
</script>
1719

test/integration/vue/src/example.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
background-color: blue;
3+
}

test/integration/vue/src/example.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
<script lang="ts">
88
import Vue from "vue";
99
import Component from "vue-class-component";
10+
import exampleCss from "./example.css";
1011
1112
@Component
1213
export default class Example extends Vue {
1314
public msg: string = "Hello World";
1415
public x: string = 1;
16+
public y: string = exampleCss;
1517
}
1618
</script>
1719

test/unit/VueProgram.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ var expect = require('chai').expect;
44
var VueProgram = require('../../lib/VueProgram');
55

66
describe('[UNIT] VueProgram', function () {
7+
it('should determine if file is a Vue file', function() {
8+
expect(VueProgram.isVue('./test.vue')).to.be.true;
9+
expect(VueProgram.isVue('../test.vue')).to.be.true;
10+
expect(VueProgram.isVue('../../test.vue')).to.be.true;
11+
expect(VueProgram.isVue('@/test.vue')).to.be.true;
12+
expect(VueProgram.isVue('../../.vue')).to.be.false;
13+
expect(VueProgram.isVue('./test.css')).to.be.false;
14+
expect(VueProgram.isVue('./')).to.be.false;
15+
});
16+
717
it('should properly resolve relative module names', function() {
818
var basedir = '/base/dir';
919
var containingFile = '/con/tain/ing/main.ts';

0 commit comments

Comments
 (0)