tsbundle/ts/tsbundle.class.tsbundle.ts

140 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2019-06-16 15:02:38 +00:00
import * as plugins from './tsbundle.plugins';
import { logger } from './tsbundle.logging';
export class TsBundle {
/**
* the basic default options for rollup
*/
2020-03-14 22:41:25 +00:00
public getBaseOptions(
fromArg: string = `ts_web/index.ts`,
toArg: string = 'dist_bundle/bundle.js'
) {
2019-07-19 08:52:27 +00:00
logger.log('info', `from: ${fromArg}`);
logger.log('info', `to: ${toArg}`);
2019-06-16 15:02:38 +00:00
const baseOptions: plugins.rollup.RollupOptions = {
2019-07-18 16:15:24 +00:00
input: fromArg,
2019-06-16 15:02:38 +00:00
output: {
name: 'tsbundle',
2019-07-18 16:15:24 +00:00
file: toArg,
2019-06-16 15:02:38 +00:00
format: 'iife',
2020-07-07 18:55:17 +00:00
sourcemap: true,
2019-06-16 15:02:38 +00:00
},
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
watch: {
2020-07-07 18:55:17 +00:00
include: ['src/**'],
2019-06-16 15:02:38 +00:00
},
plugins: [
// Compile TypeScript files
plugins.rollupTypescript({
2020-07-07 18:55:17 +00:00
include: plugins.path.parse(fromArg).dir
? plugins.path.parse(fromArg).dir + '/**/*.ts'
: '**/*.ts',
2020-03-13 14:53:15 +00:00
declaration: false,
emitDecoratorMetadata: true,
experimentalDecorators: true,
inlineSourceMap: true,
noEmitOnError: true,
2020-03-18 16:32:33 +00:00
lib: ['esnext', 'dom', 'es2017.object'],
2020-03-13 14:53:15 +00:00
noImplicitAny: false,
2020-05-25 16:43:58 +00:00
target: 'es2018',
2020-07-07 18:55:17 +00:00
allowSyntheticDefaultImports: true,
2019-06-16 15:02:38 +00:00
}),
2020-05-25 21:07:40 +00:00
plugins.rollupJson(),
2019-06-16 15:02:38 +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(),
2020-05-25 17:01:43 +00:00
plugins.rollupCommonjs({}),
2019-06-16 15:02:38 +00:00
// Resolve source maps to the original source
2020-07-07 18:55:17 +00:00
plugins.rollupSourceMaps(),
2019-07-18 16:15:24 +00:00
/*plugins.rollupBabel({
2019-06-16 15:02:38 +00:00
runtimeHelpers: true,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
babelrc: false,
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
chrome: '41'
}
}
]
],
plugins: [
[
'@babel/plugin-transform-runtime',
{
regenerator: true
}
]
]
2019-07-18 16:15:24 +00:00
})*/
2020-07-07 18:55:17 +00:00
],
2019-06-16 15:02:38 +00:00
};
return baseOptions;
}
2019-07-18 16:15:24 +00:00
public getOptionsTest(fromArg: string, toArg: string): plugins.rollup.RollupOptions {
2019-07-19 10:09:00 +00:00
return this.getBaseOptions(fromArg, toArg);
2019-07-18 16:15:24 +00:00
}
public getOptionsProduction(fromArg: string, toArg: string): plugins.rollup.RollupOptions {
2019-07-19 10:09:00 +00:00
const productionOptions = this.getBaseOptions(fromArg, toArg);
2020-03-14 22:41:25 +00:00
productionOptions.plugins.push(
plugins.rollupTerser({
compress: true,
mangle: true,
2020-07-07 18:55:17 +00:00
sourcemap: true,
2020-03-14 22:41:25 +00:00
})
);
2019-07-18 16:15:24 +00:00
return productionOptions;
}
2019-07-19 08:52:27 +00:00
constructor() {
// Nothing here
}
2019-07-18 16:15:24 +00:00
/**
* creates a bundle for the test enviroment
*/
2020-07-07 18:55:17 +00:00
public async buildTest(
fromArg: string,
toArg: string,
bundlerArg: 'rollup' | 'parcel' = 'rollup'
) {
2019-07-18 16:15:24 +00:00
// create a bundle
2020-07-07 18:55:17 +00:00
switch (bundlerArg) {
case 'rollup':
logger.log('info', `bundling for TEST!`);
const buildOptions = this.getOptionsTest(fromArg, toArg);
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!`);
break;
case 'parcel':
const parsedPath = plugins.path.parse(toArg);
const parcelInstance = new plugins.smartparcel.Parcel(fromArg, parsedPath.dir, parsedPath.base);
await parcelInstance.build();
}
2019-07-18 16:15:24 +00:00
}
/**
* creates a bundle for the production environment
*/
public async buildProduction(fromArg: string, toArg: string) {
// create a bundle
logger.log('info', `bundling for PRODUCTION!`);
2019-07-19 08:52:27 +00:00
const buildOptions = this.getOptionsProduction(fromArg, toArg);
const bundle = await plugins.rollup.rollup(buildOptions);
2020-03-02 09:42:44 +00:00
bundle.generate(buildOptions.output as plugins.rollup.OutputOptions);
await bundle.write(buildOptions.output as plugins.rollup.OutputOptions);
2019-07-18 16:15:24 +00:00
logger.log('ok', `Successfully bundled files!`);
}
2019-06-16 15:02:38 +00:00
}