-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathloader.ts
More file actions
53 lines (49 loc) · 1.44 KB
/
loader.ts
File metadata and controls
53 lines (49 loc) · 1.44 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { writeFile } from 'node:fs/promises'
import { dirname, relative } from 'node:path'
import { codeExtract, exportClassMap, exportSheet } from '@devup-ui/wasm'
import type { RawLoaderDefinitionFunction } from 'webpack'
export interface DevupUILoaderOptions {
package: string
cssFile: string
sheetFile: string
classMapFile: string
watch: boolean
}
const devupUILoader: RawLoaderDefinitionFunction<DevupUILoaderOptions> =
function (source) {
const {
watch,
package: libPackage,
cssFile,
sheetFile,
classMapFile,
} = this.getOptions()
const callback = this.async()
const id = this.resourcePath
try {
const { code, css, map } = codeExtract(
id,
source.toString(),
libPackage,
relative(dirname(this.resourcePath), cssFile).replaceAll('\\', '/'),
)
if (css && watch) {
const content = `${this.resourcePath} ${Date.now()}`
if (this._compiler) (this._compiler as any).__DEVUP_CACHE = content
// should be reset css
Promise.all([
writeFile(cssFile, `/* ${content} */`),
writeFile(sheetFile, exportSheet()),
writeFile(classMapFile, exportClassMap()),
])
.catch(console.error)
.finally(() => callback(null, code, map))
return
}
callback(null, code, map)
} catch (error) {
callback(error as Error)
}
return
}
export default devupUILoader