Skip to content

Commit 7e3cbc1

Browse files
author
Baitu Huang
committed
fix: useVue logic
1 parent 1537ad5 commit 7e3cbc1

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

lib/plugin.js

+28-23
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,32 @@ module.exports = class VueFlagsWebpackPlugin {
4545
}
4646
}
4747
}
48-
patchRules (rules, useVue) {
48+
useVue (plugins) {
49+
let vueLoader
50+
let VueLoaderPlugin
51+
try {
52+
vueLoader = require('vue-loader')
53+
VueLoaderPlugin = require('vue-loader/lib/plugin')
54+
} catch (err) {
55+
if (vueLoader && !VueLoaderPlugin) {
56+
throw genError('vue-loader < 15 is not supported, ' + err.message)
57+
}
58+
return false
59+
}
60+
const useVue = !!plugins.find(p => p.constructor === VueLoaderPlugin)
4961
if (useVue) {
62+
require.resolve('vue-template-compiler')
63+
const vueCompilerVersion = require('vue-template-compiler/package.json').version
64+
const semver = require('semver')
65+
if (semver.lt(vueCompilerVersion, MIN_VERSION)) {
66+
throw genError(`vue-template-compiler < ${MIN_VERSION} is not supported`)
67+
}
68+
}
69+
return useVue
70+
}
71+
patchRules (webpackConfig) {
72+
const { module: { rules }, plugins } = webpackConfig
73+
if (this.useVue(plugins)) {
5074
let vueUses
5175
let index
5276
assert.doesNotThrow(
@@ -92,36 +116,17 @@ module.exports = class VueFlagsWebpackPlugin {
92116
if (!compiler.hooks) {
93117
throw genError('webpack < 4 is not supported')
94118
}
95-
const { plugins, mode, context, module: moduleOption, watchOptions } = compiler.options
119+
const { plugins, mode, context, watchOptions } = compiler.options
96120
if (plugins.filter(p => p.constructor === VueFlagsWebpackPlugin).length > 1) {
97-
throw genError('This plugin can not be used multiple times')
121+
throw genError('Sorry, this plugin can not be used multiple times')
98122
}
99123
this.pluginOptions = setOptions(this.options, context, watchOptions, mode === 'development')
100124
postcssFlagsPlugin.pluginOptions = this.pluginOptions
101125

102126
// patch webpack rules
103-
let vueLoaderPath
104-
let VueLoaderPlugin
105-
let vueTemplateCompilerVersion
106-
try {
107-
vueLoaderPath = require.resolve('vue-loader')
108-
VueLoaderPlugin = require('vue-loader/lib/plugin')
109-
vueTemplateCompilerVersion = require('vue-template-compiler/package.json').version
110-
} catch (err) {
111-
if (vueLoaderPath && !VueLoaderPlugin) {
112-
throw genError('vue-loader < 15 is not supported, ' + err.message)
113-
}
114-
}
115-
const vuePlugin = VueLoaderPlugin && plugins.find(p => p.constructor === VueLoaderPlugin)
116-
if (vuePlugin) {
117-
const semver = require('semver')
118-
if (semver.lt(vueTemplateCompilerVersion, MIN_VERSION)) {
119-
throw genError(`vue-template-compiler < ${MIN_VERSION} is not supported`)
120-
}
121-
}
122127
compiler.hooks.afterEnvironment.tap(PLUGIN_NAME, () => {
123128
// ensure patching rules after `VueLoaderPlugin`
124-
this.patchRules(moduleOption.rules, !!vuePlugin)
129+
this.patchRules(compiler.options)
125130
})
126131

127132
// apply flag variable plugin

lib/utils.js

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class VueFlagsWebpackPluginError extends Error {
77
}
88
}
99

10-
exports.VueFlagsWebpackPluginError = VueFlagsWebpackPluginError
11-
1210
exports.genError = function genError (msg) {
1311
return new VueFlagsWebpackPluginError(msg)
1412
}

test/webpack-build.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const clearModule = require('clear-module')
55

66
const rfp = Module._resolveFilename
77
const mmap = [
8-
'webpack', 'vue-loader', 'vue-loader/lib/plugin', 'vue-template-compiler/package.json'
8+
'webpack', 'vue-loader', 'vue-loader/lib/plugin', 'vue-template-compiler', 'vue-template-compiler/package.json'
99
].reduce((k1, k2) => {
1010
k1[k2] = require.resolve(k2)
1111
return k1

0 commit comments

Comments
 (0)