diff --git a/package.json b/package.json index 7616a73..5b35114 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,8 @@ }, "unpkg": "./dist/shareon.min.js", "scripts": { - "build": "rollup -c ./rollup/rollup.config.prod.js", - "dev": "rollup -w -c ./rollup/rollup.config.dev.js", + "build": "rollup -c", + "dev": "rollup -w -c", "pretest": "run-s build", "test:lint": "eslint-ci --ext .js,.ts ./src/", "test:size": "size-limit", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..b25e5fe --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,89 @@ +import license from 'rollup-plugin-license'; +import postcss from 'rollup-plugin-postcss'; +import postcssPluginBanner from 'postcss-banner'; +import postcssPluginCssnano from 'cssnano'; +import strip from '@rollup/plugin-strip'; +import { terser } from 'rollup-plugin-terser'; +import typescript from '@rollup/plugin-typescript'; + +const isDev = process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'development'; + +const pkg = require('./package.json'); + +const inputFile = './src/index.ts'; +const outputDir = isDev ? './dev' : './dist/'; + +const bannerText = `${pkg.name} v${pkg.version} by Nikita Karamov\n${pkg.homepage}`; + +/* + * PLUGINS + */ + +const plugins = []; + +plugins.push(typescript()); + +if (!isDev) { + plugins.push(strip({ + debugger: true, + include: ['**/*.js', '**/*.ts'], + functions: ['console.log', 'console.debug', 'assert.*'], + sourceMap: false, + })); + + plugins.push(license({ + banner: { + commentStyle: 'ignored', + content: bannerText, + }, + })); +} + +plugins.push(postcss({ + extract: `${pkg.name}.css`, + plugins: [ + (!isDev) && postcssPluginCssnano({ + preset: 'default', + }), + postcssPluginBanner({ + banner: bannerText, + important: true, + }), + ], +})); + +/* + * OUTPUTS + */ + +const output = []; + +if (isDev) { + output.push({ + name: pkg.name, + format: 'iife', + file: `${outputDir}${pkg.name}.js`, + }); +} else { + output.push({ + name: pkg.name, + format: 'cjs', + file: `${outputDir}${pkg.name}.cjs`, + }); + output.push({ + name: pkg.name, + format: 'iife', + file: `${outputDir}${pkg.name}.min.js`, + plugins: [terser({ output: { comments: false } })], + }); +} + +/* + * EXPORT + */ + +export default { + input: inputFile, + output, + plugins, +}; diff --git a/rollup/plugins.js b/rollup/plugins.js deleted file mode 100644 index 0d40e59..0000000 --- a/rollup/plugins.js +++ /dev/null @@ -1,56 +0,0 @@ -import path from 'path'; - -/* eslint-disable import/no-extraneous-dependencies */ -import postcssPluginBanner from 'postcss-banner'; -import postcssPluginCssnano from 'cssnano'; -import rollupPluginLicense from 'rollup-plugin-license'; -import rollupPluginPostcss from 'rollup-plugin-postcss'; -import rollupPluginStrip from '@rollup/plugin-strip'; -import { terser as rollupPluginTerser } from 'rollup-plugin-terser'; -import rollupPluginTypescript from '@rollup/plugin-typescript'; - -// TODO: remove rule after changing the Rollup config -// eslint-disable-next-line import/no-dynamic-require -const pkg = require(path.join(process.cwd(), 'package.json')); -const bannerText = `${pkg.name} v${pkg.version} by Nikita Karamov -${pkg.homepage}`; - -export const license = () => rollupPluginLicense({ - banner: { - commentStyle: 'ignored', - content: bannerText, - }, -}); - -/** - * @param {boolean|string} file - * @param {boolean} minify - */ -export const postcss = (file = true, minify) => rollupPluginPostcss({ - extract: file, - plugins: [ - minify && postcssPluginCssnano({ - preset: 'default', - }), - postcssPluginBanner({ - banner: bannerText, - important: true, - }), - ], -}); - -export const strip = () => rollupPluginStrip({ - debugger: true, - include: ['**/*.js', '**/*.ts'], - functions: ['console.log', 'console.debug', 'assert.*'], - sourceMap: false, -}); - -export const terser = () => rollupPluginTerser({ - output: { - comments: false, - ecma: 5, - }, -}); - -export const typescript = () => rollupPluginTypescript(); diff --git a/rollup/rollup.config.dev.js b/rollup/rollup.config.dev.js deleted file mode 100644 index f60dbd2..0000000 --- a/rollup/rollup.config.dev.js +++ /dev/null @@ -1,18 +0,0 @@ -import { postcss, typescript } from './plugins'; - -const input = './src/index.ts'; -const name = 'shareon'; -const outputDir = './dev/'; - -export default { - input, - output: { - name, - format: 'iife', - file: `${outputDir}${name}.js`, - }, - plugins: [ - typescript(), - postcss(`${name}.css`, false), - ], -}; diff --git a/rollup/rollup.config.prod.js b/rollup/rollup.config.prod.js deleted file mode 100644 index a32f3b2..0000000 --- a/rollup/rollup.config.prod.js +++ /dev/null @@ -1,30 +0,0 @@ -import { - license, postcss, strip, terser, typescript, -} from './plugins'; - -const input = './src/index.ts'; -const name = 'shareon'; -const outputDir = './dist/'; - -export default { - input, - output: [ - { - name, - format: 'cjs', - file: `${outputDir}${name}.cjs`, - }, - { - name, - format: 'iife', - file: `${outputDir}${name}.min.js`, - plugins: [terser()], - }, - ], - plugins: [ - typescript(), - strip(), - postcss(`${name}.min.css`, true), - license(), - ], -};