UNPKG

3.93 kBPlain TextView Raw
1import * as plugins from './tsbundle.plugins';
2import { logger } from './tsbundle.logging';
3
4export class TsBundle {
5 /**
6 * the basic default options for rollup
7 */
8 public getBaseOptions(fromArg: string = `ts_web/index.ts`, toArg: string = 'dist_web/bundle.js') {
9 logger.log('info', `from: ${fromArg}`);
10 logger.log('info', `to: ${toArg}`);
11
12 const baseOptions: plugins.rollup.RollupOptions = {
13 input: fromArg,
14 output: {
15 name: 'tsbundle',
16 file: toArg,
17 format: 'iife',
18 sourcemap: true
19 },
20 // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
21 external: [],
22 watch: {
23 include: ['src/**']
24 },
25 plugins: [
26 // Compile TypeScript files
27 plugins.rollupTypescript({
28 useTsconfigDeclarationDir: true,
29 tsconfigOverride: {
30 compilerOptions: {
31 declaration: true,
32 emitDecoratorMetadata: true,
33 experimentalDecorators: true,
34 inlineSourceMap: true,
35 noEmitOnError: true,
36 lib: ['esnext', 'dom'],
37 target: 'es2018',
38 noImplicitAny: false
39 }
40 }
41 }),
42 // Allow node_modules resolution, so you can use 'external' to control
43 // which external modules to include in the bundle
44 // https://github.com/rollup/rollup-plugin-node-resolve#usage
45 plugins.rollupResolve(),
46 plugins.rollupCommonjs({
47 namedExports: {
48 'node_modules/@pushrocks/smartstate/dist/index.js': ['Smartstate']
49 }
50 }),
51
52 // Resolve source maps to the original source
53 plugins.rollupSourceMaps()
54 /*plugins.rollupBabel({
55 runtimeHelpers: true,
56 extensions: ['.js', '.jsx', '.ts', '.tsx'],
57 babelrc: false,
58 presets: [
59 [
60 '@babel/preset-env',
61 {
62 modules: false,
63 targets: {
64 chrome: '41'
65 }
66 }
67 ]
68 ],
69 plugins: [
70 [
71 '@babel/plugin-transform-runtime',
72 {
73 regenerator: true
74 }
75 ]
76 ]
77 })*/
78 ]
79 };
80 return baseOptions;
81 }
82
83 public getOptionsTest(fromArg: string, toArg: string): plugins.rollup.RollupOptions {
84 return this.getBaseOptions(fromArg, toArg);
85 }
86
87 public getOptionsProduction(fromArg: string, toArg: string): plugins.rollup.RollupOptions {
88 const productionOptions = this.getBaseOptions(fromArg, toArg);
89 productionOptions.plugins.push(plugins.rollupTerser({
90 compress: true,
91 mangle: true,
92 sourcemap: true
93 }));
94 return productionOptions;
95 }
96
97 constructor() {
98 // Nothing here
99 }
100
101 /**
102 * creates a bundle for the test enviroment
103 */
104 public async buildTest(fromArg: string, toArg: string) {
105 // create a bundle
106 logger.log('info', `bundling for TEST!`);
107 const buildOptions = this.getOptionsTest(fromArg, toArg);
108 const bundle = await plugins.rollup.rollup(buildOptions);
109 bundle.generate(buildOptions.output as plugins.rollup.OutputOptions);
110 await bundle.write(buildOptions.output as plugins.rollup.OutputOptions);
111 logger.log('ok', `Successfully bundled files!`);
112 }
113
114 /**
115 * creates a bundle for the production environment
116 */
117 public async buildProduction(fromArg: string, toArg: string) {
118 // create a bundle
119 logger.log('info', `bundling for PRODUCTION!`);
120 const buildOptions = this.getOptionsProduction(fromArg, toArg);
121 const bundle = await plugins.rollup.rollup(buildOptions);
122 bundle.generate(buildOptions.output as plugins.rollup.OutputOptions);
123 await bundle.write(buildOptions.output as plugins.rollup.OutputOptions);
124 logger.log('ok', `Successfully bundled files!`);
125 }
126}