Skip to content

Commit 289b4d9

Browse files
committed
refactor: webpack 4 compat + improve test setup
1 parent 41436e4 commit 289b4d9

File tree

9 files changed

+125
-208
lines changed

9 files changed

+125
-208
lines changed

lib/loader.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,20 @@ module.exports = function (content) {
5353
const isProduction = this.minimize || process.env.NODE_ENV === 'production'
5454

5555
const loaderContext = this
56-
const query = loaderUtils.getOptions(this) || {}
57-
const options = Object.assign(
58-
{
59-
esModule: true
60-
},
61-
this.options.vue,
62-
this.vue,
63-
query
64-
)
56+
const options = Object.assign({ esModule: true }, loaderUtils.getOptions(this))
6557

6658
// shadow mode is an internal option
6759
// enabled via vue-cli's --target web-component
6860
const isShadowMode = !!options.shadowMode
6961

7062
// disable esModule in inject mode
7163
// because import/export must be top-level
72-
if (query.inject) {
64+
if (options.inject) {
7365
options.esModule = false
7466
}
7567

76-
// #824 avoid multiple webpack runs complaining about unknown option
77-
Object.defineProperty(this.options, '__vueOptions__', {
68+
// share options between the main loader of style/template loaders
69+
Object.defineProperty(this._compilation, '__vueOptions__', {
7870
value: options,
7971
enumerable: false,
8072
configurable: true
@@ -85,8 +77,8 @@ module.exports = function (content) {
8577
const fileName = path.basename(filePath)
8678

8779
const context =
88-
(this._compiler && this._compiler.context) ||
89-
this.options.context ||
80+
this.rootContext ||
81+
(this.options && this.options.context) ||
9082
process.cwd()
9183
const sourceRoot = path.dirname(path.relative(context, filePath))
9284
const shortFilePath = path.relative(context, filePath).replace(/^(\.\.[\\\/])+/, '')
@@ -135,14 +127,7 @@ module.exports = function (content) {
135127
id: moduleId,
136128
hasScoped,
137129
hasComment,
138-
transformToRequire: options.transformToRequire,
139-
preserveWhitespace: options.preserveWhitespace,
140-
buble: bubleTemplateOptions,
141-
// only pass compilerModules if it's a path string
142-
compilerModules:
143-
typeof options.compilerModules === 'string'
144-
? options.compilerModules
145-
: undefined
130+
buble: bubleTemplateOptions
146131
})
147132

148133
const defaultLoaders = {
@@ -300,7 +285,7 @@ module.exports = function (content) {
300285
'\n'
301286
}
302287
// inject loader interop
303-
if (query.inject) {
288+
if (options.inject) {
304289
output += '__vue_script__ = __vue_script__(injections)\n'
305290
}
306291
} else {
@@ -423,7 +408,7 @@ module.exports = function (content) {
423408
output += '\n'
424409
}
425410

426-
if (!query.inject) {
411+
if (!options.inject) {
427412
// hot reload
428413
if (needsHotReload) {
429414
output +=
@@ -615,7 +600,7 @@ module.exports = function (content) {
615600
vue: true,
616601
id: moduleId,
617602
scoped: !!scoped,
618-
hasInlineConfig: !!query.postcss
603+
sourceMap: cssSourceMap
619604
}) +
620605
'!'
621606
// normalize scss/sass/postcss if no specific loaders have been provided
@@ -636,7 +621,7 @@ module.exports = function (content) {
636621
: loaders[lang]
637622

638623
const injectString =
639-
type === 'script' && query.inject ? 'inject-loader!' : ''
624+
type === 'script' && options.inject ? 'inject-loader!' : ''
640625

641626
if (loader != null) {
642627
if (Array.isArray(loader)) {

lib/style-compiler/index.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,10 @@ const scopeId = require('./plugins/scope-id')
88
module.exports = function (css, map) {
99
this.cacheable()
1010
const cb = this.async()
11+
const loaderOptions = loaderUtils.getOptions(this) || {}
12+
const inlineConfig = (this._compilation.__vueOptions__ || {}).postcss
1113

12-
const query = loaderUtils.getOptions(this) || {}
13-
let vueOptions = this.options.__vueOptions__
14-
15-
if (!vueOptions) {
16-
if (query.hasInlineConfig) {
17-
this.emitError(
18-
`\n [vue-loader] It seems you are using HappyPack with inline postcss ` +
19-
`options for vue-loader. This is not supported because loaders running ` +
20-
`in different threads cannot share non-serializable options. ` +
21-
`It is recommended to use a postcss config file instead.\n` +
22-
`\n See http://vue-loader.vuejs.org/en/features/postcss.html#using-a-config-file for more details.\n`
23-
)
24-
}
25-
vueOptions = Object.assign({}, this.options.vue, this.vue)
26-
}
27-
28-
loadPostcssConfig(this, vueOptions.postcss)
14+
loadPostcssConfig(this, inlineConfig)
2915
.then(config => {
3016
const plugins = config.plugins.concat(trim)
3117
const options = Object.assign(
@@ -38,18 +24,12 @@ module.exports = function (css, map) {
3824
)
3925

4026
// add plugin for vue-loader scoped css rewrite
41-
if (query.scoped) {
42-
plugins.push(scopeId({ id: query.id }))
27+
if (loaderOptions.scoped) {
28+
plugins.push(scopeId({ id: loaderOptions.id }))
4329
}
4430

4531
// source map
46-
if (
47-
this.sourceMap &&
48-
!this.minimize &&
49-
vueOptions.cssSourceMap !== false &&
50-
process.env.NODE_ENV !== 'production' &&
51-
!options.map
52-
) {
32+
if (loaderOptions.sourceMap && !options.map) {
5333
options.map = {
5434
inline: false,
5535
annotation: false,

lib/style-compiler/load-postcss-config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
2626
if (err.message.indexOf('No PostCSS Config found') >= 0) {
2727
return
2828
}
29-
const friendlyErr = new Error(`Error loading PostCSS config: ${err.message}`)
30-
Error.captureStackTrace(friendlyErr, err)
31-
loaderContext.emitError(friendlyErr)
29+
loaderContext.emitWarning(`Error loading PostCSS config: ${err.message}`)
3230
})
3331
}
3432

lib/template-compiler/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@ module.exports = function (html) {
1111
this.cacheable()
1212
const isServer = this.target === 'node'
1313
const isProduction = this.minimize || process.env.NODE_ENV === 'production'
14-
const vueOptions = this.options.__vueOptions__ || {}
14+
const vueOptions = this._compilation.__vueOptions__ || {}
1515
const options = loaderUtils.getOptions(this) || {}
1616
const needsHotReload = !isServer && !isProduction && vueOptions.hotReload !== false
1717
const defaultModules = [transformRequire(options.transformToRequire), transformSrcset()]
18-
let userModules = vueOptions.compilerModules || options.compilerModules
19-
20-
// for HappyPack cross-process use cases
21-
if (typeof userModules === 'string') {
22-
userModules = require(userModules)
23-
}
2418

2519
const compilerOptions = {
26-
preserveWhitespace: options.preserveWhitespace,
27-
modules: defaultModules.concat(userModules || []),
28-
directives:
29-
vueOptions.compilerDirectives || options.compilerDirectives || {},
20+
preserveWhitespace: vueOptions.preserveWhitespace,
21+
modules: defaultModules.concat(vueOptions.compilerModules || []),
22+
directives: vueOptions.compilerDirectives || {},
3023
scopeId: options.hasScoped ? options.id : null,
3124
comments: options.hasComment
3225
}

lib/template-compiler/preprocessor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module.exports = function (content) {
2020
}
2121

2222
// allow passing options to the template preprocessor via `template` option
23-
if (this.options.__vueOptions__) {
24-
Object.assign(opt, this.options.__vueOptions__.template)
23+
if (this._compilation.__vueOptions__) {
24+
Object.assign(opt, this._compilation.__vueOptions__.template)
2525
}
2626

2727
// for relative includes

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@
7070
"css-loader": "^0.28.4",
7171
"eslint": "^3.19.0",
7272
"eslint-plugin-vue-libs": "^1.2.0",
73-
"expose-loader": "^0.7.1",
7473
"extract-text-webpack-plugin": "^3.0.0",
7574
"file-loader": "^1.1.5",
7675
"gitbook-plugin-edit-link": "^2.0.2",
7776
"gitbook-plugin-github": "^3.0.0",
7877
"gitbook-plugin-theme-vuejs": "^1.1.0",
79-
"yorkie": "^1.0.3",
8078
"inject-loader": "^3.0.1",
8179
"js-yaml": "^3.9.1",
8280
"jsdom": "^9.2.1",
@@ -98,6 +96,8 @@
9896
"vue": "^2.5.0",
9997
"vue-server-renderer": "^2.5.0",
10098
"vue-template-compiler": "^2.5.0",
101-
"webpack": "^3.7.1"
99+
"webpack": "^3.0.0",
100+
"webpack-merge": "^4.1.1",
101+
"yorkie": "^1.0.3"
102102
}
103103
}

test/fixtures/entry.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const Component = require('~target')
2+
3+
if (typeof window !== 'undefined') {
4+
window.vueModule = Component
5+
}
6+
7+
module.exports = Component

0 commit comments

Comments
 (0)