Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for vite bundler #52

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-gorillas-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: Support for vite bundler
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $ pnpm preview
Config is loaded from `.tnfrc.ts` by default.

- `alias: [string, string][]`: An array of alias pairs.
- `bundler: 'webpack' | 'mako'`: The bundler to use, default is `mako`.
- `bundler: 'webpack' | 'vite' | 'mako'`: The bundler to use, default is `mako`.
- `devServer: { port?: number; host?: string; https?: { hosts?: string[] }; ip?: string }`: The development server configuration.
- `externals: Record<string, string>`: An object that maps package names to their corresponding paths.
- `less: { modifyVars?: Record<string, string>; globalVars?: Record<string, string>; math?: 'always' | 'strict' | 'parens-division' | 'parens' | 'strict-legacy' | number; sourceMap?: any; plugins?: (string | [string, Record<string, any>])[];}`: The configuration passed to lessLoader.
Expand Down
8 changes: 6 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export async function build({
});
}

// build
const bundler = createBundler({ bundler: BundlerType.MAKO });
const bundlerType = config.bundler
? BundlerType[config.bundler.toUpperCase() as keyof typeof BundlerType]
: BundlerType.MAKO;
const bundler = createBundler({
bundler: bundlerType,
});
await bundler.build({
bundlerConfig: {
entry: {
Expand Down
4 changes: 4 additions & 0 deletions src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface BundlerOpts {
export enum BundlerType {
MAKO = 'mako',
WEBPACK = 'webpack',
VITE = 'vite',
}

export interface Bundler {
Expand Down Expand Up @@ -46,6 +47,9 @@ export function createBundler(opts: BundlerOpts): Bundler {
} else if (opts.bundler === BundlerType.WEBPACK) {
// @ts-expect-error
return (await import('./bundler_webpack.js')).default.default;
} else if (opts.bundler === BundlerType.VITE) {
// @ts-expect-error
return (await import('./bundler_vite.js')).default.default;
} else {
throw new Error(`Unsupported bundler ${opts.bundler}`);
}
Expand Down
47 changes: 47 additions & 0 deletions src/bundler/bundler_vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Bundler } from './bundler';

export default {
build: async (opts) => {
const { bundlerConfig, cwd, watch } = opts;
try {
// @ts-ignore
const { build: bundlerVite } = await import(
require.resolve('@umijs/bundler-vite', {
paths: [cwd],
})
).catch(() => {
throw new Error('Please install first @umijs/bundler-vite');
});

// 转换 alias 格式
const aliasConfig: Record<string, string> = {};
if (bundlerConfig.alias?.length) {
bundlerConfig.alias.forEach(([key, value]) => {
aliasConfig[key] = value;
});
}

const config = {
resolve: {
alias: aliasConfig,
},
...bundlerConfig,
};

await bundlerVite({
entry: bundlerConfig.entry,
config,
cwd,
watch: Boolean(watch),
});
} catch (err) {
if (err instanceof Error && err.message.includes('Cannot find module')) {
throw new Error('Please install first @umijs/bundler-vite');
}
throw err;
}
},
configDevServer: async (_opts) => {
throw new Error('Not implemented');
},
} as Bundler;
2 changes: 1 addition & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const RouterGeneratorConfig = z

export const ConfigSchema = z.object({
alias: z.array(z.tuple([z.string(), z.string()])).optional(),
bundler: z.enum(['webpack', 'mako']).optional(),
bundler: z.enum(['webpack', 'mako', 'vite']).optional(),
externals: z.record(z.string()).optional(),
devServer: z
.object({
Expand Down
Loading