-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathplugin.ts
More file actions
172 lines (164 loc) · 4.77 KB
/
plugin.ts
File metadata and controls
172 lines (164 loc) · 4.77 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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join, relative, resolve } from 'node:path'
import {
exportClassMap,
exportFileMap,
exportSheet,
getCss,
getDefaultTheme,
getThemeInterface,
registerTheme,
} from '@devup-ui/wasm'
import {
DevupUIWebpackPlugin,
type DevupUIWebpackPluginOptions,
} from '@devup-ui/webpack-plugin'
import { type NextConfig } from 'next'
import { preload } from './preload'
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 = [],
} = options
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, '*')
const theme = existsSync(devupFile)
? JSON.parse(readFileSync(devupFile, 'utf-8'))?.['theme']
: {}
registerTheme(theme)
const themeInterface = getThemeInterface(
libPackage,
'CustomColors',
'DevupThemeTypography',
'DevupTheme',
)
if (themeInterface) {
writeFileSync(join(distDir, 'theme.d.ts'), themeInterface)
}
// disable turbo parallel
const excludeRegex = new RegExp(
`(node_modules(?!.*(${['@devup-ui', ...include]
.join('|')
.replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`,
)
if (process.env.NODE_ENV !== 'production') {
// check if debugger is attached
fetch('http://localhost:' + process.env.PORT)
fetch('http://localhost:' + process.debugPort).catch(() => {
setTimeout(() => {
process.exit(77)
}, 500)
})
process.env.TURBOPACK_DEBUG_JS = '*'
process.env.NODE_OPTIONS ??= ''
process.env.NODE_OPTIONS += ' --inspect-brk'
// create devup-ui.css file
writeFileSync(join(cssDir, 'devup-ui.css'), getCss(null, false))
} else {
// build
preload(excludeRegex, libPackage, singleCss, cssDir)
}
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',
},
},
],
'*.{tsx,ts,js,mjs}': {
loaders: [
{
loader: '@devup-ui/next-plugin/loader',
options: {
package: libPackage,
cssDir,
sheetFile,
classMapFile,
fileMapFile,
themeFile: devupFile,
defaultSheet,
defaultClassMap,
defaultFileMap,
watch: process.env.NODE_ENV === 'development',
singleCss,
// for turbopack, load theme is required on loader
theme,
},
},
],
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
}