Skip to content

Commit 31b163b

Browse files
committed
Implement plugins
1 parent 97546c3 commit 31b163b

File tree

6 files changed

+147
-140
lines changed

6 files changed

+147
-140
lines changed

packages/next-plugin/src/index.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,2 @@
1-
import { join } from 'node:path'
2-
import type { DevupApiOptions } from '@devup-api/core'
3-
import { createUrlMap, generateInterface } from '@devup-api/generator'
4-
import { createTmpDir, readOpenapi, writeInterface } from '@devup-api/utils'
5-
import { devupApiWebpackPlugin } from '@devup-api/webpack-plugin'
6-
import type { NextConfig } from 'next'
7-
export function DevupUI(
8-
config: NextConfig,
9-
options: DevupApiOptions = {},
10-
): NextConfig {
11-
const isTurbo =
12-
process.env.TURBOPACK === '1' || process.env.TURBOPACK === 'auto'
13-
if (isTurbo) {
14-
const tempDir = createTmpDir(options?.tempDir)
15-
const schema = readOpenapi(options?.openapiFile)
16-
17-
writeInterface(
18-
join(tempDir, 'api.d.ts'),
19-
generateInterface(schema, options),
20-
)
21-
// Create urlMap and set environment variable
22-
const urlMap = createUrlMap(schema, options)
23-
config.env ??= {}
24-
Object.assign(config.env, {
25-
DEVUP_API_URL_MAP: JSON.stringify(urlMap),
26-
})
27-
return config
28-
}
29-
30-
const webpack = config.webpack
31-
config.webpack = (config, _options) => {
32-
config.plugins.push(new devupApiWebpackPlugin(options))
33-
if (typeof webpack === 'function') return webpack(config, _options)
34-
return config
35-
}
36-
return config
37-
}
1+
export * from './plugin'
2+
export { devupApi as default } from './plugin'

