Skip to content

Commit 17a3964

Browse files
committed
Fix file-loader check for Vue 2
1 parent a60a9cd commit 17a3964

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

Diff for: src/components/Vue.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,31 @@ module.exports = class Vue extends Component {
102102
// Disable es modules for file-loader on Vue 2
103103
if (this.version === 2) {
104104
for (const rule of config.module.rules || []) {
105-
for (const loader of rule.use || []) {
105+
if (typeof rule !== 'object') {
106+
continue;
107+
}
108+
109+
let loaders = rule.use || [];
110+
111+
if (!Array.isArray(loaders)) {
112+
continue;
113+
}
114+
115+
for (const loader of loaders) {
116+
if (typeof loader !== 'object') {
117+
continue;
118+
}
119+
106120
// TODO: This isn't the best check
107121
// We should check that the loader itself is correct
108122
// Not that file-loader is anywhere in it's absolute path
109123
// As this can produce false positives
110-
if (loader.loader.includes('file-loader')) {
124+
if (
125+
loader.loader &&
126+
loader.loader.includes('file-loader') &&
127+
loader.options
128+
) {
129+
// @ts-ignore
111130
loader.options.esModule = false;
112131
}
113132
}

Diff for: test/features/vue.js

+84
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,90 @@ export function setupVueTests({ version, dir }) {
436436
.file(`test/fixtures/app/dist/js/app.js`)
437437
.contains(['false ? 0 : _vue_shared__']);
438438
});
439+
440+
test.serial(
441+
'File loader has esModule disabled for Vue 2 and is not broken by custom loader rules',
442+
async t => {
443+
const { assert, mix, webpack } = context(t);
444+
445+
if (version === 3) {
446+
t.pass.skip('Skipping for vue 3');
447+
448+
return;
449+
}
450+
451+
// Using an extension so it modifies the rules before Vue processes them
452+
mix.extend('foo', {
453+
webpackRules() {
454+
return [
455+
{
456+
test: /\.something1$/,
457+
loader: 'css-loader'
458+
},
459+
{
460+
test: /\.something2$/,
461+
use: 'css-loader'
462+
},
463+
{
464+
test: /\.something3$/,
465+
use: ['css-loader']
466+
},
467+
{
468+
test: /\.something3$/,
469+
use: [{ ident: 'foobar' }]
470+
}
471+
];
472+
}
473+
});
474+
475+
// @ts-ignore
476+
mix.foo();
477+
478+
mix.options({ assetModules: false });
479+
mix.vue();
480+
mix.js(`test/fixtures/app/src/${dir}/app-with-vue-and-css.js`, 'js/app.js');
481+
482+
const config = await webpack.buildConfig();
483+
const spy = sinon.spy();
484+
485+
// TODO: This is basically a copy of the module code in the Vue.js file
486+
// That's not so great…
487+
for (const rule of (config.module && config.module.rules) || []) {
488+
if (typeof rule !== 'object') {
489+
continue;
490+
}
491+
492+
let loaders = (rule && rule.use) || [];
493+
494+
if (!Array.isArray(loaders)) {
495+
continue;
496+
}
497+
498+
for (const loader of loaders) {
499+
if (typeof loader !== 'object') {
500+
continue;
501+
}
502+
503+
// TODO: This isn't the best check
504+
// We should check that the loader itself is correct
505+
// Not that file-loader is anywhere in it's absolute path
506+
// As this can produce false positives
507+
if (
508+
loader.loader &&
509+
loader.loader.includes('file-loader') &&
510+
loader.options
511+
) {
512+
spy();
513+
514+
// @ts-ignore
515+
t.falsy(loader.options.esModule);
516+
}
517+
}
518+
}
519+
520+
t.true(spy.called);
521+
}
522+
);
439523
}
440524

441525
async function compilerSpy(Mix) {

0 commit comments

Comments
 (0)