tsbundle/ts/tsbundle.class.tsbundle.ts

123 lines
3.6 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
*/
2019-07-18 16:15:24 +00:00
public getBaseOptions(fromArg: string = `ts_web/index.ts`, toArg: string = 'dist_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({
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
declaration: true,
emitDecoratorMetadata: true,
experimentalDecorators: true,
inlineSourceMap: true,
noEmitOnError: true,
lib: ['es2017', 'dom'],
target: 'es2017',
noImplicitAny: false
}
}
}),
// 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
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-07-18 16:15:24 +00:00
productionOptions.plugins.push(plugins.rollupTerser());
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);
bundle.generate(buildOptions.output);
await bundle.write(buildOptions.output);
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);
bundle.generate(buildOptions.output);
await bundle.write(buildOptions.output);
2019-07-18 16:15:24 +00:00
logger.log('ok', `Successfully bundled files!`);
}
2019-06-16 15:02:38 +00:00
}