Skip to content

Commit 29ddd49

Browse files
committed
Support webpack 3 module concatenation by using imports instead
of `require`s
1 parent ec4260f commit 29ddd49

File tree

1 file changed

+53
-23
lines changed

1 file changed

+53
-23
lines changed

lib/loader.js

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ module.exports = function (content) {
107107
')'
108108
}
109109

110+
function getImport (type, part, index, scoped) {
111+
return 'import __vue_' + type + '__ from ' + getRequireString(type, part, index, scoped)
112+
}
113+
110114
function getRequireString (type, part, index, scoped) {
111115
return loaderUtils.stringifyRequest(loaderContext,
112116
// disable all configuration loaders
@@ -126,6 +130,10 @@ module.exports = function (content) {
126130
')'
127131
}
128132

133+
function getImportForImport (type, impt, scoped) {
134+
return 'import __vue_' + type + '__ from ' + getRequireForImportString(type, impt, scoped)
135+
}
136+
129137
function getRequireForImportString (type, impt, scoped) {
130138
return loaderUtils.stringifyRequest(loaderContext,
131139
'!!' +
@@ -420,52 +428,74 @@ module.exports = function (content) {
420428
// scopeId,
421429
// moduleIdentifier (server only)
422430
// )
423-
output += 'var Component = require(' +
424-
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
425-
')(\n'
431+
if (options.esModule) {
432+
output += 'import normalizeComponent from ' +
433+
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
434+
'\n'
435+
} else {
436+
output += 'var normalizeComponent = require(' +
437+
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
438+
')\n'
439+
}
426440

427441
// <script>
428-
output += ' /* script */\n '
442+
output += '/* script */\n'
429443
var script = parts.script
430444
if (script) {
431-
output += script.src
432-
? getRequireForImport('script', script)
433-
: getRequire('script', script)
445+
if (options.esModule) {
446+
output += script.src
447+
? getImportForImport('script', script)
448+
: getImport('script', script) + '\n'
449+
} else {
450+
output += 'var __vue_script__ = ' + (script.src
451+
? getRequireForImport('script', script)
452+
: getRequire('script', script)) + '\n'
453+
}
434454
// inject loader interop
435455
if (query.inject) {
436-
output += '(injections)'
456+
output += '__vue_script__ = __vue_script__(injections)\n'
437457
}
438458
} else {
439-
output += 'null'
459+
output += 'var __vue_script__ = null\n'
440460
}
441-
output += ',\n'
442461

443462
// <template>
444-
output += ' /* template */\n '
463+
output += '/* template */\n'
445464
var template = parts.template
446465
if (template) {
447-
output += template.src
448-
? getRequireForImport('template', template)
449-
: getRequire('template', template)
466+
if (options.esModule) {
467+
output += (template.src
468+
? getImportForImport('template', template)
469+
: getImport('template', template)) + '\n'
470+
} else {
471+
output += 'var __vue_template__ = ' + (template.src
472+
? getRequireForImport('template', template)
473+
: getRequire('template', template)) + '\n'
474+
}
450475
} else {
451-
output += 'null'
476+
output += 'var __vue_template__ = null\n'
452477
}
453-
output += ',\n'
454478

455479
// style
456-
output += ' /* styles */\n '
457-
output += (parts.styles.length ? 'injectStyle' : 'null') + ',\n'
480+
output += '/* styles */\n'
481+
output += 'var __vue_styles__ = ' + (parts.styles.length ? 'injectStyle' : 'null') + '\n'
458482

459483
// scopeId
460-
output += ' /* scopeId */\n '
461-
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + ',\n'
484+
output += '/* scopeId */\n'
485+
output += 'var __vue_scopeId__ = ' + (hasScoped ? JSON.stringify(moduleId) : 'null') + '\n'
462486

463487
// moduleIdentifier (server only)
464-
output += ' /* moduleIdentifier (server only) */\n '
465-
output += (isServer ? JSON.stringify(hash(this.request)) : 'null') + '\n'
488+
output += '/* moduleIdentifier (server only) */\n'
489+
output += 'var __vue_module_identifier__ = ' + (isServer ? JSON.stringify(hash(this.request)) : 'null') + '\n'
466490

467491
// close normalizeComponent call
468-
output += ')\n'
492+
output += 'var Component = normalizeComponent(\n' +
493+
' __vue_script__,\n' +
494+
' __vue_template__,\n' +
495+
' __vue_styles__,\n' +
496+
' __vue_scopeId__,\n' +
497+
' __vue_module_identifier__\n' +
498+
')\n'
469499

470500
// development-only code
471501
if (!isProduction) {

0 commit comments

Comments
 (0)