Skip to content

Commit 1bc1cfd

Browse files
wip(core): handle script/style/template src attribute
1 parent 02296da commit 1bc1cfd

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/createVue2SFCModule.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export async function createSFCModule(source : string, filename : string, option
7777
const component = {};
7878

7979

80-
const { moduleCache, compiledCache, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
80+
const { moduleCache, compiledCache, pathHandlers: { resolve }, getFile, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
8181

8282
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
8383
const descriptor = sfc_parse({
@@ -106,7 +106,7 @@ export async function createSFCModule(source : string, filename : string, option
106106

107107
const compileTemplateOptions : TemplateCompileOptions = descriptor.template ? {
108108
// hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap
109-
source: descriptor.template.content,
109+
source: descriptor.template.src ? (await getFile(resolve(filename, descriptor.template.src))).content : descriptor.template.content,
110110
filename,
111111
compiler: vueTemplateCompiler as VueTemplateCompiler,
112112
compilerOptions: {
@@ -136,13 +136,15 @@ export async function createSFCModule(source : string, filename : string, option
136136

137137
// eg: https://github.com/vuejs/vue-loader/blob/v15.9.6/lib/index.js
138138

139-
const [ depsList, transformedScriptSource ] = await withCache(compiledCache, [ componentHash, descriptor.script.content ], async ({ preventCache }) => {
139+
const src = descriptor.script.src ? (await getFile(resolve(filename, descriptor.script.src))).content : descriptor.script.content;
140+
141+
const [ depsList, transformedScriptSource ] = await withCache(compiledCache, [ componentHash, src ], async ({ preventCache }) => {
140142

141143
const babelParserPlugins : babel_ParserPlugin[] = [];
142144

143145
let ast: t.File
144146
try {
145-
ast = babel_parse(descriptor.script.content, {
147+
ast = babel_parse(src, {
146148
// doc: https://babeljs.io/docs/en/babel-parser#options
147149
// if: https://github.com/babel/babel/blob/main/packages/babel-parser/typings/babel-parser.d.ts#L24
148150
plugins: [
@@ -161,7 +163,7 @@ export async function createSFCModule(source : string, filename : string, option
161163
const depsList = parseDeps(ast);
162164

163165
// doc: https://babeljs.io/docs/en/babel-core#transformfromastasync
164-
const transformedScript = await babel_transformFromAstAsync(ast, descriptor.script.content, {
166+
const transformedScript = await babel_transformFromAstAsync(ast, src, {
165167
sourceMaps: genSourcemap, // https://babeljs.io/docs/en/options#sourcemaps
166168
plugins: [ // https://babeljs.io/docs/en/options#plugins
167169
babelPluginTransformModulesCommonjs, // https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#options
@@ -183,7 +185,7 @@ export async function createSFCModule(source : string, filename : string, option
183185
if ( descriptor.template !== null ) {
184186
// compiler-sfc src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileTemplate.ts#L39
185187
// compileTemplate eg: https://github.com/vuejs/vue-loader/blob/next/src/templateLoader.ts#L33
186-
const [ templateDepsList, templateTransformedSource ] = await withCache(compiledCache, [ componentHash, descriptor.template.content ], async ({ preventCache }) => {
188+
const [ templateDepsList, templateTransformedSource ] = await withCache(compiledCache, [ componentHash, compileTemplateOptions.source ], async ({ preventCache }) => {
187189

188190
const template = sfc_compileTemplate(compileTemplateOptions);
189191
// "@vue/component-compiler-utils" does NOT assume any module system, and expose render in global scope.
@@ -227,11 +229,13 @@ export async function createSFCModule(source : string, filename : string, option
227229

228230
for ( const descStyle of descriptor.styles ) {
229231

230-
const style = await withCache(compiledCache, [ componentHash, descStyle.content ], async ({ preventCache }) => {
232+
const src = descStyle.src ? (await getFile(resolve(filename, descStyle.src))).content : descStyle.content;
233+
234+
const style = await withCache(compiledCache, [ componentHash, src, descStyle.lang ], async ({ preventCache }) => {
231235
// src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileStyle.ts#L70
232236

233237
const compileStyleOptions: StyleCompileOptions = {
234-
source: descStyle.content,
238+
source: src,
235239
filename,
236240
id: scopeId,
237241
scoped: descStyle.scoped,
@@ -254,7 +258,7 @@ export async function createSFCModule(source : string, filename : string, option
254258
preventCache();
255259
for ( const err of compiledStyle.errors ) {
256260

257-
log?.('error', 'SFC style', formatError(err, filename, descStyle.content));
261+
log?.('error', 'SFC style', formatError(err, filename, source));
258262
}
259263
}
260264

src/createVue3SFCModule.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export async function createSFCModule(source : string, filename : string, option
7878
const component = {};
7979

8080

81-
const { delimiters, moduleCache, compiledCache, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
81+
const { delimiters, moduleCache, compiledCache, pathHandlers: { resolve }, getFile, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
8282

8383
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
8484
const { descriptor, errors } = sfc_parse(source, {
@@ -102,7 +102,7 @@ export async function createSFCModule(source : string, filename : string, option
102102
const compileTemplateOptions : SFCTemplateCompileOptions = descriptor.template ? {
103103
// hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap
104104
compiler: { ...vue_CompilerDOM, compile: (template, options) => vue_CompilerDOM.compile(template, { ...options, sourceMap: genSourcemap }) },
105-
source: descriptor.template.content,
105+
source: descriptor.template.src ? (await getFile(resolve(filename, descriptor.template.src))).content : descriptor.template.content,
106106
filename: descriptor.filename,
107107
isProd,
108108
scoped: hasScoped,
@@ -121,6 +121,13 @@ export async function createSFCModule(source : string, filename : string, option
121121

122122
// eg: https://github.com/vuejs/vue-loader/blob/6ed553f70b163031457acc961901313390cde9ef/src/index.ts#L136
123123

124+
// TBD: check if this is the right solution
125+
if ( descriptor.script.src )
126+
descriptor.script.content = (await getFile(resolve(filename, descriptor.script.src))).content;
127+
128+
// TBD: handle <script setup scr="...
129+
130+
124131
const [ depsList, transformedScriptSource ] = await withCache(compiledCache, [ componentHash, descriptor.script?.content, descriptor.scriptSetup?.content ], async ({ preventCache }) => {
125132

126133
const babelParserPlugins : babel_ParserPlugin[] = [];
@@ -193,7 +200,7 @@ export async function createSFCModule(source : string, filename : string, option
193200
if ( descriptor.template !== null ) {
194201
// compiler-sfc src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileTemplate.ts#L39
195202
// compileTemplate eg: https://github.com/vuejs/vue-loader/blob/next/src/templateLoader.ts#L33
196-
const [ templateDepsList, templateTransformedSource ] = await withCache(compiledCache, [ componentHash, descriptor.template.content ], async ({ preventCache }) => {
203+
const [ templateDepsList, templateTransformedSource ] = await withCache(compiledCache, [ componentHash, compileTemplateOptions.source ], async ({ preventCache }) => {
197204

198205
const template = sfc_compileTemplate(compileTemplateOptions);
199206

@@ -230,12 +237,14 @@ export async function createSFCModule(source : string, filename : string, option
230237
if ( descStyle.lang )
231238
await loadModule(descStyle.lang, options);
232239

233-
const style = await withCache(compiledCache, [ componentHash, descStyle.content ], async ({ preventCache }) => {
240+
const src = descStyle.src ? (await getFile(resolve(filename, descStyle.src))).content : descStyle.content;
241+
242+
const style = await withCache(compiledCache, [ componentHash, src ], async ({ preventCache }) => {
234243

235244
// src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileStyle.ts#L70
236245
const compiledStyle = await sfc_compileStyleAsync({
237246
filename: descriptor.filename,
238-
source: descStyle.content,
247+
source: src,
239248
isProd,
240249
id: scopeId,
241250
scoped: descStyle.scoped,

0 commit comments

Comments
 (0)