Skip to content

Commit

Permalink
minify & correctly generate sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
giabao committed Apr 20, 2019
1 parent a5513af commit c7ca257
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 11 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-re": "^1.0.7",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-terser": "^4.0.4",
"rollup-plugin-typescript2": "^0.20.1",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.4.1"
}
}
32 changes: 24 additions & 8 deletions scripts/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import sourceMaps from 'rollup-plugin-sourcemaps'
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import * as pkg from '../package.json';
import replace from 'rollup-plugin-re';
import {RollupOptions} from "rollup";
import {modules, projectRoot} from "./util";
import {resolve} from "path";
import { uglify } from 'rollup-plugin-uglify'
import { terser } from 'rollup-plugin-terser'

const buildVars = (function() {
const date = new Date();
Expand All @@ -30,19 +33,29 @@ const plugins = [
}),
nodeResolve(),
commonjs(),
// Resolve source maps to the original source
sourceMaps(),
];

function configsFor(name: string): RollupOptions[] {
function concatIf<T>(test: boolean, a: T[], b:()=>T[]) {
return test? a.concat(b()) : a;
}

function outputFile(f: string, isProd: boolean) {
return isProd ? f.replace(/\.js$/, '.min.js') : f;
}

function configsFor(module: string, isProd: boolean): RollupOptions[] {
// const tsconfig = await import(`${srcDir}/${name}/tsconfig.json`);
// const outDir = tsconfig.compilerOptions.outDir;
const d = resolve(projectRoot, 'dist', name);
const isRangyModule = name !== 'core';
const d = resolve(projectRoot, 'dist', module);
const isRangyModule = module !== 'core';
// Indicate here external modules you don't wanna include in your bundle
const external = isRangyModule? ['rangy2'] : [];
return [
{ input: [`${d}/esm5/index.js`],
output: {
file: `${d}/bundles/index.umd.js`,
file: outputFile(`${d}/bundles/index.umd.js`, isProd),
format: 'umd',
name: 'rangy',
sourcemap: true,
Expand All @@ -51,22 +64,25 @@ function configsFor(name: string): RollupOptions[] {
},
inlineDynamicImports: true,
external,
plugins,
plugins: concatIf(isProd, plugins, uglify),
},
{ input: [`${d}/esm2015/index.js`],
output: {
file: `${d}/bundles/index.esm.js`,
file: outputFile(`${d}/bundles/index.esm.js`, isProd),
format: 'es',
sourcemap: true,
},
inlineDynamicImports: true,
// only bundle tslib, core-js in umd file
external: (id) => [...external, 'tslib', 'core-js'].includes(id) || id.startsWith('core-js/'),
plugins,
plugins: concatIf(isProd, plugins, terser),
}
]
}

const configs = modules.flatMap(configsFor);
const configs = modules.flatMap(m => [
...configsFor(m, false),
...configsFor(m, true)
]);

export default configs;
Loading

0 comments on commit c7ca257

Please sign in to comment.