@@ -45,8 +45,32 @@ module.exports = class VueFlagsWebpackPlugin {
45
45
}
46
46
}
47
47
}
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 )
49
61
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 ) ) {
50
74
let vueUses
51
75
let index
52
76
assert . doesNotThrow (
@@ -92,36 +116,17 @@ module.exports = class VueFlagsWebpackPlugin {
92
116
if ( ! compiler . hooks ) {
93
117
throw genError ( 'webpack < 4 is not supported' )
94
118
}
95
- const { plugins, mode, context, module : moduleOption , watchOptions } = compiler . options
119
+ const { plugins, mode, context, watchOptions } = compiler . options
96
120
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' )
98
122
}
99
123
this . pluginOptions = setOptions ( this . options , context , watchOptions , mode === 'development' )
100
124
postcssFlagsPlugin . pluginOptions = this . pluginOptions
101
125
102
126
// 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
- }
122
127
compiler . hooks . afterEnvironment . tap ( PLUGIN_NAME , ( ) => {
123
128
// ensure patching rules after `VueLoaderPlugin`
124
- this . patchRules ( moduleOption . rules , ! ! vuePlugin )
129
+ this . patchRules ( compiler . options )
125
130
} )
126
131
127
132
// apply flag variable plugin
0 commit comments