Skip to content

Commit e119bd4

Browse files
committed
feat(create-rzpack): 添加打包器的选择功能
1 parent 4711191 commit e119bd4

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

packages/create-rzpack/src/create.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs'
1+
import fs from 'node:fs'
22
import {
33
bold,
44
fileExists,

packages/create-rzpack/src/prompts.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import prompts from 'prompts'
22
import { blue, bold, cyan, getFileFullPath, red, yellow } from 'rzpack-utils'
3-
import { CLIOptions, Template } from '.'
3+
import { type CLIOptions, Template } from '.'
44
import { DEFAULT_CONFIG } from './constant'
55
import {
66
canSafelyOverwrite,
@@ -14,11 +14,17 @@ export enum JS_LINT {
1414
BIOME = 'biome',
1515
}
1616

17+
export enum BUILDER {
18+
WEBPACK = 'webpack',
19+
RSPACK = 'rspack',
20+
}
21+
1722
export interface PromptsResult {
1823
projectName?: string
1924
packageName?: string
2025
overwrite?: boolean
2126
template?: Template
27+
builder?: BUILDER
2228
jtsLoader?: string
2329
cssScoped?: string
2430
jsLint?: JS_LINT
@@ -31,6 +37,7 @@ export interface PromptsResult {
3137

3238
const getPrompts = async ({ projectName, template, force }: CLIOptions) => {
3339
let targetDir = projectName
40+
let builder: BUILDER = BUILDER.WEBPACK
3441

3542
let result: PromptsResult = {
3643
projectName,
@@ -103,8 +110,20 @@ const getPrompts = async ({ projectName, template, force }: CLIOptions) => {
103110
],
104111
},
105112
{
106-
name: 'jtsLoader',
113+
name: 'builder',
107114
type: 'select',
115+
message: yellow('请选择打包器'),
116+
choices: [
117+
{ title: yellow('Webpack'), value: BUILDER.WEBPACK },
118+
{ title: blue('Rspack'), value: BUILDER.RSPACK },
119+
],
120+
onState: (state) => {
121+
builder = state.value
122+
},
123+
},
124+
{
125+
name: 'jtsLoader',
126+
type: () => (builder === 'rspack' ? null : 'select'),
108127
message: yellow('请选择Js/Ts文件的loader'),
109128
hint: '用于编译时处理JSX文件',
110129
choices: [

packages/create-rzpack/src/render.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import fs from 'fs'
2-
import path from 'path'
1+
import fs from 'node:fs'
2+
import path from 'node:path'
33
import { pathResolve, run } from 'rzpack-utils'
44
import { Template } from '.'
55
import biomeVscodeSettings from '../template-biome/.vscode/settings.json'
6-
import { JS_LINT, PromptsResult } from './prompts'
6+
import { BUILDER, JS_LINT, type PromptsResult } from './prompts'
77
import { deepMerge, sortDependencies } from './utils'
88

99
/**
@@ -330,9 +330,23 @@ export const renderReadme = ({
330330
* @param {PromptsResult} result
331331
*/
332332
export const renderConfig = (result: PromptsResult) => {
333-
const { projectName, cssScoped, jtsLoader, template, million, imageMini } =
334-
result
333+
const {
334+
projectName,
335+
cssScoped,
336+
builder,
337+
jtsLoader,
338+
template,
339+
million,
340+
imageMini,
341+
} = result
335342
const isTsTemplate = template === Template.TS
343+
let importStr = 'import { defineConfig'
344+
if (jtsLoader) {
345+
importStr += ', JSX_TOOLS'
346+
}
347+
if (builder) {
348+
importStr += ', BUILDER'
349+
}
336350

337351
let assets = ' assets: {\n'
338352
if (cssScoped) {
@@ -351,12 +365,12 @@ export const renderConfig = (result: PromptsResult) => {
351365
const lessVars =
352366
' lessVars: {\n' + ` file: './src/theme/globalVars.ts',\n` + ' },\n'
353367

368+
importStr += ` } from 'rzpack'\n\n`
354369
fs.writeFileSync(
355370
path.resolve(process.env.ROOT, 'rzpack.config.ts'),
356-
`import { defineConfig${
357-
jtsLoader ? ', JSX_TOOLS' : ''
358-
} } from 'rzpack'\n\n` +
371+
importStr +
359372
'export default defineConfig({\n' +
373+
` ${builder === BUILDER.RSPACK ? 'builder: BUILDER.RSPACK,\n' : ''}` +
360374
`${hasAssets ? assets : ''}` +
361375
' html: {\n' +
362376
` title: '${projectName}',\n` +

0 commit comments

Comments
 (0)