tsbundle/ts/mod_esbuild/index.child.ts

101 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-03-15 23:21:05 +00:00
import * as plugins from './plugins.js';
2022-05-04 15:13:52 +00:00
import * as paths from '../paths.js';
2022-03-15 23:21:05 +00:00
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];
aliasObject[alias] = aliasPath;
}
}
return aliasObject;
} catch (error) {
return {};
}
}
2022-03-15 23:21:05 +00:00
/**
* creates a bundle for the test enviroment
*/
public async buildTest(fromArg: string, toArg: string, argvArg: any) {
2022-03-15 23:21:05 +00:00
// create a bundle
const esbuild = await plugins.esbuild.build({
entryPoints: [fromArg],
bundle: true,
2022-03-17 07:28:11 +00:00
sourcemap: true,
format: 'esm',
2023-10-03 17:12:49 +00:00
target: 'es2022',
2024-01-08 15:28:22 +00:00
entryNames: plugins.path.parse(toArg).name,
2024-01-08 15:24:52 +00:00
outdir: plugins.path.parse(toArg).dir,
2024-01-09 15:54:29 +00:00
// splitting: true,
tsconfig: paths.tsconfigPath,
alias: await this.getAliases(),
2022-03-15 23:21:05 +00:00
});
}
/**
* creates a bundle for the production environment
*/
public async buildProduction(fromArg: string, toArg: string, argvArg: any) {
2022-03-15 23:21:05 +00:00
// create a bundle
2022-03-16 10:08:52 +00:00
console.log('esbuild specific:');
console.log(`from: ${fromArg}`);
console.log(`to: ${toArg}`);
2022-03-15 23:21:05 +00:00
const esbuild = await plugins.esbuild.build({
entryPoints: [fromArg],
bundle: true,
2022-03-17 07:28:11 +00:00
sourcemap: true,
format: 'esm',
2023-10-03 17:12:49 +00:00
target: 'es2022',
2022-03-17 07:28:11 +00:00
minify: true,
2024-01-08 15:24:52 +00:00
entryNames: 'bundle',
outdir: plugins.path.parse(toArg).dir,
tsconfig: paths.tsconfigPath,
2024-01-09 15:54:29 +00:00
// splitting: true,
2024-01-08 15:24:52 +00:00
chunkNames: 'chunks/[name]-[hash]',
alias: await this.getAliases(),
2022-03-15 23:21:05 +00:00
});
}
}
const run = async () => {
console.log('running spawned compilation process');
const transportOptions: interfaces.IEnvTransportOptions = JSON.parse(
process.env.transportOptions
);
2022-03-16 12:25:08 +00:00
console.log('=======> ESBUILD');
2022-03-15 23:21:05 +00:00
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:');
2022-03-15 23:21:05 +00:00
tsbundleProcessInstance.buildTest(
2022-03-16 10:52:59 +00:00
plugins.smartpath.transform.makeAbsolute(transportOptions.from, process.cwd()),
plugins.smartpath.transform.makeAbsolute(transportOptions.to, process.cwd()),
2022-03-15 23:21:05 +00:00
transportOptions.argv
);
} else {
console.log('building for production:');
2022-03-15 23:21:05 +00:00
tsbundleProcessInstance.buildProduction(
2022-03-16 10:52:59 +00:00
plugins.smartpath.transform.makeAbsolute(transportOptions.from, process.cwd()),
plugins.smartpath.transform.makeAbsolute(transportOptions.to, process.cwd()),
2022-03-15 23:21:05 +00:00
transportOptions.argv
);
}
};
run();