-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathplugin.ts
More file actions
150 lines (141 loc) · 4.09 KB
/
plugin.ts
File metadata and controls
150 lines (141 loc) · 4.09 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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join, relative, resolve } from 'node:path'
import {
exportClassMap,
exportFileMap,
exportSheet,
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) {
// if (process.env.NODE_ENV === 'production') {
// throw new Error('Devup UI is not supported in production with turbopack')
// }
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,
'DevupThemeColors',
'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('/', '[\\/\\\\_]')})([\\/\\\\.]|$))`,
)
if (process.env.NODE_ENV !== 'production') {
// dev
process.env.TURBOPACK_DEBUG_JS = '*'
process.env.NODE_OPTIONS ??= ''
process.env.NODE_OPTIONS += ' --inspect-brk'
} else {
// build
preload(excludeRegex, libPackage, singleCss, theme, cssDir)
}
const rules: NonNullable<typeof config.turbopack.rules> = {
[`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [
{
loader: '@devup-ui/next-plugin/css-loader',
},
],
'*.{tsx,ts,js,mjs}': {
loaders: [
{
loader: '@devup-ui/next-plugin/loader',
options: {
package: libPackage,
cssDir,
sheetFile,
classMapFile,
fileMapFile,
defaultSheet: JSON.parse(exportSheet()),
defaultClassMap: JSON.parse(exportClassMap()),
defaultFileMap: JSON.parse(exportFileMap()),
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
}