Skip to content

Commit 6659b04

Browse files
author
YiSiWang
committed
add SSR support with test case
1 parent e98edb2 commit 6659b04

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

lib/loader.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = function (content) {
5858
var bubleOptions = hasBuble && options.buble ? '?' + JSON.stringify(options.buble) : ''
5959
var defaultLoaders = {
6060
html: templateCompilerPath + '?id=' + moduleId,
61-
css: styleLoaderPath + '!css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
61+
css: (isServer ? '' : styleLoaderPath + '!') + 'css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
6262
js: hasBuble ? ('buble-loader' + bubleOptions) : hasBabel ? 'babel-loader' : ''
6363
}
6464

@@ -177,21 +177,33 @@ module.exports = function (content) {
177177
var cssModules = {}
178178

179179
// add requires for styles
180-
if (!isServer && parts.styles.length) {
180+
if (parts.styles.length) {
181181
output += '\n/* styles */\n'
182182
parts.styles.forEach(function (style, i) {
183183
/* !HACK! */
184184
style.module = i === 0 ? 'style' : '$style'
185+
186+
// require style
187+
if (isServer && !style.module) return
185188
var requireString = style.src
186189
? getRequireForImport('styles', style, style.scoped)
187190
: getRequire('styles', style, i, style.scoped)
191+
188192
// setCssModule
189193
if (style.module) {
190194
if (style.module in cssModules) {
191195
loaderContext.emitError('CSS module name "' + style.module + '" is not unique!')
192196
output += requireString
193197
} else {
194198
cssModules[style.module] = true
199+
200+
// `style-loader` exposes the name-to-hash map directly
201+
// `css-loader` exposes it in `.locals`
202+
// We drop `style-loader` in SSR, and add `.locals` here.
203+
if (isServer) {
204+
requireString += '.locals'
205+
}
206+
195207
output += '__vue_styles__["' + style.module + '"] = ' + requireString + '\n'
196208
}
197209
} else {

test/test.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,21 +427,23 @@ describe('vue-loader', function () {
427427
})
428428

429429
it.only('css-modules in SSR', function (done) {
430-
test({
430+
bundle({
431431
entry: './test/fixtures/css-modules.vue',
432-
vue: {
433-
target: 'node'
432+
target: 'node',
433+
output: Object.assign({}, globalConfig.output, {
434+
libraryTarget: 'commonjs2'
435+
})
436+
}, function (code) {
437+
// http://stackoverflow.com/questions/17581830/load-node-js-module-from-string-in-memory
438+
function requireFromString(src, filename) {
439+
var Module = module.constructor;
440+
var m = new Module();
441+
m._compile(src, filename);
442+
return m.exports;
434443
}
435-
}, function (window) {
436-
var module = window.vueModule
437-
438-
// class name
439-
var className = module.computed.style().red
440-
expect(className).to.match(/^_/)
441-
442-
// no css
443-
expect(window.document.querySelector('style')).to.not.exist()
444444

445+
var output = requireFromString(code, './test.build.js')
446+
expect(output.computed.style().red).to.match(/^_/)
445447
done()
446448
})
447449
})

0 commit comments

Comments
 (0)