Skip to content

Commit faf6919

Browse files
committed
save
1 parent 2e9330a commit faf6919

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

lib/component-normalizer.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
module.exports = function normalizeComponent (
55
rawScriptExports,
66
compiledTemplate,
7-
scopeId,
8-
cssModules
7+
injectStyles,
8+
scopeId
99
) {
1010
var esModule
1111
var scriptExports = rawScriptExports = rawScriptExports || {}
@@ -28,21 +28,32 @@ module.exports = function normalizeComponent (
2828
options.staticRenderFns = compiledTemplate.staticRenderFns
2929
}
3030

31+
if (injectStyles) {
32+
var injected = false
33+
var cssModules
34+
var inject = function () {
35+
if (!injected) {
36+
injected = true
37+
cssModules = injectStyles()
38+
}
39+
// inject cssModules
40+
if (cssModules) {
41+
for (var key in cssModules) {
42+
this[key] = cssModules[key]
43+
}
44+
}
45+
}
46+
var existing = options.beforeCreate
47+
options.beforeCreate = existing
48+
? [].concat(existing, inject)
49+
: [inject]
50+
}
51+
3152
// scopedId
3253
if (scopeId) {
3354
options._scopeId = scopeId
3455
}
3556

36-
// inject cssModules
37-
if (cssModules) {
38-
var computed = Object.create(options.computed || null)
39-
Object.keys(cssModules).forEach(function (key) {
40-
var module = cssModules[key]
41-
computed[key] = function () { return module }
42-
})
43-
options.computed = computed
44-
}
45-
4657
return {
4758
esModule: esModule,
4859
exports: scriptExports,

lib/loader.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ module.exports = function (content) {
286286

287287
// add requires for styles
288288
if (parts.styles.length) {
289-
output += '\n/* styles */\n'
289+
var styleInjectionCode = 'function __injectVueStyle__ () {\n'
290290
var hasModules = false
291291
parts.styles.forEach(function (style, i) {
292292
// require style
@@ -302,11 +302,11 @@ module.exports = function (content) {
302302
}
303303
if (!hasModules) {
304304
hasModules = true
305-
output += 'var cssModules = {}\n'
305+
styleInjectionCode += 'var cssModules = {}\n'
306306
}
307307
if (moduleName in cssModules) {
308308
loaderContext.emitError('CSS module name "' + moduleName + '" is not unique!')
309-
output += requireString
309+
styleInjectionCode += requireString
310310
} else {
311311
cssModules[moduleName] = true
312312

@@ -317,21 +317,25 @@ module.exports = function (content) {
317317
requireString += '.locals'
318318
}
319319

320-
output += 'cssModules["' + moduleName + '"] = ' + requireString + '\n'
320+
styleInjectionCode += 'cssModules["' + moduleName + '"] = ' + requireString + '\n'
321321
}
322322
} else {
323-
output += requireString + '\n'
323+
styleInjectionCode += requireString + '\n'
324324
}
325325
})
326-
output += '\n'
326+
if (hasModules) {
327+
styleInjectionCode += `return cssModules`
328+
}
329+
styleInjectionCode += '}\n'
330+
output += styleInjectionCode
327331
}
328332

329333
// we require the component normalizer function, and call it like so:
330334
// normalizeComponent(
331335
// scriptExports,
332336
// compiledTemplate,
333-
// scopeId,
334-
// cssModules
337+
// injectStyles,
338+
// scopeId
335339
// )
336340
output += 'var Component = require(' +
337341
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
@@ -365,19 +369,13 @@ module.exports = function (content) {
365369
}
366370
output += ',\n'
367371

372+
// style
373+
output += ' /* styles */\n '
374+
output += (parts.styles.length ? '__injectVueStyle__' : 'null') + ',\n'
375+
368376
// scopeId
369377
output += ' /* scopeId */\n '
370-
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + ',\n'
371-
372-
// cssModules
373-
output += ' /* cssModules */\n '
374-
if (cssModules) {
375-
// inject style modules as computed properties
376-
output += 'cssModules'
377-
} else {
378-
output += 'null'
379-
}
380-
output += '\n'
378+
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + '\n'
381379

382380
// close normalizeComponent call
383381
output += ')\n'

test/test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ function test (options, assert) {
7272
console.log(err[0].data.error.stack)
7373
expect(err).to.be.null
7474
}
75-
assert(window, interopDefault(window.vueModule), window.vueModule)
75+
const module = interopDefault(window.vueModule)
76+
const instance = {}
77+
if (module.beforeCreate) {
78+
module.beforeCreate.call(instance)
79+
}
80+
assert(window, module, window.vueModule, instance)
7681
}
7782
})
7883
})
@@ -458,11 +463,9 @@ describe('vue-loader', function () {
458463
localIdentName: localIdentName
459464
}
460465
}
461-
}, (window) => {
462-
var module = window.vueModule
463-
466+
}, (window, module, raw, instance) => {
464467
// get local class name
465-
var className = module.computed.style().red
468+
var className = instance.style.red
466469
expect(className).to.match(regexToMatch)
467470

468471
// class name in style
@@ -480,7 +483,7 @@ describe('vue-loader', function () {
480483
expect(style).to.contain('animation: ' + animationName + ' 1s;')
481484

482485
// default module + pre-processor + scoped
483-
var anotherClassName = module.computed.$style().red
486+
var anotherClassName = instance.$style.red
484487
expect(anotherClassName).to.match(regexToMatch).and.not.equal(className)
485488
var id = 'data-v-' + hash('vue-loader/test/fixtures/css-modules.vue')
486489
expect(style).to.contain('.' + anotherClassName + '[' + id + ']')
@@ -514,7 +517,10 @@ describe('vue-loader', function () {
514517
}
515518

516519
var output = requireFromString(code, './test.build.js')
517-
expect(output.computed.style().red).to.exist
520+
var mockInstance = {}
521+
522+
output.beforeCreate.call(mockInstance)
523+
expect(mockInstance.style.red).to.exist
518524

519525
done()
520526
})

0 commit comments

Comments
 (0)