tsbundle/ts/tsbundle.class.tsbundleprocess.ts

176 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-03-14 15:32:12 +00:00
import * as plugins from './tsbundle.plugins.js';
import { logger } from './tsbundle.logging.js';
2021-07-23 13:45:23 +00:00
export class TsBundleProcess {
/**
* the basic default options for rollup
*/
public getBaseOptions(
fromArg: string = `ts_web/index.ts`,
2022-03-15 12:01:18 +00:00
toArg: string = 'dist_bundle/bundle.js',
argvArg: any
2021-07-23 13:45:23 +00:00
) {
logger.log('info', `from: ${fromArg}`);
logger.log('info', `to: ${toArg}`);
const baseOptions: plugins.rollup.RollupOptions = {
input: fromArg,
output: {
name: 'tsbundle',
file: toArg,
format: 'iife',
sourcemap: true,
},
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
watch: {
include: ['src/**'],
},
plugins: [
// Compile TypeScript files
2022-03-14 15:32:12 +00:00
(plugins.rollupTypescript as any)({
2021-07-23 13:45:23 +00:00
include: plugins.path.parse(fromArg).dir
? plugins.path.parse(fromArg).dir + '/**/*.ts'
: '**/*.ts',
declaration: false,
emitDecoratorMetadata: true,
experimentalDecorators: true,
inlineSourceMap: true,
noEmitOnError: true,
2022-03-14 15:32:12 +00:00
lib: ['dom'],
2021-07-23 13:45:23 +00:00
noImplicitAny: false,
2022-03-14 15:32:12 +00:00
target: 'es2020',
2022-03-15 12:01:18 +00:00
module: 'es2020',
moduleResolution: 'node12',
2021-07-23 13:45:23 +00:00
allowSyntheticDefaultImports: true,
2021-10-06 11:36:26 +00:00
importsNotUsedAsValues: 'preserve',
2022-03-15 12:45:03 +00:00
...(argvArg && argvArg.skiplibcheck
? {
skipLibCheck: true,
}
: {}),
...(argvArg && argvArg.allowimplicitany
? {
noImplicitAny: false,
}
: {}),
...(argvArg && argvArg.commonjs
? {
module: 'commonjs',
moduleResolution: 'node',
}
: {}),
2021-07-23 13:45:23 +00:00
}),
2022-03-14 15:32:12 +00:00
(plugins.rollupJson as any)(),
2021-07-23 13:45:23 +00:00
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
plugins.rollupResolve(),
2022-03-14 15:32:12 +00:00
(plugins.rollupCommonjs as any)({}),
2021-07-23 13:45:23 +00:00
// Resolve source maps to the original source
2022-03-14 15:32:12 +00:00
plugins.rollupSourceMaps(),
2021-07-23 13:45:23 +00:00
],
};
return baseOptions;
}
2022-03-15 12:45:03 +00:00
public getOptionsTest(
fromArg: string,
toArg: string,
argvArg: any
): plugins.rollup.RollupOptions {
2022-03-15 12:01:18 +00:00
return this.getBaseOptions(fromArg, toArg, argvArg);
2021-07-23 13:45:23 +00:00
}
2022-03-15 12:45:03 +00:00
public getOptionsProduction(
fromArg: string,
toArg: string,
argvArg: any
): plugins.rollup.RollupOptions {
2022-03-15 12:01:18 +00:00
const productionOptions = this.getBaseOptions(fromArg, toArg, argvArg);
2021-07-23 13:45:23 +00:00
productionOptions.plugins.push(
plugins.rollupTerser({
compress: true,
2022-03-14 15:32:12 +00:00
mangle: true,
2021-07-23 13:45:23 +00:00
})
);
return productionOptions;
}
constructor() {
// Nothing here
}
/**
* creates a bundle for the test enviroment
*/
public async buildTest(
fromArg: string,
toArg: string,
2022-03-15 12:01:18 +00:00
bundlerArg: 'rollup' | 'parcel' = 'rollup',
argvArg: any
2021-07-23 13:45:23 +00:00
) {
// create a bundle
switch (bundlerArg) {
case 'rollup':
logger.log('info', `bundling for TEST!`);
2022-03-15 12:01:18 +00:00
const buildOptions = this.getOptionsTest(fromArg, toArg, argvArg);
2021-07-23 13:45:23 +00:00
const bundle = await plugins.rollup.rollup(buildOptions);
bundle.generate(buildOptions.output as plugins.rollup.OutputOptions);
await bundle.write(buildOptions.output as plugins.rollup.OutputOptions);
logger.log('ok', `Successfully bundled files!`);
2021-08-28 01:07:10 +00:00
process.exit(0);
2021-07-23 13:45:23 +00:00
case 'parcel':
const parsedPath = plugins.path.parse(toArg);
2022-03-14 15:32:12 +00:00
const parcelInstance = new plugins.smartparcel.Parcel(
fromArg,
parsedPath.dir,
parsedPath.base
);
2021-07-23 13:45:23 +00:00
await parcelInstance.build();
}
}
/**
* creates a bundle for the production environment
*/
2022-03-15 12:01:18 +00:00
public async buildProduction(fromArg: string, toArg: string, argvArg: any) {
2021-07-23 13:45:23 +00:00
// create a bundle
logger.log('info', `bundling for PRODUCTION!`);
2022-03-15 12:01:18 +00:00
const buildOptions = this.getOptionsProduction(fromArg, toArg, argvArg);
2021-07-23 13:45:23 +00:00
const bundle = await plugins.rollup.rollup(buildOptions);
bundle.generate(buildOptions.output as plugins.rollup.OutputOptions);
await bundle.write(buildOptions.output as plugins.rollup.OutputOptions);
logger.log('ok', `Successfully bundled files!`);
2021-08-28 01:07:10 +00:00
process.exit(0);
2021-07-23 13:45:23 +00:00
}
}
const run = async () => {
console.log('running spawned compilation process');
console.log(`cwd: ${process.env.tsbundleCwd}`);
console.log(`from: ${process.env.tsbundleFrom}`);
console.log(`to: ${process.env.tsbundleTo}`);
console.log(`mode: ${process.env.tsbundleMode}`);
process.chdir(process.env.tsbundleCwd);
2022-03-14 15:32:12 +00:00
console.log(`switched to ${process.cwd()}`);
2021-07-23 13:45:23 +00:00
const tsbundleProcessInstance = new TsBundleProcess();
if (process.env.tsbundleMode === 'test') {
2022-03-14 15:32:12 +00:00
tsbundleProcessInstance.buildTest(
process.env.tsbundleFrom,
process.env.tsbundleTo,
2022-03-15 12:01:18 +00:00
process.env.tsbundleBundler as 'rollup' | 'parcel',
2022-03-15 12:45:03 +00:00
JSON.parse(process.env.tsbundleArgv)
2022-03-14 15:32:12 +00:00
);
2021-07-23 13:45:23 +00:00
} else {
2022-03-15 12:45:03 +00:00
tsbundleProcessInstance.buildProduction(
process.env.tsbundleFrom,
process.env.tsbundleTo,
JSON.parse(process.env.tsbundleArgv)
);
2021-07-23 13:45:23 +00:00
}
2022-03-14 15:32:12 +00:00
};
2021-07-23 13:45:23 +00:00
run();