11import { existsSync , mkdirSync , readFileSync , writeFileSync } from 'node:fs'
22import { join , relative , resolve } from 'node:path'
33
4+ import { exportClassMap , exportFileMap , exportSheet } from '@devup-ui/wasm'
45import {
56 DevupUIWebpackPlugin ,
67 type DevupUIWebpackPluginOptions ,
78} from '@devup-ui/webpack-plugin'
89import { type NextConfig } from 'next'
910
11+ import { preload } from './preload'
12+
1013type DevupUiNextPluginOptions = Omit <
1114 Partial < DevupUIWebpackPluginOptions > ,
1215 'watch'
@@ -26,9 +29,9 @@ export function DevupUI(
2629 process . env . TURBOPACK === '1' || process . env . TURBOPACK === 'auto'
2730 // turbopack is now stable, TURBOPACK is set to auto without any flags
2831 if ( isTurbo ) {
29- if ( process . env . NODE_ENV === 'production' ) {
30- throw new Error ( 'Devup UI is not supported in production with turbopack' )
31- }
32+ // if (process.env.NODE_ENV === 'production') {
33+ // throw new Error('Devup UI is not supported in production with turbopack')
34+ // }
3235
3336 config ??= { }
3437 config . turbopack ??= { }
@@ -39,6 +42,7 @@ export function DevupUI(
3942 cssDir = resolve ( distDir , 'devup-ui' ) ,
4043 singleCss = false ,
4144 devupFile = 'devup.json' ,
45+ include = [ ] ,
4246 } = options
4347
4448 const sheetFile = join ( distDir , 'sheet.json' )
@@ -54,35 +58,56 @@ export function DevupUI(
5458 recursive : true ,
5559 } )
5660 if ( ! existsSync ( gitignoreFile ) ) writeFileSync ( gitignoreFile , '*' )
61+ const theme = existsSync ( devupFile )
62+ ? JSON . parse ( readFileSync ( devupFile , 'utf-8' ) ) ?. [ 'theme' ]
63+ : undefined
5764 // disable turbo parallel
58- process . env . TURBOPACK_DEBUG_JS = '*'
59- process . env . NODE_OPTIONS ??= ''
60- process . env . NODE_OPTIONS += ' --inspect-brk'
65+ const excludeRegex = new RegExp (
66+ `node_modules(?!.*(${ [ '@devup-ui' , ...include ]
67+ . join ( '|' )
68+ . replaceAll ( '/' , '[\\/\\\\_]' ) } )([\\/\\\\.]|$))`,
69+ )
70+
71+ if ( process . env . NODE_ENV !== 'production' ) {
72+ process . env . TURBOPACK_DEBUG_JS = '*'
73+ process . env . NODE_OPTIONS ??= ''
74+ process . env . NODE_OPTIONS += ' --inspect-brk'
75+ } else {
76+ preload ( excludeRegex , libPackage , singleCss , theme , cssDir )
77+ }
6178
6279 const rules : NonNullable < typeof config . turbopack . rules > = {
6380 [ `./${ relative ( process . cwd ( ) , cssDir ) . replaceAll ( '\\' , '/' ) } /*.css` ] : [
6481 {
6582 loader : '@devup-ui/webpack-plugin/css-loader' ,
6683 } ,
6784 ] ,
68- '*.{tsx,ts,js,mjs}' : [
69- {
70- loader : '@devup-ui/webpack-plugin/loader' ,
71- options : {
72- package : libPackage ,
73- cssDir,
74- sheetFile,
75- classMapFile,
76- fileMapFile,
77- watch : process . env . NODE_ENV === 'development' ,
78- singleCss,
79- // for turbopack, load theme is required on loader
80- theme : existsSync ( devupFile )
81- ? JSON . parse ( readFileSync ( devupFile , 'utf-8' ) ) ?. [ 'theme' ]
82- : undefined ,
85+ '*.{tsx,ts,js,mjs}' : {
86+ loaders : [
87+ {
88+ loader : '@devup-ui/webpack-plugin/loader' ,
89+ options : {
90+ package : libPackage ,
91+ cssDir,
92+ sheetFile,
93+ classMapFile,
94+ fileMapFile,
95+ defaultSheet : JSON . parse ( exportSheet ( ) ) ,
96+ defaultClassMap : JSON . parse ( exportClassMap ( ) ) ,
97+ defaultFileMap : JSON . parse ( exportFileMap ( ) ) ,
98+ watch : process . env . NODE_ENV === 'development' ,
99+ singleCss,
100+ // for turbopack, load theme is required on loader
101+ theme,
102+ } ,
103+ } ,
104+ ] ,
105+ condition : {
106+ not : {
107+ path : excludeRegex ,
83108 } ,
84109 } ,
85- ] ,
110+ } ,
86111 }
87112 Object . assign ( config . turbopack . rules , rules )
88113 return config
0 commit comments