tsbundle/ts/tsbundle.class.tsbundle.ts

122 lines
3.7 KiB
TypeScript
Raw 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-13 18:54:31 +00:00
public getBaseOptions(fromArg: string = `ts_web/index.ts`, toArg: string = 'dist_ts_web/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',
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
plugins.rollupTypescript({
2020-03-13 14:53:15 +00:00
declaration: false,
emitDecoratorMetadata: true,
experimentalDecorators: true,
inlineSourceMap: true,
noEmitOnError: true,
lib: ['esnext', 'dom'],
noImplicitAny: false,
target: 'es2018'
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(),
plugins.rollupCommonjs({
namedExports: {
'node_modules/@pushrocks/smartstate/dist/index.js': ['Smartstate']
}
}),
// Resolve source maps to the original source
2019-08-25 14:35:22 +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
})*/
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);
2019-10-05 19:25:44 +00:00
productionOptions.plugins.push(plugins.rollupTerser({
compress: true,
2019-10-05 23:21:32 +00:00
mangle: true,
2019-10-06 21:34:04 +00:00
sourcemap: true
2019-10-05 19:25:44 +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
*/
public async buildTest(fromArg: string, toArg: string) {
// create a bundle
logger.log('info', `bundling for TEST!`);
2019-07-19 08:52:27 +00:00
const buildOptions = this.getOptionsTest(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!`);
}
/**
* 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
}