Skip to content

Commit 00ef1e5

Browse files
committed
feat: import component normalizer with esm
1 parent 436ac94 commit 00ef1e5

File tree

3 files changed

+117
-6
lines changed

3 files changed

+117
-6
lines changed

lib/loader.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const selectorPath = normalize.lib('selector')
1111
const styleCompilerPath = normalize.lib('style-compiler/index')
1212
const templateCompilerPath = normalize.lib('template-compiler/index')
1313
const templatePreprocessorPath = normalize.lib('template-compiler/preprocessor')
14-
const componentNormalizerPath = normalize.lib('component-normalizer')
14+
const componentNormalizerPath = normalize.lib('runtime/component-normalizer')
15+
const componentNormalizerESMPath = normalize.lib('runtime/component-normalizer.esm')
1516

1617
// dep loaders
1718
const styleLoaderPath = normalize.dep('vue-style-loader')
@@ -260,10 +261,17 @@ module.exports = function (content) {
260261
// scopeId,
261262
// moduleIdentifier (server only)
262263
// )
263-
output +=
264-
'var normalizeComponent = require(' +
265-
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
266-
')\n'
264+
if (!options.esModule) {
265+
output +=
266+
'var normalizeComponent = require(' +
267+
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
268+
')\n'
269+
} else {
270+
output +=
271+
`import normalizeComponent from ` +
272+
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerESMPath) +
273+
'\n'
274+
}
267275

268276
// <script>
269277
output += '/* script */\n'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* globals __VUE_SSR_CONTEXT__ */
2+
3+
// IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
4+
// This module is a runtime utility for cleaner component module output and will
5+
// be included in the final webpack user bundle.
6+
7+
export default function normalizeComponent (
8+
rawScriptExports,
9+
compiledTemplate,
10+
functionalTemplate,
11+
injectStyles,
12+
scopeId,
13+
moduleIdentifier /* server only */
14+
) {
15+
var esModule
16+
var scriptExports = rawScriptExports = rawScriptExports || {}
17+
18+
// ES6 modules interop
19+
var type = typeof rawScriptExports.default
20+
if (type === 'object' || type === 'function') {
21+
esModule = rawScriptExports
22+
scriptExports = rawScriptExports.default
23+
}
24+
25+
// Vue.extend constructor export interop
26+
var options = typeof scriptExports === 'function'
27+
? scriptExports.options
28+
: scriptExports
29+
30+
// render functions
31+
if (compiledTemplate) {
32+
options.render = compiledTemplate.render
33+
options.staticRenderFns = compiledTemplate.staticRenderFns
34+
options._compiled = true
35+
}
36+
37+
// functional template
38+
if (functionalTemplate) {
39+
options.functional = true
40+
}
41+
42+
// scopedId
43+
if (scopeId) {
44+
options._scopeId = scopeId
45+
}
46+
47+
var hook
48+
if (moduleIdentifier) { // server build
49+
hook = function (context) {
50+
// 2.3 injection
51+
context =
52+
context || // cached call
53+
(this.$vnode && this.$vnode.ssrContext) || // stateful
54+
(this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
55+
// 2.2 with runInNewContext: true
56+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
57+
context = __VUE_SSR_CONTEXT__
58+
}
59+
// inject component styles
60+
if (injectStyles) {
61+
injectStyles.call(this, context)
62+
}
63+
// register component module identifier for async chunk inferrence
64+
if (context && context._registeredComponents) {
65+
context._registeredComponents.add(moduleIdentifier)
66+
}
67+
}
68+
// used by ssr in case component is cached and beforeCreate
69+
// never gets called
70+
options._ssrRegister = hook
71+
} else if (injectStyles) {
72+
hook = injectStyles
73+
}
74+
75+
if (hook) {
76+
var functional = options.functional
77+
var existing = functional
78+
? options.render
79+
: options.beforeCreate
80+
81+
if (!functional) {
82+
// inject component registration as beforeCreate hook
83+
options.beforeCreate = existing
84+
? [].concat(existing, hook)
85+
: [hook]
86+
} else {
87+
// for template-only hot-reload because in that case the render fn doesn't
88+
// go through the normalizer
89+
options._injectStyles = hook
90+
// register for functioal component in vue file
91+
options.render = function renderWithStyleInjection (h, context) {
92+
hook.call(context)
93+
return existing(h, context)
94+
}
95+
}
96+
}
97+
98+
return {
99+
esModule: esModule,
100+
exports: scriptExports,
101+
options: options
102+
}
103+
}

lib/component-normalizer.js renamed to lib/runtime/component-normalizer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* globals __VUE_SSR_CONTEXT__ */
22

3-
// IMPORTANT: Do NOT use ES2015 features in this file.
3+
// IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
44
// This module is a runtime utility for cleaner component module output and will
55
// be included in the final webpack user bundle.
66

0 commit comments

Comments
 (0)