-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathplugin.ts
More file actions
121 lines (110 loc) · 3.04 KB
/
plugin.ts
File metadata and controls
121 lines (110 loc) · 3.04 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
existsSync,
mkdirSync,
readFileSync,
stat,
writeFileSync,
} from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { getCss, getThemeInterface, registerTheme } from '@devup-ui/wasm'
import { type Compiler } from 'webpack'
const _filename = fileURLToPath(import.meta.url)
const _dirname = dirname(_filename)
export interface DevupUIWebpackPluginOptions {
package: string
cssFile: string
devupPath: string
interfacePath: string
}
export class DevupUIWebpackPlugin {
options: DevupUIWebpackPluginOptions
watch = false
constructor({
package: libPackage = '@devup-ui/react',
cssFile = join(_dirname, 'devup-ui.css'),
devupPath = 'devup.json',
interfacePath = '.df',
}: Partial<DevupUIWebpackPluginOptions>) {
this.options = {
package: libPackage,
cssFile,
devupPath,
interfacePath,
}
}
writeDataFiles() {
registerTheme(
JSON.parse(readFileSync(this.options.devupPath, 'utf-8'))?.['theme'],
)
const interfaceCode = getThemeInterface(
this.options.package,
'DevupThemeColors',
'DevupThemeTypography',
)
if (interfaceCode) {
if (!existsSync(this.options.interfacePath))
mkdirSync(this.options.interfacePath)
writeFileSync(
join(this.options.interfacePath, 'theme.d.ts'),
interfaceCode,
{
encoding: 'utf-8',
},
)
}
writeFileSync(this.options.cssFile, getCss(), {
encoding: 'utf-8',
})
}
apply(compiler: Compiler) {
// read devup.json
const existsDevup = existsSync(this.options.devupPath)
if (existsDevup) {
try {
this.writeDataFiles()
} catch (error) {
console.error(error)
}
compiler.hooks.afterCompile.tap('DevupUIWebpackPlugin', (compilation) => {
compilation.fileDependencies.add(this.options.devupPath)
})
}
let lastModifiedTime: number | null = null
compiler.hooks.watchRun.tapAsync('DevupUIWebpackPlugin', (_, callback) => {
this.watch = true
if (existsDevup)
stat(this.options.devupPath, (err, stats) => {
if (err) {
console.error(`Error checking ${this.options.devupPath}:`, err)
return callback()
}
const modifiedTime = stats.mtimeMs
if (lastModifiedTime && lastModifiedTime !== modifiedTime) {
this.writeDataFiles()
}
lastModifiedTime = modifiedTime
callback()
})
})
// Create an empty CSS file
if (!existsSync(this.options.cssFile)) {
writeFileSync(this.options.cssFile, '', { encoding: 'utf-8' })
}
compiler.options.module.rules.push({
test: /\.(tsx|ts|js|mjs|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: createRequire(import.meta.url).resolve(
'@devup-ui/webpack-plugin/loader',
),
options: {
plugin: this,
},
},
],
})
}
}