-
Notifications
You must be signed in to change notification settings - Fork 915
/
Copy pathstylePostLoader.ts
36 lines (30 loc) · 1.07 KB
/
stylePostLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as qs from 'querystring'
import type { LoaderDefinitionFunction } from 'webpack'
import { compiler } from './compiler'
const { compileStyle } = 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))
// skip normal CSS files
if (!('vue' in query) || query.type !== 'style' || !query.id) {
this.callback(null, source, inMap)
return
}
const { code, map, errors } = compileStyle({
source: source as string,
filename: this.resourcePath,
id: `data-v-${query.id}`,
map: inMap as any,
scoped: !!query.scoped,
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)
}
}
export default StylePostLoader