Skip to content

Commit c75837c

Browse files
committed
simplify parser
1 parent e75c0f4 commit c75837c

File tree

4 files changed

+90
-100
lines changed

4 files changed

+90
-100
lines changed

lib/loader.js

Lines changed: 78 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var loaderUtils = require('loader-utils')
22
var assign = require('object-assign')
3+
var parse = require('./parser')
34
var selectorPath = require.resolve('./selector')
4-
var parserPath = require.resolve('./parser')
55
var hash = require('hash-sum')
66
var path = require('path')
77

@@ -26,7 +26,6 @@ var rewriters = {
2626
module.exports = function (content) {
2727
var self = this
2828
this.cacheable()
29-
var cb = this.async()
3029
var options = this.options.vue || {}
3130
var query = loaderUtils.parseQuery(this.query)
3231
var vueUrl = loaderUtils.getRemainingRequest(this)
@@ -39,8 +38,8 @@ module.exports = function (content) {
3938
defaultLoaders.js = 'babel-loader'
4039
}
4140

42-
// check if there are custom loaders specified with
43-
// vueLoader.withLoaders(), otherwise use defaults
41+
// check if there are custom loaders specified via
42+
// webpack config, otherwise use defaults
4443
var loaders = assign({}, defaultLoaders, options.loaders)
4544

4645
function getRequire (type, part, index, scoped) {
@@ -131,94 +130,86 @@ module.exports = function (content) {
131130
}
132131
}
133132

134-
var url = '!!' + parserPath + '!' + vueUrl
135-
this.loadModule(url, function (err, source) {
136-
if (err) return cb(err)
137-
138-
// up to this part, what we have done is basically executing
139-
// parser.js on the raw vue file and get the parsing result
140-
// which is an object that contains info about the vue file.
141-
var parts = self.exec(source, url)
142-
var hasLocalStyles = false
143-
var output = 'var __vue_script__, __vue_template__\n'
144-
145-
// add requires for src imports
146-
parts.styleImports.forEach(function (impt) {
147-
if (impt.scoped) hasLocalStyles = true
148-
output += getRequireForImport('style', impt, impt.scoped)
149-
})
150-
151-
// add requires for styles
152-
parts.style.forEach(function (style, i) {
153-
if (style.scoped) hasLocalStyles = true
154-
output += getRequire('style', style, i, style.scoped)
155-
})
156-
157-
// add require for script
158-
var script
159-
if (parts.script.length) {
160-
script = parts.script[0]
161-
output +=
162-
'__vue_script__ = ' + (
163-
script.src
164-
? getRequireForImport('script', script, 0)
165-
: getRequire('script', script, 0)
166-
)
167-
}
133+
var parts = parse(content)
134+
var hasLocalStyles = false
135+
var output = 'var __vue_script__, __vue_template__\n'
168136

169-
// add require for template
170-
var template
171-
if (parts.template.length) {
172-
template = parts.template[0]
173-
output += '__vue_template__ = ' + (
174-
template.src
175-
? getRequireForImport('template', template, hasLocalStyles)
176-
: getRequire('template', template, 0, hasLocalStyles)
177-
)
178-
}
137+
// add requires for src imports
138+
parts.styleImports.forEach(function (impt) {
139+
if (impt.scoped) hasLocalStyles = true
140+
output += getRequireForImport('style', impt, impt.scoped)
141+
})
179142

180-
if (!query.inject) {
181-
// attach template
182-
output +=
183-
'module.exports = __vue_script__ || {}\n' +
184-
'if (module.exports.__esModule) module.exports = module.exports.default\n' +
185-
'if (__vue_template__) { (typeof module.exports === "function" ? module.exports.options : module.exports).template = __vue_template__ }\n'
186-
// hot reload
187-
if (
188-
process.env.NODE_ENV !== 'production' &&
189-
(parts.script.length || parts.template.length)
190-
) {
191-
output +=
192-
'if (module.hot) {(function () {' +
193-
' module.hot.accept()\n' +
194-
' var hotAPI = require("vue-hot-reload-api")\n' +
195-
' hotAPI.install(require("vue"), true)\n' +
196-
' if (!hotAPI.compatible) return\n' +
197-
' var id = ' + JSON.stringify(filePath) + '\n' +
198-
' if (!module.hot.data) {\n' +
199-
// initial insert
200-
' hotAPI.createRecord(id, module.exports)\n' +
201-
' } else {\n' +
202-
// update
203-
' hotAPI.update(id, module.exports, __vue_template__)\n' +
204-
' }\n' +
205-
'})()}'
206-
}
207-
} else {
143+
// add requires for styles
144+
parts.style.forEach(function (style, i) {
145+
if (style.scoped) hasLocalStyles = true
146+
output += getRequire('style', style, i, style.scoped)
147+
})
148+
149+
// add require for script
150+
var script
151+
if (parts.script.length) {
152+
script = parts.script[0]
153+
output +=
154+
'__vue_script__ = ' + (
155+
script.src
156+
? getRequireForImport('script', script, 0)
157+
: getRequire('script', script, 0)
158+
)
159+
}
160+
161+
// add require for template
162+
var template
163+
if (parts.template.length) {
164+
template = parts.template[0]
165+
output += '__vue_template__ = ' + (
166+
template.src
167+
? getRequireForImport('template', template, hasLocalStyles)
168+
: getRequire('template', template, 0, hasLocalStyles)
169+
)
170+
}
171+
172+
if (!query.inject) {
173+
// attach template
174+
output +=
175+
'module.exports = __vue_script__ || {}\n' +
176+
'if (module.exports.__esModule) module.exports = module.exports.default\n' +
177+
'if (__vue_template__) { (typeof module.exports === "function" ? module.exports.options : module.exports).template = __vue_template__ }\n'
178+
// hot reload
179+
if (
180+
process.env.NODE_ENV !== 'production' &&
181+
(parts.script.length || parts.template.length)
182+
) {
208183
output +=
209-
'module.exports = function (injections) {\n' +
210-
' var mod = __vue_script__\n' +
211-
' ? __vue_script__(injections)\n' +
212-
' : {}\n' +
213-
' if (mod.__esModule) mod = mod.default\n' +
214-
' if (__vue_template__) { (typeof mod === "function" ? mod.options : mod).template = __vue_template__ }\n' +
215-
' return mod\n' +
216-
'}'
184+
'if (module.hot) {(function () {' +
185+
' module.hot.accept()\n' +
186+
' var hotAPI = require("vue-hot-reload-api")\n' +
187+
' hotAPI.install(require("vue"), true)\n' +
188+
' if (!hotAPI.compatible) return\n' +
189+
' var id = ' + JSON.stringify(filePath) + '\n' +
190+
' if (!module.hot.data) {\n' +
191+
// initial insert
192+
' hotAPI.createRecord(id, module.exports)\n' +
193+
' } else {\n' +
194+
// update
195+
' hotAPI.update(id, module.exports, __vue_template__)\n' +
196+
' }\n' +
197+
'})()}'
217198
}
199+
} else {
200+
output +=
201+
'module.exports = function (injections) {\n' +
202+
' var mod = __vue_script__\n' +
203+
' ? __vue_script__(injections)\n' +
204+
' : {}\n' +
205+
' if (mod.__esModule) mod = mod.default\n' +
206+
' if (__vue_template__) { (typeof mod === "function" ? mod.options : mod).template = __vue_template__ }\n' +
207+
' return mod\n' +
208+
'}'
209+
}
218210

219-
// done
220-
cb(null, output)
221-
})
211+
// done
212+
return output
222213
}
223214

224215
module.exports.withLoaders = function () {

lib/parser.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
var parse5 = require('parse5')
22
var parser = new parse5.Parser(null, { locationInfo: true })
3+
var cache = require('lru-cache')(100)
34

45
module.exports = function (content) {
5-
this.cacheable()
66

7-
var output = {
7+
var output = cache.get(content)
8+
if (output) return output
9+
10+
output = {
811
template: [],
912
style: [],
1013
script: [],
@@ -91,7 +94,8 @@ module.exports = function (content) {
9194
})
9295
})
9396

94-
return 'module.exports = ' + JSON.stringify(output)
97+
cache.set(content, output)
98+
return output
9599
}
96100

97101
function commentScript (content, lang) {

lib/selector.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1+
var parse = require('./parser')
12
var loaderUtils = require('loader-utils')
23

3-
module.exports = function () {
4+
module.exports = function (content) {
45
this.cacheable()
5-
var cb = this.async()
66
var query = loaderUtils.parseQuery(this.query)
7-
8-
var self = this
9-
var url = '!!' + require.resolve('./parser.js') + '!' + this.resource
10-
this.loadModule(url, function (err, source) {
11-
if (err) return cb(err)
12-
var parts = self.exec(source, url)
13-
cb(null, parts[query.type][query.index].content)
14-
})
7+
var parts = parse(content)
8+
return parts[query.type][query.index].content
159
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"autoprefixer": "^6.0.3",
3333
"hash-sum": "^1.0.2",
3434
"loader-utils": "^0.2.10",
35+
"lru-cache": "^2.7.0",
3536
"object-assign": "^4.0.0",
3637
"parse5": "^1.5.0",
3738
"postcss": "^5.0.10",

0 commit comments

Comments
 (0)