feat(bundler): Introduce rspack bundler support and update multi-bundler workflow
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tsbundle',
|
||||
version: '2.3.0',
|
||||
description: 'a bundler using rollup for painless bundling of web projects'
|
||||
version: '2.4.0',
|
||||
description: 'a multi-bundler tool supporting esbuild, rolldown, and rspack for painless bundling of web projects'
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ export interface ICliOptions {
|
||||
commonjs?: boolean;
|
||||
skiplibcheck?: boolean;
|
||||
production?: boolean;
|
||||
bundler: 'parcel' | 'esbuild' | 'rollup' | 'rolldown'
|
||||
bundler: 'esbuild' | 'rolldown' | 'rspack'
|
||||
}
|
||||
|
||||
export interface IEnvTransportOptions {
|
||||
|
@ -61,7 +61,7 @@ export class TsBundleProcess {
|
||||
format: 'esm',
|
||||
target: 'es2022',
|
||||
minify: true,
|
||||
entryNames: 'bundle',
|
||||
entryNames: plugins.path.parse(toArg).name,
|
||||
outdir: plugins.path.parse(toArg).dir,
|
||||
tsconfig: paths.tsconfigPath,
|
||||
splitting: false,
|
||||
|
@ -39,10 +39,15 @@ export class TsBundleProcess {
|
||||
},
|
||||
});
|
||||
|
||||
const outputDir = plugins.path.dirname(toArg);
|
||||
const outputFilename = plugins.path.basename(toArg);
|
||||
|
||||
await result.write({
|
||||
file: toArg,
|
||||
dir: outputDir,
|
||||
entryFileNames: outputFilename,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
inlineDynamicImports: true,
|
||||
});
|
||||
}
|
||||
|
||||
@ -66,11 +71,16 @@ export class TsBundleProcess {
|
||||
},
|
||||
});
|
||||
|
||||
const outputDir = plugins.path.dirname(toArg);
|
||||
const outputFilename = plugins.path.basename(toArg);
|
||||
|
||||
await result.write({
|
||||
file: toArg,
|
||||
dir: outputDir,
|
||||
entryFileNames: outputFilename,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
minify: true,
|
||||
inlineDynamicImports: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
236
ts/mod_rspack/index.child.ts
Normal file
236
ts/mod_rspack/index.child.ts
Normal file
@ -0,0 +1,236 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
import * as interfaces from '../interfaces/index.js';
|
||||
import { logger } from '../tsbundle.logging.js';
|
||||
|
||||
export class TsBundleProcess {
|
||||
constructor() {
|
||||
// Nothing here
|
||||
}
|
||||
|
||||
public async getAliases() {
|
||||
try {
|
||||
const aliasObject: Record<string, string> = {};
|
||||
const localTsConfig = plugins.smartfile.fs.toObjectSync(
|
||||
plugins.path.join(paths.cwd, 'tsconfig.json')
|
||||
);
|
||||
if (localTsConfig.compilerOptions && localTsConfig.compilerOptions.paths) {
|
||||
for (const alias of Object.keys(localTsConfig.compilerOptions.paths)) {
|
||||
const aliasPath = localTsConfig.compilerOptions.paths[alias][0];
|
||||
// Convert TypeScript path to absolute path for rspack
|
||||
aliasObject[alias.replace('/*', '')] = plugins.path.resolve(paths.cwd, aliasPath.replace('/*', ''));
|
||||
}
|
||||
}
|
||||
return aliasObject;
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a bundle for the test enviroment
|
||||
*/
|
||||
public async buildTest(fromArg: string, toArg: string, argvArg: any) {
|
||||
const aliases = await this.getAliases();
|
||||
const outputDir = plugins.path.dirname(toArg);
|
||||
const outputFilename = plugins.path.basename(toArg);
|
||||
|
||||
const config = {
|
||||
mode: 'development' as const,
|
||||
entry: {
|
||||
main: fromArg,
|
||||
},
|
||||
output: {
|
||||
path: outputDir,
|
||||
filename: outputFilename,
|
||||
library: {
|
||||
type: 'module' as const,
|
||||
},
|
||||
},
|
||||
devtool: 'source-map' as const,
|
||||
resolve: {
|
||||
alias: aliases,
|
||||
extensions: ['.ts', '.tsx', '.js', '.jsx'],
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'builtin:swc-loader',
|
||||
options: {
|
||||
jsc: {
|
||||
parser: {
|
||||
syntax: 'typescript',
|
||||
tsx: true,
|
||||
decorators: true,
|
||||
},
|
||||
target: 'es2022',
|
||||
transform: {
|
||||
decoratorVersion: '2022-03',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
type: 'javascript/auto',
|
||||
},
|
||||
],
|
||||
},
|
||||
experiments: {
|
||||
outputModule: true,
|
||||
},
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
plugins.rspack(config, (err, stats) => {
|
||||
if (err) {
|
||||
console.error(err.stack || err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.hasErrors()) {
|
||||
console.error(stats.toString());
|
||||
reject(new Error('Build failed with errors'));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(stats.toString({
|
||||
colors: true,
|
||||
modules: false,
|
||||
children: false,
|
||||
chunks: false,
|
||||
chunkModules: false,
|
||||
}));
|
||||
|
||||
resolve(undefined);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a bundle for the production environment
|
||||
*/
|
||||
public async buildProduction(fromArg: string, toArg: string, argvArg: any) {
|
||||
console.log('rspack specific:');
|
||||
console.log(`from: ${fromArg}`);
|
||||
console.log(`to: ${toArg}`);
|
||||
|
||||
const aliases = await this.getAliases();
|
||||
const outputDir = plugins.path.dirname(toArg);
|
||||
const outputFilename = plugins.path.basename(toArg);
|
||||
|
||||
const config = {
|
||||
mode: 'production' as const,
|
||||
entry: {
|
||||
main: fromArg,
|
||||
},
|
||||
output: {
|
||||
path: outputDir,
|
||||
filename: outputFilename,
|
||||
library: {
|
||||
type: 'module' as const,
|
||||
},
|
||||
},
|
||||
devtool: 'source-map' as const,
|
||||
resolve: {
|
||||
alias: aliases,
|
||||
extensions: ['.ts', '.tsx', '.js', '.jsx'],
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'builtin:swc-loader',
|
||||
options: {
|
||||
jsc: {
|
||||
parser: {
|
||||
syntax: 'typescript',
|
||||
tsx: true,
|
||||
decorators: true,
|
||||
},
|
||||
target: 'es2022',
|
||||
transform: {
|
||||
decoratorVersion: '2022-03',
|
||||
},
|
||||
minify: {
|
||||
compress: true,
|
||||
mangle: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
type: 'javascript/auto',
|
||||
},
|
||||
],
|
||||
},
|
||||
optimization: {
|
||||
minimize: true,
|
||||
concatenateModules: true,
|
||||
usedExports: true,
|
||||
sideEffects: true,
|
||||
},
|
||||
experiments: {
|
||||
outputModule: true,
|
||||
},
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
plugins.rspack(config, (err, stats) => {
|
||||
if (err) {
|
||||
console.error(err.stack || err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.hasErrors()) {
|
||||
console.error(stats.toString());
|
||||
reject(new Error('Build failed with errors'));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(stats.toString({
|
||||
colors: true,
|
||||
modules: false,
|
||||
children: false,
|
||||
chunks: false,
|
||||
chunkModules: false,
|
||||
}));
|
||||
|
||||
resolve(undefined);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const run = async () => {
|
||||
console.log('running spawned compilation process');
|
||||
const transportOptions: interfaces.IEnvTransportOptions = JSON.parse(
|
||||
process.env.transportOptions
|
||||
);
|
||||
console.log('=======> RSPACK');
|
||||
console.log(transportOptions);
|
||||
process.chdir(transportOptions.cwd);
|
||||
console.log(`switched to ${process.cwd()}`);
|
||||
const tsbundleProcessInstance = new TsBundleProcess();
|
||||
if (transportOptions.mode === 'test') {
|
||||
console.log('building for test:');
|
||||
await tsbundleProcessInstance.buildTest(
|
||||
plugins.smartpath.transform.makeAbsolute(transportOptions.from, process.cwd()),
|
||||
plugins.smartpath.transform.makeAbsolute(transportOptions.to, process.cwd()),
|
||||
transportOptions.argv
|
||||
);
|
||||
} else {
|
||||
console.log('building for production:');
|
||||
await tsbundleProcessInstance.buildProduction(
|
||||
plugins.smartpath.transform.makeAbsolute(transportOptions.from, process.cwd()),
|
||||
plugins.smartpath.transform.makeAbsolute(transportOptions.to, process.cwd()),
|
||||
transportOptions.argv
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
5
ts/mod_rspack/plugins.ts
Normal file
5
ts/mod_rspack/plugins.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from '../plugins.js';
|
||||
|
||||
import { rspack } from '@rspack/core';
|
||||
|
||||
export { rspack }
|
@ -15,10 +15,8 @@ export class TsBundle {
|
||||
switch (argvArg.bundler) {
|
||||
case 'rolldown':
|
||||
return './mod_rolldown/index.child.js';
|
||||
case 'rollup':
|
||||
return './mod_rollup/index.child.js';
|
||||
case 'parcel':
|
||||
return './mod_parcel/index.child.js';
|
||||
case 'rspack':
|
||||
return './mod_rspack/index.child.js';
|
||||
case 'esbuild':
|
||||
default:
|
||||
return './mod_esbuild/index.child.js';
|
||||
|
Reference in New Issue
Block a user