Skip to content

Commit d0910ff

Browse files
author
Franck Freiburger
committed
wip(core): handle asynchronous style processing
fix #149
1 parent 7249907 commit d0910ff

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/createVue2SFCModule.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export async function createSFCModule(source : string, filename : AbstractPath,
8181
additionalBabelPlugins = {},
8282
customBlockHandler,
8383
devMode = false,
84-
createCJSModule
84+
createCJSModule,
85+
processStyles,
8586
} = options;
8687

8788
const descriptor = sfc_parse({
@@ -238,29 +239,36 @@ export async function createSFCModule(source : string, filename : AbstractPath,
238239

239240
for ( const descStyle of descriptor.styles ) {
240241

241-
const src = descStyle.src ? (await (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).getContentData(false)) as string : descStyle.content;
242+
const srcRaw = descStyle.src ? (await (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).getContentData(false)) as string : descStyle.content;
242243

243244
const style = await withCache(
244245
compiledCache,
245246
[
246247
componentHash,
247-
src,
248+
srcRaw,
248249
descStyle.lang
249250
],
250251
async ({ preventCache }) => {
251252

253+
const src = processStyles !== undefined ? await processStyles(srcRaw, descStyle.lang, filename, options) : srcRaw;
254+
255+
if ( src === undefined )
256+
preventCache();
257+
252258
const compileStyleOptions: StyleCompileOptions = {
253259
source: src,
254260
filename: strFilename,
255261
id: scopeId,
256262
scoped: descStyle.scoped !== undefined ? descStyle.scoped : false,
257263
trim: false,
258-
preprocessLang: descStyle.lang,
259-
preprocessOptions: {
264+
...processStyles === undefined ? {
265+
preprocessLang: descStyle.lang,
260266
preprocessOptions: {
261-
customRequire: (id: string) => moduleCache[id]
267+
preprocessOptions: {
268+
customRequire: (id: string) => moduleCache[id]
269+
}
262270
}
263-
}
271+
} : {},
264272
}
265273

266274
// Vue2 doesn't support preprocessCustomRequire, so we have to preprocess manually

src/createVue3SFCModule.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export async function createSFCModule(source : string, filename : AbstractPath,
8080
additionalBabelPlugins = {},
8181
customBlockHandler,
8282
devMode = false,
83-
createCJSModule
83+
createCJSModule,
84+
processStyles,
8485
} = options;
8586

8687
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
@@ -240,21 +241,27 @@ export async function createSFCModule(source : string, filename : AbstractPath,
240241

241242
for ( const descStyle of descriptor.styles ) {
242243

243-
// hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below
244-
if ( descStyle.lang )
245-
await loadModuleInternal({ refPath: filename, relPath: descStyle.lang }, options);
246-
247-
const src = descStyle.src ? (await (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).getContentData(false)) as string : descStyle.content;
248-
244+
const srcRaw = descStyle.src ? (await (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).getContentData(false)) as string : descStyle.content;
245+
249246
const style =
250247
await withCache(
251248
compiledCache,
252249
[
253250
componentHash,
254-
src
251+
srcRaw,
252+
descStyle.lang
255253
],
256254
async ({ preventCache }) => {
257255

256+
const src = processStyles !== undefined ? await processStyles(srcRaw, descStyle.lang, filename, options) : srcRaw;
257+
258+
if ( src === undefined )
259+
preventCache();
260+
261+
// hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below
262+
if ( processStyles === undefined && descStyle.lang !== undefined )
263+
await loadModuleInternal({ refPath: filename, relPath: descStyle.lang }, options);
264+
258265
// src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileStyle.ts#L70
259266
const compiledStyle = await sfc_compileStyleAsync({
260267
filename: descriptor.filename,
@@ -263,8 +270,10 @@ export async function createSFCModule(source : string, filename : AbstractPath,
263270
id: scopeId,
264271
scoped: descStyle.scoped,
265272
trim: true,
266-
preprocessLang: descStyle.lang as PreprocessLang,
267-
preprocessCustomRequire: id => moduleCache[id],
273+
...processStyles === undefined ? {
274+
preprocessLang: descStyle.lang as PreprocessLang,
275+
preprocessCustomRequire: id => moduleCache[id],
276+
} : {},
268277
});
269278

270279
if ( compiledStyle.errors.length ) {

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ export type Options = {
374374

375375

376376
/**
377-
*
377+
* creates a CommonJS module from JS source string.
378378
*/
379-
createCJSModule(refPath : AbstractPath, source : string, options : Options) : Module,
379+
createCJSModule?(refPath : AbstractPath, source : string, options : Options) : Module,
380380

381381

382382

@@ -422,6 +422,14 @@ export type Options = {
422422
* prevent minification, allow debugger statement,
423423
*/
424424
devMode?: boolean,
425+
426+
/**
427+
*
428+
* @param srcRaw
429+
* @param lang
430+
* @param filename
431+
*/
432+
processStyles(srcRaw : string, lang : string | undefined, filename : AbstractPath, options : Options) : Promise<string>,
425433

426434
}
427435

0 commit comments

Comments
 (0)