-
Notifications
You must be signed in to change notification settings - Fork 807
/
vue.js
415 lines (305 loc) · 11.1 KB
/
vue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import test from 'ava';
import path from 'path';
import File from '../../src/File';
import webpack from '../helpers/webpack';
import assert from '../helpers/assertions';
import '../helpers/mix';
test.beforeEach(() => {
webpack.setupVueAliases(2);
mix.options({ processCssUrls: false });
});
test('it adds the Vue 2 resolve alias', async t => {
mix.vue({ version: 2, extractStyles: true });
t.is('vue/dist/vue.esm.js', (await webpack.buildConfig()).resolve.alias.vue$);
});
test('non-feature-flag use of mix.vue throws an error', t => {
t.throws(() => mix.vue('js/app.js', 'js'), {
message: /mix.vue\(\) is a feature flag/
});
});
test('it adds the Vue 2 runtime resolve alias', async t => {
mix.vue({ version: 2, runtimeOnly: true });
t.is('vue/dist/vue.runtime.esm.js', (await webpack.buildConfig()).resolve.alias.vue$);
});
test('it knows the Vue 2 compiler name', t => {
mix.vue({ version: 2 });
let dependencies = Mix.components.get('vue').dependencies();
t.true(dependencies.includes('vue-template-compiler'));
});
test('it switches to vue-style-loader when not extracting styles', async t => {
mix.vue({ version: 2, extractStyles: false });
const config = await webpack.buildConfig();
assert.hasWebpackLoader(t, config, 'vue-style-loader');
assert.doesNotHaveWebpackLoader(t, config, 'style-loader');
assert.doesNotHaveWebpackLoader(t, config, loader =>
loader.includes('mini-css-extract-plugin')
);
});
test('it does not switch to vue-style-loader when extracting styles', async t => {
mix.vue({ version: 2, extractStyles: true });
const config = await webpack.buildConfig();
assert.doesNotHaveWebpackLoader(t, config, 'vue-style-loader');
assert.hasWebpackLoader(t, config, loader =>
loader.includes('mini-css-extract-plugin')
);
});
test('it does not use vue-style-loader when not using .vue', async t => {
const config = await webpack.buildConfig();
assert.doesNotHaveWebpackLoader(t, config, 'vue-style-loader');
assert.hasWebpackLoader(t, config, loader =>
loader.includes('mini-css-extract-plugin')
);
});
test('it appends vue styles to your sass compiled file', async t => {
mix.vue({ version: 2, extractStyles: true });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-scss.js`, 'js/app.js').sass(
`test/fixtures/app/src/sass/app.scss`,
'css/app.css'
);
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/app.css`));
let expected = `body {
color: red;
}
.hello {
color: blue;
background: url(./does-not-exist.png);
}
`;
assert.fileMatchesCss(`test/fixtures/app/dist/css/app.css`, expected, t);
});
test('it appends vue styles to your less compiled file', async t => {
mix.vue({ version: 2, extractStyles: true });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-scss.js`, 'js/app.js').less(
`test/fixtures/app/src/less/main.less`,
'css/app.css'
);
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/app.css`));
let expected = `body {
color: pink;
}
.hello {
color: blue;
background: url(./does-not-exist.png);
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/app.css`).read());
});
test('it appends vue styles to a vue-styles.css file, if no preprocessor is used', async t => {
mix.vue({ version: 2, extractStyles: true });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-scss.js`, 'js/app.js');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/vue-styles.css`));
let expected = `.hello {
color: blue;
background: url(./does-not-exist.png);
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/vue-styles.css`).read());
});
test('it extracts vue vanilla CSS styles to a dedicated file', async t => {
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-css.js`, 'js/app.js');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `
.hello {
color: green;
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/components.css`).read());
});
test('it extracts vue Stylus styles to a dedicated file', async t => {
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-stylus.js`, 'js/app.js');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `.hello {
margin: 10px;
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/components.css`).read());
});
test('it does also add the vue webpack rules with typescript component', async t => {
mix.vue({ version: 2 });
mix.ts('js/app.js', 'dist/js');
t.truthy(
(await webpack.buildConfig()).module.rules.find(
rule => rule.test.toString() === '/\\.vue$/'
)
);
});
test('it extracts vue .scss styles to a dedicated file', async t => {
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-scss.js`, 'js/app.js').sass(
`test/fixtures/app/src/sass/app.scss`,
'css/app.css'
);
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/app.css`));
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `body {
color: red;
}
`;
assert.fileMatchesCss(`test/fixtures/app/dist/css/app.css`, expected, t);
expected = `.hello {
color: blue;
background: url(./does-not-exist.png);
}
`;
assert.fileMatchesCss(`test/fixtures/app/dist/css/components.css`, expected, t);
});
test('it extracts vue .sass styles to a dedicated file', async t => {
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(
`test/fixtures/app/src/vue/app-with-vue-and-indented-sass.js`,
'js/app.js'
).sass(`test/fixtures/app/src/sass/app.scss`, 'css/app.css');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/app.css`));
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `body {
color: red;
}
`;
assert.fileMatchesCss(`test/fixtures/app/dist/css/app.css`, expected, t);
expected = `.hello {
color: black;
}
`;
assert.fileMatchesCss(`test/fixtures/app/dist/css/components.css`, expected, t);
});
test('it extracts vue PostCSS styles to a dedicated file', async t => {
// Given the user has a postcss.config.js file with the postcss-custom-properties plugin...
let postCssConfigFile = new File(path.resolve('postcss.config.js')).write(
`module.exports = { plugins: [require('postcss-custom-properties')] };`
);
// When we compile Vue...
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-postcss.js`, 'js/app.js');
await webpack.compile();
// The generated output should be...
let expected = `
:root {
--color: white;
}
.hello {
color: white;
color: var(--color);
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/components.css`).read());
// Clean up.
postCssConfigFile.delete();
});
test('it extracts vue Less styles to a dedicated file', async t => {
mix.vue({ version: 2, extractStyles: 'css/components.css' });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-less.js`, 'js/app.js');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `.hello {
color: blue;
}
`;
t.is(expected, File.find(`test/fixtures/app/dist/css/components.css`).read());
});
test('it supports global Vue styles for sass', async t => {
// Given the user has a postcss.config.js file with the postcss-custom-properties plugin...
let postCssConfigFile = new File(path.resolve('postcss.config.js')).write(
`module.exports = { plugins: [require('postcss-custom-properties')] };`
);
mix.vue({
version: 2,
extractStyles: 'css/components.css',
globalStyles: {
css: [`test/fixtures/app/src/css/global.css`],
sass: [`test/fixtures/app/src/sass/global.sass`],
scss: [`test/fixtures/app/src/sass/global.scss`],
less: [`test/fixtures/app/src/less/global.less`],
stylus: [`test/fixtures/app/src/stylus/global.styl`]
}
});
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-global-styles.js`, 'js/app.js');
mix.sass(`test/fixtures/app/src/sass/app.scss`, 'css/app.css');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
t.true(File.exists(`test/fixtures/app/dist/css/components.css`));
let expected = `
:root {
--shared-color: rebeccapurple;
}
.shared-css {
color: rebeccapurple;
color: var(--shared-color);
}
.shared-scss {
color: rebeccapurple;
}
.shared-sass {
color: rebeccapurple;
}
.shared-less {
color: rebeccapurple;
}
.shared-stylus {
color: #639;
}
`;
t.is(
expected.trim(),
File.find(`test/fixtures/app/dist/css/components.css`).read().trim()
);
postCssConfigFile.delete();
});
test('it supports Vue SFCs with separate files', async t => {
mix.vue({ version: 2 });
mix.js(`test/fixtures/app/src/vue/app-with-vue-separate-files.js`, 'js/app.js');
await webpack.compile();
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
});
test('Vue-loader options via mix.options.vue', async t => {
const compiler = compilerSpy();
mix.vue({ version: 2 });
mix.options({ vue: { compiler } });
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-css.js`, 'js/app.js');
await webpack.compile();
t.truthy(compiler.called);
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
});
test('Vue-loader options via mix.vue', async t => {
const compiler = compilerSpy();
mix.vue({
version: 2,
options: { compiler }
});
mix.js(`test/fixtures/app/src/vue/app-with-vue-and-css.js`, 'js/app.js');
await webpack.compile();
t.true(compiler.called);
t.true(File.exists(`test/fixtures/app/dist/js/app.js`));
});
function compilerSpy() {
const compiler = require('vue-template-compiler');
let called = false;
// We don't use sinon.spy here because if you create a spy of
// `require("vue-template-compiler")` it will always be true
// as that modifies the global object that's used by default.
// Since we want to ensure that passing loader options works
// we need to do that by ensuring our "custom" compiler is used
return {
...compiler,
compile(...args) {
called = true;
return compiler.compile(...args);
},
get called() {
return called;
}
};
}