Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 513fe46

Browse files
committed
fix: use import statements for style when assembling in esModule mode
1 parent f136528 commit 513fe46

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/assemble.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,27 @@ module.exports = function assemble (source, filename, config) {
5353

5454
let cssModules
5555
if (styles.length) {
56-
let styleInjectionCode = `function ${INJECT_STYLE_FN} (ssrContext) {\n`
56+
let styleInjectionCode = ''
5757

58-
if (needsHotReload) styleInjectionCode += `if (${DISPOSED}) return`
59-
if (config.isServer) styleInjectionCode += ' var i\n'
58+
if (needsHotReload) styleInjectionCode += `if (${DISPOSED}) return\n`
6059

6160
styles.forEach((style, i) => {
62-
const invokeStyle = config.isServer && config.hasStyleInjectFn
63-
? code => ` ;i=${code},i.__inject__&&i.__inject__(ssrContext),i)\n`
64-
: code => ` ${code}\n`
65-
61+
const IMPORT_NAME = `__vue_style_${i}__`
62+
const IMPORT_STRING = _s(style.id)
6663
const moduleName = (style.descriptor.module === true) ? '$style' : style.descriptor.module
67-
const requireString = `require(${_s(style.id)})`
64+
const needsStyleInjection = config.isServer && config.hasStyleInjectFn
65+
const needsNamedImport = needsStyleInjection || typeof moduleName === 'string'
66+
const runInjection = needsStyleInjection ? `${IMPORT_NAME} && ${IMPORT_NAME}.__inject__ && ${IMPORT_NAME}.__inject__(ssrContext)\n` : ''
67+
68+
if (needsNamedImport) {
69+
output += config.esModule
70+
? `import ${IMPORT_NAME} from ${IMPORT_STRING}\n`
71+
: `const ${IMPORT_NAME} = requrie(${IMPORT_STRING})\n`
72+
} else {
73+
output += config.esModule
74+
? `import ${IMPORT_STRING}\n`
75+
: `require(${IMPORT_STRING})\n`
76+
}
6877

6978
if (moduleName) {
7079
if (!cssModules) {
@@ -77,16 +86,16 @@ module.exports = function assemble (source, filename, config) {
7786
config.onWarn({
7887
message: 'CSS module name "' + moduleName + '" is not unique!'
7988
})
80-
styleInjectionCode += invokeStyle(requireString)
89+
styleInjectionCode += runInjection
8190
} else {
8291
cssModules[moduleName] = true
8392
const MODULE_KEY = _s(moduleName)
8493

8594
if (!needsHotReload) {
86-
styleInjectionCode += invokeStyle(`this[${MODULE_KEY}] = ${requireString}`)
95+
styleInjectionCode += runInjection + `this[${MODULE_KEY}] = ${IMPORT_NAME}\n`
8796
} else {
88-
styleInjectionCode +=
89-
invokeStyle(`${CSS_MODULES}[${MODULE_KEY}] = ${requireString}`) +
97+
styleInjectionCode += runInjection +
98+
`${CSS_MODULES}[${MODULE_KEY}] = ${IMPORT_NAME}\n` +
9099
`Object.defineProperty(this, ${MODULE_KEY}, { get: function () { return ${CSS_MODULES}[${MODULE_KEY}] }})\n`
91100

92101
output +=
@@ -95,7 +104,7 @@ module.exports = function assemble (source, filename, config) {
95104
` var oldLocals = ${CSS_MODULES}[${MODULE_KEY}]\n` +
96105
` if (!oldLocals) return\n` +
97106
// 2. re-import (side effect: updates the <style>)
98-
` var newLocals = ${requireString}\n` +
107+
` var newLocals = require(${IMPORT_STRING})\n` +
99108
// 3. compare new and old locals to see if selectors changed
100109
` if (JSON.stringify(newLocals) === JSON.stringify(oldLocals)) return\n` +
101110
// 4. locals changed. Update and force re-render.
@@ -105,12 +114,9 @@ module.exports = function assemble (source, filename, config) {
105114
}
106115
}
107116
} else {
108-
styleInjectionCode += invokeStyle(requireString)
109117
}
110118
})
111-
112-
styleInjectionCode += '}\n'
113-
output += styleInjectionCode
119+
output += `function ${INJECT_STYLE_FN} (ssrContext) {\n` + pad(styleInjectionCode) + `}\n`
114120
}
115121

116122
// we require the component normalizer function, and call it like so:
@@ -242,5 +248,5 @@ module.exports = function assemble (source, filename, config) {
242248
}
243249

244250
function pad (content) {
245-
return content.split('\n').map(line => ' ' + line).join('\n')
251+
return content.trim().split('\n').map(line => ' ' + line).join('\n') + '\n'
246252
}

0 commit comments

Comments
 (0)