From 9e322c4cfb70a0ad39770be348d89bc995f50098 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 25 Jan 2021 22:15:02 +0100 Subject: [PATCH] Refactor rollup configuration --- packages/compiler/rollup.config.js | 55 ++---------------------- packages/core/rollup.config.js | 68 ++++++------------------------ rollup.js | 67 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 105 deletions(-) create mode 100644 rollup.js diff --git a/packages/compiler/rollup.config.js b/packages/compiler/rollup.config.js index 59c94e2..b6e123e 100644 --- a/packages/compiler/rollup.config.js +++ b/packages/compiler/rollup.config.js @@ -1,53 +1,6 @@ -import resolve from '@rollup/plugin-node-resolve'; -import { terser } from 'rollup-plugin-terser'; - -function distfile(insertion) { - const f = `syndicate-compiler${insertion}.js`; - return `dist/${f}`; -} - -function umd(insertion, extra) { - return { - file: distfile(insertion), - format: 'umd', - name: 'SyndicateCompiler', - sourcemap: true, - ... (extra || {}) - }; -} - -function es6(insertion, extra) { - return { - file: distfile('.es6' + insertion), - format: 'es', - sourcemap: true, - ... (extra || {}) - }; -} - +import { SyndicateRollup } from '../../rollup.js'; +const r = new SyndicateRollup('syndicate-compiler', { globalName: 'SyndicateCompiler' }); export default [ - { - input: 'lib/index.js', - plugins: [ - resolve({ - preferBuiltins: false, - }), - ], - output: [ - umd(''), - umd('.min', { plugins: [terser()] }), - ], - }, { - input: 'lib/index.js', - plugins: [ - resolve({ - // moduleDirectories: ['stubs', 'node_modules'], - preferBuiltins: false, - }), - ], - output: [ - es6(''), - es6('.min', { plugins: [terser()] }), - ], - } + r.config('lib/index.js', r.umd), + r.config('lib/index.js', r.es6), ]; diff --git a/packages/core/rollup.config.js b/packages/core/rollup.config.js index 1c51e5a..15be0d6 100644 --- a/packages/core/rollup.config.js +++ b/packages/core/rollup.config.js @@ -1,56 +1,16 @@ -import resolve from '@rollup/plugin-node-resolve'; -import { terser } from 'rollup-plugin-terser'; - -function distfile(insertion) { - const f = `syndicate${insertion}.js`; - return `dist/${f}`; -} - -function umd(insertion, extra) { - return { - file: distfile(insertion), - format: 'umd', - name: 'Syndicate', - sourcemap: true, - globals: { - 'crypto': 'crypto', - }, - ... (extra || {}) - }; -} - -function es6(insertion, extra) { - return { - file: distfile('.es6' + insertion), - format: 'es', - sourcemap: true, - ... (extra || {}) - }; -} - +import { SyndicateRollup } from '../../rollup.js'; +const r = new SyndicateRollup('syndicate', { globalName: 'Syndicate' }); export default [ - { - input: 'lib/index.js', - plugins: [ - resolve({ - preferBuiltins: false, - }), - ], - output: [ - umd(''), - umd('.min', { plugins: [terser()] }), - ], - }, { - input: 'lib/index.js', - plugins: [ - resolve({ - moduleDirectories: ['stubs', 'node_modules'], - preferBuiltins: false, - }), - ], - output: [ - es6(''), - es6('.min', { plugins: [terser()] }), - ], - } + r.config('lib/index.js', r.umd, { + output: { + globals: { + 'crypto': 'crypto', + }, + }, + }), + r.config('lib/index.js', r.es6, { + resolve: { + moduleDirectories: ['stubs', 'node_modules'], + }, + }), ]; diff --git a/rollup.js b/rollup.js new file mode 100644 index 0000000..573aa66 --- /dev/null +++ b/rollup.js @@ -0,0 +1,67 @@ +import resolve from '@rollup/plugin-node-resolve'; +import { terser } from 'rollup-plugin-terser'; + +export class SyndicateRollup { + constructor(packageName, options = {}) { + this.packageName = packageName; + this.globalName = options.globalName; + this.configs = []; + } + + distfile(insertion) { + const f = `${this.packageName}${insertion}.js`; + return `dist/${f}`; + } + + terser() { + return terser(); + } + + get umd() { + return (insertion, extra) => ({ + file: this.distfile(insertion), + format: 'umd', + sourcemap: true, + ... (this.globalName !== void 0) && { name: this.globalName }, + ... (extra || {}) + }); + } + + get es6() { + return (insertion, extra) => ({ + file: this.distfile('.es6' + insertion), + format: 'es', + sourcemap: true, + ... (extra || {}) + }); + } + + config(inputFile, outputMaker, options) { + return { + input: inputFile, + plugins: [ + resolve({ + preferBuiltins: false, + ... options && options.resolve + }), + ... (options && options.inputPlugins) || [] + ], + output: [ + outputMaker('', options && options.output), + outputMaker('.min', { plugins: [terser()], ... options && options.output }), + ], + ... options && options.external && { external: options.external }, + }; + } + + configNoCore(inputFile, outputMaker, options) { + options = { ... options }; + options.output = { ... options.output }; + options.output.globals = { ... options.output.globals }; + if (!('@syndicate-lang/core' in options.output.globals)) + options.output.globals['@syndicate-lang/core'] = 'Syndicate'; + options.external = options.external || []; + options.external.push('@syndicate-lang/core'); + return this.config(inputFile, outputMaker, options); + } +}