@@ -5,38 +5,25 @@ const plugin = require('./plugin')
55const selectBlock = require ( './select' )
66const loaderUtils = require ( 'loader-utils' )
77const { attrsToQuery } = require ( './codegen/utils' )
8- const { parse } = require ( '@vue/component-compiler-utils' )
98const genStylesCode = require ( './codegen/styleInjection' )
109const { genHotReloadCode } = require ( './codegen/hotReload' )
1110const genCustomBlocksCode = require ( './codegen/customBlocks' )
1211const componentNormalizerPath = require . resolve ( './runtime/componentNormalizer' )
1312const { NS } = require ( './plugin' )
13+ const { resolveCompiler } = require ( './compiler' )
1414
1515let errorEmitted = false
1616
17- function loadTemplateCompiler ( loaderContext ) {
18- try {
19- return require ( 'vue-template-compiler' )
20- } catch ( e ) {
21- if ( / v e r s i o n m i s m a t c h / . test ( e . toString ( ) ) ) {
22- loaderContext . emitError ( e )
23- } else {
24- loaderContext . emitError ( new Error (
25- `[vue-loader] vue-template-compiler must be installed as a peer dependency, ` +
26- `or a compatible compiler implementation must be passed via options.`
27- ) )
28- }
29- }
30- }
31-
3217module . exports = function ( source ) {
3318 const loaderContext = this
3419
3520 if ( ! errorEmitted && ! loaderContext [ 'thread-loader' ] && ! loaderContext [ NS ] ) {
36- loaderContext . emitError ( new Error (
37- `vue-loader was used without the corresponding plugin. ` +
38- `Make sure to include VueLoaderPlugin in your webpack config.`
39- ) )
21+ loaderContext . emitError (
22+ new Error (
23+ `vue-loader was used without the corresponding plugin. ` +
24+ `Make sure to include VueLoaderPlugin in your webpack config.`
25+ )
26+ )
4027 errorEmitted = true
4128 }
4229
@@ -59,14 +46,17 @@ module.exports = function (source) {
5946
6047 const isServer = target === 'node'
6148 const isShadow = ! ! options . shadowMode
62- const isProduction = options . productionMode || minimize || process . env . NODE_ENV === 'production'
49+ const isProduction =
50+ options . productionMode || minimize || process . env . NODE_ENV === 'production'
6351 const filename = path . basename ( resourcePath )
6452 const context = rootContext || process . cwd ( )
6553 const sourceRoot = path . dirname ( path . relative ( context , resourcePath ) )
6654
67- const descriptor = parse ( {
55+ const { compiler, templateCompiler } = resolveCompiler ( loaderContext )
56+
57+ const descriptor = compiler . parse ( {
6858 source,
69- compiler : options . compiler || loadTemplateCompiler ( loaderContext ) ,
59+ compiler : options . compiler || templateCompiler ,
7060 filename,
7161 sourceRoot,
7262 needMap : sourceMap
@@ -78,6 +68,7 @@ module.exports = function (source) {
7868 if ( incomingQuery . type ) {
7969 return selectBlock (
8070 descriptor ,
71+ options ,
8172 loaderContext ,
8273 incomingQuery ,
8374 ! ! options . appendExtension
@@ -93,19 +84,19 @@ module.exports = function (source) {
9384
9485 const id = hash (
9586 isProduction
96- ? ( shortFilePath + '\n' + source . replace ( / \r \n / g, '\n' ) )
87+ ? shortFilePath + '\n' + source . replace ( / \r \n / g, '\n' )
9788 : shortFilePath
9889 )
9990
10091 // feature information
10192 const hasScoped = descriptor . styles . some ( s => s . scoped )
102- const hasFunctional = descriptor . template && descriptor . template . attrs . functional
103- const needsHotReload = (
93+ const hasFunctional =
94+ descriptor . template && descriptor . template . attrs . functional
95+ const needsHotReload =
10496 ! isServer &&
10597 ! isProduction &&
106- ( descriptor . script || descriptor . template ) &&
98+ ( descriptor . script || descriptor . scriptSetup || descriptor . template ) &&
10799 options . hotReload !== false
108- )
109100
110101 // template
111102 let templateImport = `var render, staticRenderFns`
@@ -116,21 +107,20 @@ module.exports = function (source) {
116107 const scopedQuery = hasScoped ? `&scoped=true` : ``
117108 const attrsQuery = attrsToQuery ( descriptor . template . attrs )
118109 const query = `?vue&type=template${ idQuery } ${ scopedQuery } ${ attrsQuery } ${ inheritQuery } `
119- const request = templateRequest = stringifyRequest ( src + query )
110+ const request = ( templateRequest = stringifyRequest ( src + query ) )
120111 templateImport = `import { render, staticRenderFns } from ${ request } `
121112 }
122113
123114 // script
124115 let scriptImport = `var script = {}`
125- if ( descriptor . script ) {
126- const src = descriptor . script . src || resourcePath
127- const attrsQuery = attrsToQuery ( descriptor . script . attrs , 'js' )
116+ const { script, scriptSetup } = descriptor
117+ if ( script || scriptSetup ) {
118+ const src = ( script && ! scriptSetup && script . src ) || resourcePath
119+ const attrsQuery = attrsToQuery ( ( scriptSetup || script ) . attrs , 'js' )
128120 const query = `?vue&type=script${ attrsQuery } ${ inheritQuery } `
129121 const request = stringifyRequest ( src + query )
130- scriptImport = (
131- `import script from ${ request } \n` +
132- `export * from ${ request } ` // support named exports
133- )
122+ scriptImport =
123+ `import script from ${ request } \n` + `export * from ${ request } ` // support named exports
134124 }
135125
136126 // styles
@@ -147,7 +137,8 @@ module.exports = function (source) {
147137 )
148138 }
149139
150- let code = `
140+ let code =
141+ `
151142${ templateImport }
152143${ scriptImport }
153144${ stylesCode }
@@ -183,7 +174,9 @@ var component = normalizer(
183174 if ( ! isProduction ) {
184175 // Expose the file's full path in development, so that it can be opened
185176 // from the devtools.
186- code += `\ncomponent.options.__file = ${ JSON . stringify ( rawShortFilePath . replace ( / \\ / g, '/' ) ) } `
177+ code += `\ncomponent.options.__file = ${ JSON . stringify (
178+ rawShortFilePath . replace ( / \\ / g, '/' )
179+ ) } `
187180 } else if ( options . exposeFilename ) {
188181 // Libraries can opt-in to expose their components' filenames in production builds.
189182 // For security reasons, only expose the file's basename in production.
0 commit comments