packages/next-plugin/src/plugin.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { join } from 'node:path'
2+
import type { DevupApiOptions } from '@devup-api/core'
3+
import { createUrlMap, generateInterface } from '@devup-api/generator'
4+
import { createTmpDir, readOpenapi, writeInterface } from '@devup-api/utils'
5+
import { devupApiWebpackPlugin } from '@devup-api/webpack-plugin'
6+
import type { NextConfig } from 'next'
7+
8+
export function devupApi(
9+
config: NextConfig,
10+
options: DevupApiOptions = {},
11+
): NextConfig {
12+
const isTurbo =
13+
process.env.TURBOPACK === '1' || process.env.TURBOPACK === 'auto'
14+
if (isTurbo) {
15+
const tempDir = createTmpDir(options?.tempDir)
16+
const schema = readOpenapi(options?.openapiFile)
17+
18+
writeInterface(
19+
join(tempDir, 'api.d.ts'),
20+
generateInterface(schema, options),
21+
)
22+
// Create urlMap and set environment variable
23+
const urlMap = createUrlMap(schema, options)
24+
config.env ??= {}
25+
Object.assign(config.env, {
26+
DEVUP_API_URL_MAP: JSON.stringify(urlMap),
27+
})
28+
return config
29+
}
30+
31+
const webpack = config.webpack
32+
config.webpack = (config, _options) => {
33+
config.plugins.push(new devupApiWebpackPlugin(options))
34+
if (typeof webpack === 'function') return webpack(config, _options)
35+
return config
36+
}
37+
return config
38+
}
Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,2 @@
1-
import { join } from 'node:path'
2-
import type { DevupApiOptions } from '@devup-api/core'
3-
import { createUrlMap, generateInterface } from '@devup-api/generator'
4-
import {
5-
createTmpDirAsync,
6-
readOpenapiAsync,
7-
writeInterfaceAsync,
8-
} from '@devup-api/utils'
9-
import type { RsbuildPlugin } from '@rsbuild/core'
10-
11-
export default function devupApiRsbuildPlugin(
12-
options?: DevupApiOptions,
13-
): RsbuildPlugin {
14-
return {
15-
name: 'devup-api',
16-
async setup(build) {
17-
const tempDir = await createTmpDirAsync(options?.tempDir)
18-
const schema = await readOpenapiAsync(options?.openapiFile)
19-
20-
// Generate interface file
21-
await writeInterfaceAsync(
22-
join(tempDir, 'api.d.ts'),
23-
generateInterface(schema, options),
24-
)
25-
26-
// Create urlMap and set environment variable
27-
const urlMap = createUrlMap(schema, options)
28-
29-
build.modifyRsbuildConfig((config) => {
30-
config.source ??= {}
31-
config.source.define ??= {}
32-
if (urlMap) {
33-
config.source.define['process.env.DEVUP_API_URL_MAP'] =
34-
JSON.stringify(urlMap)
35-
}
36-
return config
37-
})
38-
},
39-
}
40-
}
1+
export * from './plugin'
2+
export { devupApiRsbuildPlugin as default } from './plugin'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { join } from 'node:path'
2+
import type { DevupApiOptions } from '@devup-api/core'
3+
import { createUrlMap, generateInterface } from '@devup-api/generator'
4+
import {
5+
createTmpDirAsync,
6+
readOpenapiAsync,
7+
writeInterfaceAsync,
8+
} from '@devup-api/utils'
9+
import type { RsbuildPlugin } from '@rsbuild/core'
10+
11+
export function devupApiRsbuildPlugin(
12+
options?: DevupApiOptions,
13+
): RsbuildPlugin {
14+
return {
15+
name: 'devup-api',
16+
async setup(build) {
17+
const tempDir = await createTmpDirAsync(options?.tempDir)
18+
const schema = await readOpenapiAsync(options?.openapiFile)
19+
20+
// Generate interface file
21+
await writeInterfaceAsync(
22+
join(tempDir, 'api.d.ts'),
23+
generateInterface(schema, options),
24+
)
25+
26+
// Create urlMap and set environment variable
27+
const urlMap = createUrlMap(schema, options)
28+
29+
build.modifyRsbuildConfig((config) => {
30+
config.source ??= {}
31+
config.source.define ??= {}
32+
if (urlMap) {
33+
config.source.define['process.env.DEVUP_API_URL_MAP'] =
34+
JSON.stringify(urlMap)
35+
}
36+
return config
37+
})
38+
},
39+
}
40+
}
Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,2 @@
1-
import { join } from 'node:path'
2-
import type { DevupApiOptions } from '@devup-api/core'
3-
import { createUrlMap, generateInterface } from '@devup-api/generator'
4-
import {
5-
createTmpDirAsync,
6-
readOpenapiAsync,
7-
writeInterfaceAsync,
8-
} from '@devup-api/utils'
9-
import type { Compiler } from 'webpack'
10-
import { DefinePlugin } from 'webpack'
11-
12-
export class devupApiWebpackPlugin {
13-
options: DevupApiOptions
14-
private initialized = false
15-
16-
constructor(options?: DevupApiOptions) {
17-
this.options = options || {}
18-
}
19-
20-
apply(compiler: Compiler): void {
21-
// Perform async operations before compilation
22-
compiler.hooks.beforeCompile.tapAsync(
23-
'devup-api',
24-
async (_params, callback) => {
25-
// Guard: only run once
26-
if (this.initialized) {
27-
callback()
28-
return
29-
}
30-
31-
try {
32-
this.initialized = true
33-
34-
const tempDir = await createTmpDirAsync(this.options?.tempDir)
35-
const schema = await readOpenapiAsync(this.options?.openapiFile)
36-
37-
// Generate interface file
38-
await writeInterfaceAsync(
39-
join(tempDir, 'api.d.ts'),
40-
generateInterface(schema, this.options),
41-
)
42-
43-
// Create urlMap and set environment variable
44-
const urlMap = createUrlMap(schema, this.options)
45-
const define: Record<string, string> = {}
46-
if (urlMap) {
47-
define['process.env.DEVUP_API_URL_MAP'] = JSON.stringify(urlMap)
48-
}
49-
50-
// Add DefinePlugin to webpack configuration
51-
if (Object.keys(define).length > 0) {
52-
new DefinePlugin(define).apply(compiler)
53-
}
54-
55-
callback()
56-
} catch (error) {
57-
this.initialized = false
58-
callback(error as Error)
59-
}
60-
},
61-
)
62-
}
63-
}
1+
export * from './plugin'
2+
export { devupApiWebpackPlugin as default } from './plugin'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { join } from 'node:path'
2+
import type { DevupApiOptions } from '@devup-api/core'
3+
import { createUrlMap, generateInterface } from '@devup-api/generator'
4+
import {
5+
createTmpDirAsync,
6+
readOpenapiAsync,
7+
writeInterfaceAsync,
8+
} from '@devup-api/utils'
9+
import type { Compiler } from 'webpack'
10+
import { DefinePlugin } from 'webpack'
11+
12+
export class devupApiWebpackPlugin {
13+
options: DevupApiOptions
14+
private initialized = false
15+
16+
constructor(options?: DevupApiOptions) {
17+
this.options = options || {}
18+
}
19+
20+
apply(compiler: Compiler): void {
21+
// Perform async operations before compilation
22+
compiler.hooks.beforeCompile.tapAsync(
23+
'devup-api',
24+
async (_params, callback) => {
25+
// Guard: only run once
26+
if (this.initialized) {
27+
callback()
28+
return
29+
}
30+
31+
try {
32+
this.initialized = true
33+
34+
const tempDir = await createTmpDirAsync(this.options?.tempDir)
35+
const schema = await readOpenapiAsync(this.options?.openapiFile)
36+
37+
// Generate interface file
38+
await writeInterfaceAsync(
39+
join(tempDir, 'api.d.ts'),
40+
generateInterface(schema, this.options),
41+
)
42+
43+
// Create urlMap and set environment variable
44+
const urlMap = createUrlMap(schema, this.options)
45+
const define: Record<string, string> = {}
46+
if (urlMap) {
47+
define['process.env.DEVUP_API_URL_MAP'] = JSON.stringify(urlMap)
48+
}
49+
50+
// Add DefinePlugin to webpack configuration
51+
if (Object.keys(define).length > 0) {
52+
new DefinePlugin(define).apply(compiler)
53+
}
54+
55+
callback()
56+
} catch (error) {
57+
this.initialized = false
58+
callback(error as Error)
59+
}
60+
},
61+
)
62+
}
63+
}

0 commit comments

Comments
 (0)