Skip to content

Commit 8e38245

Browse files
committed
compatibility with HappyPack
1 parent 0afde0e commit 8e38245

File tree

5 files changed

+67
-31
lines changed

5 files changed

+67
-31
lines changed

docs/en/features/postcss.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Any CSS output processed by `vue-loader` is piped through [PostCSS](https://gith
44

55
## Using a Config File
66

7-
Starting in 11.0 `vue-loader` supports auto-loading the same PostCss config files supported by `postcss-loader`:
7+
Starting in 11.0 `vue-loader` supports auto-loading the same PostCss config files supported by [`postcss-loader`](https://github.com/postcss/postcss-loader#usage):
88

99
- `postcss.config.js`
1010
- `.postcssrc`

docs/en/options.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,22 @@ module.exports = {
8181

8282
### postcss
8383

84+
> Note: in >=11.0.0 it is recommended to use a PostCSS config file instead. [The usage is the same as `postcss-loader`](https://github.com/postcss/postcss-loader#usage).
85+
8486
- type: `Array` or `Function` or `Object`
8587

8688
Specify custom PostCSS plugins to be applied to CSS inside `*.vue` files. If using a function, the function will called using the same loader context and should return an Array of plugins.
8789

8890
``` js
8991
// ...
90-
vue: {
91-
// note: do not nest the `postcss` option under `loaders`
92-
postcss: [require('postcss-cssnext')()],
93-
loaders: {
94-
// ...
92+
{
93+
loader: 'vue-loader',
94+
options: {
95+
// note: do not nest the `postcss` option under `loaders`
96+
postcss: [require('postcss-cssnext')()],
97+
loaders: {
98+
// ...
99+
}
95100
}
96101
}
97102
```

lib/loader.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function getRawRequest (context) {
5050

5151
module.exports = function (content) {
5252
this.cacheable()
53-
var isServer = this.options.target === 'node'
53+
var isServer = this.target === 'node'
5454
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
5555

5656
var loaderContext = this
@@ -60,8 +60,9 @@ module.exports = function (content) {
6060

6161
var filePath = this.resourcePath
6262
var fileName = path.basename(filePath)
63-
var moduleId = 'data-v-' + genId(filePath, this._compiler.context)
64-
var styleRewriter = styleRewriterPath + '?id=' + moduleId
63+
64+
var context = (this._compiler && this._compiler.context) || this.options.context || process.cwd()
65+
var moduleId = 'data-v-' + genId(filePath, context)
6566

6667
var cssLoaderOptions = ''
6768
if (!isProduction && this.sourceMap && options.cssSourceMap !== false) {
@@ -75,8 +76,15 @@ module.exports = function (content) {
7576
? '?' + JSON.stringify(options.buble)
7677
: ''
7778

79+
var templateCompilerOptions = '?' + JSON.stringify({
80+
id: moduleId,
81+
transformToRequire: options.transformToRequire,
82+
preserveWhitespace: options.preserveWhitespace,
83+
buble: options.buble
84+
})
85+
7886
var defaultLoaders = {
79-
html: templateCompilerPath + '?id=' + moduleId,
87+
html: templateCompilerPath + templateCompilerOptions,
8088
css: styleLoaderPath + '!' + 'css-loader' + cssLoaderOptions,
8189
js: hasBuble ? ('buble-loader' + bubleOptions) : hasBabel ? 'babel-loader' : ''
8290
}
@@ -177,31 +185,42 @@ module.exports = function (content) {
177185
function getRawLoaderString (type, part, index, scoped) {
178186
var lang = part.lang || defaultLang[type]
179187
var loader = loaders[lang]
180-
var rewriter = type === 'styles' ? styleRewriter + (scoped ? '&scoped=true!' : '!') : ''
181-
var injectString = (type === 'script' && query.inject) ? 'inject-loader!' : ''
188+
189+
var styleRewriter = ''
190+
if (type === 'styles') {
191+
styleRewriter = styleRewriterPath + '?' + JSON.stringify({
192+
id: moduleId,
193+
scoped: !!scoped,
194+
hasInlineConfig: !!query.postcss
195+
}) + '!'
196+
}
197+
198+
var injectString = (type === 'script' && query.inject)
199+
? 'inject-loader!'
200+
: ''
201+
182202
if (loader != null) {
183203
if (Array.isArray(loader)) {
184204
loader = stringifyLoaders(loader)
185205
} else if (typeof loader === 'object') {
186206
loader = stringifyLoaders([loader])
187207
}
188-
// add css modules
189208
if (type === 'styles') {
209+
// add css modules
190210
loader = addCssModulesToLoader(loader, part, index)
211+
// inject rewriter before css loader for extractTextPlugin use cases
212+
if (rewriterInjectRE.test(loader)) {
213+
loader = loader.replace(rewriterInjectRE, function (m, $1) {
214+
return ensureBang($1) + styleRewriter
215+
})
216+
} else {
217+
loader = ensureBang(loader) + styleRewriter
218+
}
191219
}
192220
// if user defines custom loaders for html, add template compiler to it
193221
if (type === 'template' && loader.indexOf(defaultLoaders.html) < 0) {
194222
loader = defaultLoaders.html + '!' + loader
195223
}
196-
// inject rewriter before css/html loader for
197-
// extractTextPlugin use cases
198-
if (rewriterInjectRE.test(loader)) {
199-
loader = loader.replace(rewriterInjectRE, function (m, $1) {
200-
return ensureBang($1) + rewriter
201-
})
202-
} else {
203-
loader = ensureBang(loader) + rewriter
204-
}
205224
return injectString + ensureBang(loader)
206225
} else {
207226
// unknown lang, infer the loader to be used
@@ -210,7 +229,7 @@ module.exports = function (content) {
210229
return defaultLoaders.html + '!' + templateLoaderPath + '?raw&engine=' + lang + '!'
211230
case 'styles':
212231
loader = addCssModulesToLoader(defaultLoaders.css, part, index)
213-
return loader + '!' + rewriter + ensureBang(ensureLoader(lang))
232+
return loader + '!' + styleRewriter + ensureBang(ensureLoader(lang))
214233
case 'script':
215234
return injectString + ensureBang(ensureLoader(lang))
216235
default:

lib/style-rewriter.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ module.exports = function (css, map) {
4545
var query = loaderUtils.parseQuery(this.query)
4646
var vueOptions = this.options.__vueOptions__
4747

48+
if (!vueOptions) {
49+
if (query.hasInlineConfig) {
50+
this.emitError(
51+
`\n [vue-loader] It seems you are using HappyPack with inline postcss ` +
52+
`options for vue-loader. This is not supported because loaders running ` +
53+
`in different threads cannot share non-serializable options. ` +
54+
`It is recommended to use a postcss config file instead.\n` +
55+
`\n See http://vue-loader.vuejs.org/en/features/postcss.html#using-a-config-file for more details.\n`
56+
)
57+
}
58+
vueOptions = Object.assign({}, this.options.vue, this.vue)
59+
}
60+
4861
// use the same config loading interface as postcss-loader
4962
loadPostcssConfig({ webpack: this }).catch(() => {
5063
// postcss-load-config throws error when no config file is found,

lib/template-compiler.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ function rewrite (attr, name) {
4848

4949
module.exports = function (html) {
5050
this.cacheable()
51+
var isServer = this.target === 'node'
5152
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
52-
var query = loaderUtils.parseQuery(this.query)
53-
var isServer = this.options.target === 'node'
54-
var vueOptions = this.options.__vueOptions__
55-
if (vueOptions.transformToRequire) {
53+
var options = loaderUtils.parseQuery(this.query)
54+
if (options.transformToRequire) {
5655
transformToRequire = Object.assign(
5756
{},
5857
defaultTransformToRequire,
59-
vueOptions.transformToRequire
58+
options.transformToRequire
6059
)
6160
}
6261
var compiled = compiler.compile(html, Object.assign({
63-
preserveWhitespace: vueOptions.preserveWhitespace
62+
preserveWhitespace: options.preserveWhitespace
6463
}, defaultCompileOptions))
6564
var code
6665
if (compiled.errors.length) {
@@ -70,7 +69,7 @@ module.exports = function (html) {
7069
)
7170
code = 'module.exports={render:function(){},staticRenderFns:[]}'
7271
} else {
73-
var bubleOptions = vueOptions.buble
72+
var bubleOptions = options.buble
7473
code = transpile('module.exports={' +
7574
'render:' + toFunction(compiled.render) + ',' +
7675
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
@@ -92,7 +91,7 @@ module.exports = function (html) {
9291
'\nif (module.hot) {\n' +
9392
' module.hot.accept()\n' +
9493
' if (module.hot.data) {\n' +
95-
' require("' + hotReloadAPIPath + '").rerender("' + query.id + '", module.exports)\n' +
94+
' require("' + hotReloadAPIPath + '").rerender("' + options.id + '", module.exports)\n' +
9695
' }\n' +
9796
'}'
9897
}

0 commit comments

Comments
 (0)