-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathplugin.ts
More file actions
228 lines (212 loc) · 6.56 KB
/
plugin.ts
File metadata and controls
228 lines (212 loc) · 6.56 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import {
existsSync,
mkdirSync,
readFileSync,
unlinkSync,
writeFileSync,
} from 'node:fs'
import { join, relative, resolve } from 'node:path'
import {
createNodeModulesExcludeRegex,
loadDevupConfigSync,
mergeImportAliases,
} from '@devup-ui/plugin-utils'
import {
exportClassMap,
exportFileMap,
exportSheet,
getCss,
getDefaultTheme,
getThemeInterface,
importClassMap,
importFileMap,
importSheet,
registerTheme,
setPrefix,
} from '@devup-ui/wasm'
import {
DevupUIWebpackPlugin,
type DevupUIWebpackPluginOptions,
} from '@devup-ui/webpack-plugin'
import { type NextConfig } from 'next'
import { startCoordinator } from './coordinator'
type DevupUiNextPluginOptions = Omit<
Partial<DevupUIWebpackPluginOptions>,
'watch'
>
/**
* Devup UI Next Plugin
* @param config
* @param options
* @constructor
*/
export function DevupUI(
config: NextConfig,
options: DevupUiNextPluginOptions = {},
): NextConfig {
const isTurbo =
process.env.TURBOPACK === '1' || process.env.TURBOPACK === 'auto'
// turbopack is now stable, TURBOPACK is set to auto without any flags
if (isTurbo) {
config ??= {}
config.turbopack ??= {}
config.turbopack.rules ??= {}
const {
package: libPackage = '@devup-ui/react',
distDir = 'df',
cssDir = resolve(distDir, 'devup-ui'),
singleCss = false,
devupFile = 'devup.json',
include = [],
prefix,
importAliases: userImportAliases,
} = options
if (prefix) {
setPrefix(prefix)
}
const importAliases = mergeImportAliases(userImportAliases)
const sheetFile = join(distDir, 'sheet.json')
const classMapFile = join(distDir, 'classMap.json')
const fileMapFile = join(distDir, 'fileMap.json')
const gitignoreFile = join(distDir, '.gitignore')
if (!existsSync(distDir))
mkdirSync(distDir, {
recursive: true,
})
if (!existsSync(cssDir))
mkdirSync(cssDir, {
recursive: true,
})
if (!existsSync(gitignoreFile)) writeFileSync(gitignoreFile, '*')
// Import previous session state to handle Turbopack persistent cache.
// When the dev server restarts, Turbopack may skip re-running loaders for
// unchanged files. Without importing previous state, the coordinator's WASM
// starts empty and CSS for cached files would be missing.
try {
importSheet(JSON.parse(readFileSync(sheetFile, 'utf-8')))
importClassMap(JSON.parse(readFileSync(classMapFile, 'utf-8')))
importFileMap(JSON.parse(readFileSync(fileMapFile, 'utf-8')))
} catch {
// No previous session state (first run) or corrupt files — start fresh
}
const devupConfig = loadDevupConfigSync(devupFile)
const theme: any = devupConfig.theme ?? {}
// Register current theme after importing previous state,
// since importSheet replaces the entire sheet including its theme.
registerTheme(theme)
const themeInterface = getThemeInterface(
libPackage,
'CustomColors',
'DevupThemeTypography',
'DevupTheme',
)
if (themeInterface) {
writeFileSync(join(distDir, 'theme.d.ts'), themeInterface)
}
// disable turbo parallel
const excludeRegex = createNodeModulesExcludeRegex(include, '.mdx.[tj]sx?$')
const coordinatorPortFile = join(distDir, 'coordinator.port')
// create devup-ui.css file
writeFileSync(join(cssDir, 'devup-ui.css'), getCss(null, false))
// Delete stale port file from previous session so loaders don't connect
// to a dead coordinator port. The new coordinator writes a fresh port file
// once it starts listening.
try {
unlinkSync(coordinatorPortFile)
} catch {
// Port file doesn't exist (first run) — safe to ignore
}
const coordinator = startCoordinator({
package: libPackage,
cssDir,
singleCss,
sheetFile,
classMapFile,
fileMapFile,
importAliases: importAliases as unknown as Record<string, string | null>,
coordinatorPortFile,
})
// Cleanup on exit
process.on('exit', () => {
coordinator.close()
})
const defaultSheet = JSON.parse(exportSheet())
const defaultClassMap = JSON.parse(exportClassMap())
const defaultFileMap = JSON.parse(exportFileMap())
// for theme script
const defaultTheme = getDefaultTheme()
if (defaultTheme) {
process.env.DEVUP_UI_DEFAULT_THEME = defaultTheme
config.env ??= {}
Object.assign(config.env, {
DEVUP_UI_DEFAULT_THEME: defaultTheme,
})
}
const rules: NonNullable<typeof config.turbopack.rules> = {
[`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [
{
loader: '@devup-ui/next-plugin/css-loader',
options: {
watch: process.env.NODE_ENV === 'development',
coordinatorPortFile,
sheetFile,
classMapFile,
fileMapFile,
themeFile: devupFile,
defaultSheet,
defaultClassMap,
defaultFileMap,
theme,
},
},
],
'*.{tsx,ts,js,mjs}': {
loaders: [
{
loader: '@devup-ui/next-plugin/loader',
options: {
package: libPackage,
cssDir,
coordinatorPortFile,
sheetFile,
classMapFile,
fileMapFile,
themeFile: devupFile,
defaultSheet,
defaultClassMap,
defaultFileMap,
watch: process.env.NODE_ENV === 'development',
singleCss,
// for turbopack, load theme is required on loader
theme,
importAliases: importAliases as unknown as Record<string, string>,
},
},
],
condition: {
not: {
path: excludeRegex,
},
},
},
}
Object.assign(config.turbopack.rules, rules)
return config
}
const { webpack } = config
config.webpack = (config, _options) => {
options.cssDir ??= resolve(
_options.dev ? (options.distDir ?? 'df') : '.next/cache',
`devup-ui_${_options.buildId}`,
)
config.plugins.push(
new DevupUIWebpackPlugin({
...options,
watch: _options.dev,
}),
)
if (typeof webpack === 'function') return webpack(config, _options)
return config
}
return config
}