Skip to content

Commit 894686a

Browse files
committed
refactor
1 parent 9795614 commit 894686a

File tree

9 files changed

+175
-155
lines changed

9 files changed

+175
-155
lines changed

lib/load-postcss-config.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/loader.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var querystring = require('querystring')
77

88
// internal lib loaders
99
var selectorPath = normalize.lib('selector')
10-
var styleRewriterPath = normalize.lib('style-rewriter')
11-
var templateLoaderPath = normalize.lib('template-loader')
12-
var templateCompilerPath = normalize.lib('template-compiler')
10+
var styleCompilerPath = normalize.lib('style-compiler/index')
11+
var templateCompilerPath = normalize.lib('template-compiler/index')
12+
var templatePreprocessorPath = normalize.lib('template-compiler/preprocessor')
1313
var componentNormalizerPath = normalize.lib('component-normalizer')
1414

1515
// dep loaders
@@ -190,9 +190,9 @@ module.exports = function (content) {
190190
var lang = part.lang || defaultLang[type]
191191
var loader = loaders[lang]
192192

193-
var styleRewriter = ''
193+
var styleCompiler = ''
194194
if (type === 'styles') {
195-
styleRewriter = styleRewriterPath + '?' + JSON.stringify({
195+
styleCompiler = styleCompilerPath + '?' + JSON.stringify({
196196
id: moduleId,
197197
scoped: !!scoped,
198198
hasInlineConfig: !!query.postcss
@@ -215,10 +215,10 @@ module.exports = function (content) {
215215
// inject rewriter before css loader for extractTextPlugin use cases
216216
if (rewriterInjectRE.test(loader)) {
217217
loader = loader.replace(rewriterInjectRE, function (m, $1) {
218-
return ensureBang($1) + styleRewriter
218+
return ensureBang($1) + styleCompiler
219219
})
220220
} else {
221-
loader = ensureBang(loader) + styleRewriter
221+
loader = ensureBang(loader) + styleCompiler
222222
}
223223
}
224224
// if user defines custom loaders for html, add template compiler to it
@@ -230,10 +230,10 @@ module.exports = function (content) {
230230
// unknown lang, infer the loader to be used
231231
switch (type) {
232232
case 'template':
233-
return defaultLoaders.html + '!' + templateLoaderPath + '?raw&engine=' + lang + '!'
233+
return defaultLoaders.html + '!' + templatePreprocessorPath + '?raw&engine=' + lang + '!'
234234
case 'styles':
235235
loader = addCssModulesToLoader(defaultLoaders.css, part, index)
236-
return loader + '!' + styleRewriter + ensureBang(ensureLoader(lang))
236+
return loader + '!' + styleCompiler + ensureBang(ensureLoader(lang))
237237
case 'script':
238238
return injectString + ensureBang(ensureLoader(lang))
239239
default:

lib/style-rewriter.js renamed to lib/style-compiler/index.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,14 @@ module.exports = function (css, map) {
5959
}
6060

6161
// use the same config loading interface as postcss-loader
62-
loadPostcssConfig().then(config => {
63-
var plugins
64-
var options
65-
66-
// inline postcss options for vue-loader
67-
var rawInlineOptions = vueOptions.postcss
68-
if (typeof rawInlineOptions === 'function') {
69-
rawInlineOptions = rawInlineOptions.call(this, this)
70-
}
71-
if (Array.isArray(rawInlineOptions)) {
72-
plugins = rawInlineOptions
73-
} else if (isObject(rawInlineOptions)) {
74-
plugins = rawInlineOptions.plugins
75-
options = rawInlineOptions.options
76-
}
77-
78-
plugins = [trim].concat(plugins || [])
62+
loadPostcssConfig(vueOptions.postcss).then(({ plugins, options }) => {
63+
plugins = [trim].concat(plugins)
7964
options = Object.assign({
8065
to: this.resourcePath,
8166
from: this.resourcePath,
8267
map: false
8368
}, options)
8469

85-
// merge postcss config file
86-
if (config && config.plugins) {
87-
plugins = plugins.concat(config.plugins)
88-
}
89-
if (config && config.options) {
90-
options = Object.assign({}, config.options, options)
91-
}
92-
9370
// add plugin for vue-loader scoped css rewrite
9471
if (query.scoped) {
9572
plugins.push(addId({ id: query.id }))
@@ -110,20 +87,15 @@ module.exports = function (css, map) {
11087
}
11188
}
11289

113-
postcss(plugins)
90+
return postcss(plugins)
11491
.process(css, options)
11592
.then(function (result) {
11693
var map = result.map && result.map.toJSON()
11794
cb(null, result.css, map)
11895
return null // silence bluebird warning
11996
})
120-
.catch(function (e) {
121-
console.log(e)
122-
cb(e)
123-
})
97+
}).catch(e => {
98+
console.log(e)
99+
cb(e)
124100
})
125101
}
126-
127-
function isObject (val) {
128-
return val && typeof val === 'object'
129-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var load = require('postcss-load-config')
2+
3+
let loaded
4+
5+
function isObject (val) {
6+
return val && typeof val === 'object'
7+
}
8+
9+
module.exports = function loadPostcssConfig (inlineConfig) {
10+
if (process.env.VUE_LOADER_TEST || !loaded) {
11+
loaded = load({}, null, { argv: false }).catch(() => {
12+
// postcss-load-config throws error when no config file is found,
13+
// but for us it's optional.
14+
})
15+
}
16+
17+
return loaded.then(config => {
18+
var plugins = []
19+
var options = {}
20+
21+
// inline postcss options for vue-loader
22+
if (typeof inlineConfig === 'function') {
23+
inlineConfig = inlineConfig.call(this, this)
24+
}
25+
if (Array.isArray(inlineConfig)) {
26+
plugins = inlineConfig
27+
} else if (isObject(inlineConfig)) {
28+
plugins = inlineConfig.plugins || []
29+
options = inlineConfig.options || {}
30+
}
31+
32+
// merge postcss config file
33+
if (config && config.plugins) {
34+
plugins = plugins.concat(config.plugins)
35+
}
36+
if (config && config.options) {
37+
options = Object.assign({}, config.options, options)
38+
}
39+
40+
return {
41+
plugins,
42+
options
43+
}
44+
})
45+
}

lib/template-compiler.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

lib/template-compiler/index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var normalize = require('../normalize')
2+
var loaderUtils = require('loader-utils')
3+
var compiler = require('vue-template-compiler')
4+
var beautify = require('js-beautify').js_beautify
5+
var transpile = require('vue-template-es2015-compiler')
6+
var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
7+
var transformRequire = require('./modules/transform-require')
8+
var rewriteInlineStyles = require('./modules/inline-style')
9+
10+
module.exports = function (html) {
11+
this.cacheable()
12+
var cb = this.async()
13+
var isServer = this.target === 'node'
14+
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
15+
var vueOptions = this.options.__vueOptions__ || {}
16+
var options = loaderUtils.getOptions(this) || {}
17+
18+
Promise.all([
19+
transformRequire(options.transformToRequire),
20+
rewriteInlineStyles()
21+
]).then(defaultCompilerModules => {
22+
// user compiler modules
23+
var userCompilerModules = options.compilerModules || vueOptions.compilerModules
24+
// for HappyPack cross-process use cases
25+
if (typeof userCompilerModules === 'string') {
26+
userCompilerModules = require(userCompilerModules)
27+
}
28+
29+
var compiled = compiler.compile(html, {
30+
preserveWhitespace: options.preserveWhitespace,
31+
modules: defaultCompilerModules.concat(userCompilerModules || [])
32+
})
33+
34+
// tips
35+
if (compiled.tips && compiled.tips.length) {
36+
compiled.tips.forEach(tip => {
37+
this.emitWarning(tip)
38+
})
39+
}
40+
41+
var code
42+
if (compiled.errors && compiled.errors.length) {
43+
this.emitError(
44+
`\n Error compiling template:\n${pad(html)}\n` +
45+
compiled.errors.map(e => ` - ${e}`).join('\n') + '\n'
46+
)
47+
code = 'module.exports={render:function(){},staticRenderFns:[]}'
48+
} else {
49+
var bubleOptions = options.buble
50+
code = transpile('module.exports={' +
51+
'render:' + toFunction(compiled.render) + ',' +
52+
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
53+
'}', bubleOptions)
54+
// mark with stripped (this enables Vue to use correct runtime proxy detection)
55+
if (!isProduction && (
56+
!bubleOptions ||
57+
!bubleOptions.transforms ||
58+
bubleOptions.transforms.stripWith !== false
59+
)) {
60+
code += `\nmodule.exports.render._withStripped = true`
61+
}
62+
}
63+
// hot-reload
64+
if (!isServer && !isProduction) {
65+
code +=
66+
'\nif (module.hot) {\n' +
67+
' module.hot.accept()\n' +
68+
' if (module.hot.data) {\n' +
69+
' require("' + hotReloadAPIPath + '").rerender("' + options.id + '", module.exports)\n' +
70+
' }\n' +
71+
'}'
72+
}
73+
74+
cb(null, code)
75+
}).catch(cb)
76+
}
77+
78+
function toFunction (code) {
79+
return 'function (){' + beautify(code, {
80+
indent_size: 2 // eslint-disable-line camelcase
81+
}) + '}'
82+
}
83+
84+
function pad (html) {
85+
return html.split(/\r?\n/).map(line => ` ${line}`).join('\n')
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 1. load postcss config
2+
// 2. transform staticStyle?
3+
var loadPostcssConfig = require('../../style-compiler/load-postcss-config')
4+
5+
module.exports = () => {
6+
return loadPostcssConfig().then(config => {
7+
return {}
8+
})
9+
}

lib/template-compiler-modules/transform-require.js renamed to lib/template-compiler/modules/transform-require.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ var defaultOptions = {
44
image: 'xlink:href'
55
}
66

7-
module.exports = {
8-
_userOptions: null,
9-
postTransformNode: el => {
10-
var userOptions = module.exports._userOptions
11-
var options = userOptions
12-
? Object.assign({}, defaultOptions, userOptions)
13-
: defaultOptions
14-
for (var tag in options) {
15-
if (el.tag === tag && el.attrs) {
16-
var attributes = options[tag]
17-
if (typeof attributes === 'string') {
18-
el.attrs.some(attr => rewrite(attr, attributes))
19-
} else if (Array.isArray(attributes)) {
20-
attributes.forEach(item => el.attrs.some(attr => rewrite(attr, item)))
21-
}
7+
module.exports = userOptions => {
8+
var options = userOptions
9+
? Object.assign({}, defaultOptions, userOptions)
10+
: defaultOptions
11+
12+
return {
13+
postTransformNode: node => {
14+
transform(node, options)
15+
}
16+
}
17+
}
18+
19+
function transform (node, options) {
20+
for (var tag in options) {
21+
if (node.tag === tag && node.attrs) {
22+
var attributes = options[tag]
23+
if (typeof attributes === 'string') {
24+
node.attrs.some(attr => rewrite(attr, attributes))
25+
} else if (Array.isArray(attributes)) {
26+
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
2227
}
2328
}
2429
}
File renamed without changes.

0 commit comments

Comments
 (0)