New Rollup configuration

pull/12/head
Nikita Karamov 2020-06-26 16:51:13 +02:00
parent c2bf29fff2
commit 93dc9d0b84
No known key found for this signature in database
GPG Key ID: E40DFE6E993540FF
5 changed files with 91 additions and 106 deletions

View File

@ -28,8 +28,8 @@
}, },
"unpkg": "./dist/shareon.min.js", "unpkg": "./dist/shareon.min.js",
"scripts": { "scripts": {
"build": "rollup -c ./rollup/rollup.config.prod.js", "build": "rollup -c",
"dev": "rollup -w -c ./rollup/rollup.config.dev.js", "dev": "rollup -w -c",
"pretest": "run-s build", "pretest": "run-s build",
"test:lint": "eslint-ci --ext .js,.ts ./src/", "test:lint": "eslint-ci --ext .js,.ts ./src/",
"test:size": "size-limit", "test:size": "size-limit",

89
rollup.config.js Normal file
View File

@ -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,
};

View File

@ -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();

View File

@ -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),
],
};

View File

@ -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(),
],
};