Skip to content

Commit acb4774

Browse files
committed
Implement turbopack dev
1 parent dfc1ecb commit acb4774

File tree

7 files changed

+35
-134
lines changed

7 files changed

+35
-134
lines changed

packages/next-plugin/package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
".": {
3535
"import": "./dist/index.js",
3636
"require": "./dist/index.cjs"
37-
},
38-
"./loader": {
39-
"import": "./dist/turbo/loader.js",
40-
"require": "./dist/turbo/loader.cjs"
41-
},
42-
"./css-loader": {
43-
"import": "./dist/turbo/css-loader.js",
44-
"require": "./dist/turbo/css-loader.cjs"
4537
}
4638
},
4739
"files": [

packages/next-plugin/src/plugin.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
1+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
22
import { join, relative, resolve } from 'node:path'
33

44
import {
@@ -24,6 +24,7 @@ export function DevupUI(
2424
): NextConfig {
2525
const isTurbo =
2626
process.env.TURBOPACK === '1' || process.env.TURBOPACK === 'auto'
27+
// turbopack is now stable, TURBOPACK is set to auto without any flags
2728
if (isTurbo) {
2829
config ??= {}
2930
config.turbopack ??= {}
@@ -33,27 +34,40 @@ export function DevupUI(
3334
distDir = 'df',
3435
cssDir = resolve(distDir, 'devup-ui'),
3536
singleCss = false,
37+
devupFile = 'devup.json',
3638
} = options
3739

3840
const sheetFile = join(distDir, 'sheet.json')
3941
const classMapFile = join(distDir, 'classMap.json')
4042
const fileMapFile = join(distDir, 'fileMap.json')
4143
const gitignoreFile = join(distDir, '.gitignore')
42-
if (!existsSync(distDir)) mkdirSync(distDir)
43-
if (!existsSync(cssDir)) mkdirSync(cssDir)
44+
if (!existsSync(distDir))
45+
mkdirSync(distDir, {
46+
recursive: true,
47+
})
48+
if (!existsSync(cssDir))
49+
mkdirSync(cssDir, {
50+
recursive: true,
51+
})
4452
if (!existsSync(gitignoreFile)) writeFileSync(gitignoreFile, '*')
53+
// disable turbo parallel
54+
process.env.TURBOPACK_LOADER_CPU = '1'
55+
56+
// will be removed after merge
57+
// https://github.com/vercel/next.js/pull/85268
58+
process.env.TURBOPACK_DEBUG_JS = 'webpack_loader'
59+
process.env.NODE_OPTIONS ??= ''
60+
process.env.NODE_OPTIONS += ' --inspect-brk'
61+
4562
const rules: NonNullable<typeof config.turbopack.rules> = {
4663
[`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [
4764
{
48-
loader: '@devup-ui/next-plugin/css-loader',
49-
options: {
50-
watch: process.env.NODE_ENV === 'development',
51-
},
65+
loader: '@devup-ui/webpack-plugin/css-loader',
5266
},
5367
],
5468
'*.{tsx,ts,js,mjs}': [
5569
{
56-
loader: '@devup-ui/next-plugin/loader',
70+
loader: '@devup-ui/webpack-plugin/loader',
5771
options: {
5872
package: libPackage,
5973
cssDir,
@@ -62,6 +76,10 @@ export function DevupUI(
6276
fileMapFile,
6377
watch: process.env.NODE_ENV === 'development',
6478
singleCss,
79+
// for turbopack, load theme is required on loader
80+
theme: existsSync(devupFile)
81+
? JSON.parse(readFileSync(devupFile, 'utf-8'))?.['theme']
82+
: undefined,
6583
},
6684
},
6785
],

packages/next-plugin/src/turbo/css-loader.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/next-plugin/src/turbo/loader.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

packages/next-plugin/vite.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export default defineConfig({
5656
formats: ['es', 'cjs'],
5757
entry: {
5858
index: 'src/index.ts',
59-
['turbo/loader']: 'src/turbo/loader.ts',
60-
['turbo/css-loader']: 'src/turbo/css-loader.ts',
6159
},
6260
},
6361
outDir: 'dist',

packages/webpack-plugin/src/css-loader.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ function getFileNumByFilename(filename: string) {
66
return parseInt(filename.split('devup-ui-')[1].split('.')[0])
77
}
88

9-
const devupUICssLoader: RawLoaderDefinitionFunction<{
10-
watch: boolean
11-
}> = function (_, map, meta) {
12-
const { watch } = this.getOptions()
9+
const devupUICssLoader: RawLoaderDefinitionFunction = function (_, map, meta) {
1310
const fileNum = getFileNumByFilename(this.resourcePath)
14-
if (!watch) return this.callback(null, getCss(fileNum, true))
1511
this.callback(null, getCss(fileNum, true), map, meta)
1612
}
1713
export default devupUICssLoader

packages/webpack-plugin/src/loader.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
exportFileMap,
88
exportSheet,
99
getCss,
10+
registerTheme,
1011
} from '@devup-ui/wasm'
1112
import type { RawLoaderDefinitionFunction } from 'webpack'
1213

@@ -18,7 +19,9 @@ export interface DevupUILoaderOptions {
1819
fileMapFile: string
1920
watch: boolean
2021
singleCss: boolean
22+
theme?: object
2123
}
24+
let init = false
2225

2326
const devupUILoader: RawLoaderDefinitionFunction<DevupUILoaderOptions> =
2427
function (source) {
@@ -30,9 +33,14 @@ const devupUILoader: RawLoaderDefinitionFunction<DevupUILoaderOptions> =
3033
classMapFile,
3134
fileMapFile,
3235
singleCss,
36+
theme,
3337
} = this.getOptions()
3438
const callback = this.async()
3539
const id = this.resourcePath
40+
if (theme && !init) {
41+
init = true
42+
registerTheme(theme)
43+
}
3644

3745
try {
3846
let rel = relative(dirname(this.resourcePath), cssDir).replaceAll(

0 commit comments

Comments
 (0)