Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/stylePostLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import * as qs from 'querystring'
import type { LoaderDefinitionFunction } from 'webpack'
import { compiler } from './compiler'

const { compileStyle } = compiler
const { compileStyleAsync } = compiler

// This is a post loader that handles scoped CSS transforms.
// Injected right before css-loader by the global pitcher (../pitch.js)
// for any <style scoped> selection requests initiated from within vue files.
const StylePostLoader: LoaderDefinitionFunction = function (source, inMap) {
const query = qs.parse(this.resourceQuery.slice(1))
const callback = this.async()

// skip normal CSS files
if (!('vue' in query) || query.type !== 'style' || !query.id) {
this.callback(null, source, inMap)
return
}

const { code, map, errors } = compileStyle({
compileStyleAsync({
source: source as string,
filename: this.resourcePath,
id: `data-v-${query.id}`,
Expand All @@ -25,12 +26,16 @@ const StylePostLoader: LoaderDefinitionFunction = function (source, inMap) {
trim: true,
isProd: this.mode === 'production' || process.env.NODE_ENV === 'production',
})

if (errors.length) {
this.callback(errors[0])
} else {
this.callback(null, code, map as any)
}
.then(({ code, map, errors }) => {
if (errors.length) {
callback(errors[0])
} else {
callback(null, code, map as any)
}
})
.catch((error) => {
callback(error)
})
}

export default StylePostLoader