236 lines
6.3 KiB
TypeScript
236 lines
6.3 KiB
TypeScript
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(); |