shareon/rollup.config.js

110 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2021-01-17 14:32:59 -05:00
import { join, resolve } from 'path';
import buble from '@rollup/plugin-buble';
2020-07-31 12:54:51 -04:00
import consts from '@nickkaramoff/rollup-plugin-consts';
2020-06-26 10:51:13 -04:00
import strip from '@rollup/plugin-strip';
2021-01-17 14:32:59 -05:00
import postcss from 'rollup-plugin-postcss';
2020-06-26 10:51:13 -04:00
import { terser } from 'rollup-plugin-terser';
2021-01-17 14:32:59 -05:00
const autoprefixer = require('autoprefixer');
const banner = require('postcss-banner');
const calc = require('postcss-calc');
const cssnano = require('cssnano');
const cssVariables = require('postcss-css-variables');
const mixins = require('postcss-mixins');
const networks = require('./src/networksMixin');
const { urlBuilderMap } = require('./src/networks');
2021-01-17 14:32:59 -05:00
const pkg = require('./package.json');
2020-07-31 12:54:51 -04:00
2020-06-26 10:51:13 -04:00
const isDev = process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'development';
2021-01-17 14:32:59 -05:00
const outputDir = resolve('.', 'dist');
const bannerText = `${pkg.name} v${pkg.version}`;
2020-06-26 10:51:13 -04:00
2021-01-17 14:32:59 -05:00
const postcssPlugins = [
mixins({
mixins: {
networks,
},
}),
cssVariables,
calc,
];
2020-06-26 10:51:13 -04:00
2021-01-17 14:32:59 -05:00
if (!isDev) {
postcssPlugins.push(
cssnano({
preset: 'default',
}),
autoprefixer(),
banner({
banner: bannerText,
important: true,
inline: true,
}),
);
}
2020-06-26 10:51:13 -04:00
2021-01-17 14:32:59 -05:00
const getPlugins = (css) => [
2020-07-31 12:54:51 -04:00
consts({
urlBuilderMap,
}),
2021-01-17 14:32:59 -05:00
css && postcss({
extract: resolve(join(outputDir, 'shareon.min.css')),
plugins: postcssPlugins,
}),
(!isDev) && strip({
2020-06-26 10:51:13 -04:00
debugger: true,
include: ['**/*.js'],
2020-06-26 10:51:13 -04:00
functions: ['console.log', 'console.debug', 'assert.*'],
sourceMap: false,
}),
(!isDev) && buble({
transforms: {
modules: false,
2020-06-26 10:51:13 -04:00
},
}),
];
2020-06-26 10:51:13 -04:00
const getOutput = (baseDir) => {
2020-07-30 06:18:27 -04:00
const defaultParameters = {
name: pkg.name,
exports: 'default',
};
2020-07-23 05:13:21 -04:00
return [
{
2020-07-30 06:18:27 -04:00
...defaultParameters,
2020-07-26 08:17:16 -04:00
format: 'iife',
file: join(baseDir, `${pkg.name}${isDev ? '' : '.min'}.js`),
plugins: isDev ? [] : [terser({ output: { comments: /^!/ } })],
2021-01-17 14:32:59 -05:00
banner: `/*! ${bannerText} */`,
},
(!isDev) && {
2020-07-30 06:18:27 -04:00
...defaultParameters,
2020-07-26 08:17:16 -04:00
format: 'cjs',
file: join(baseDir, `${pkg.name}.cjs`),
2021-01-17 14:32:59 -05:00
banner: `/*! ${bannerText} */`,
},
(!isDev) && {
2020-07-30 06:18:27 -04:00
...defaultParameters,
format: 'esm',
file: join(baseDir, `${pkg.name}.mjs`),
2021-01-17 14:32:59 -05:00
banner: `/*! ${bannerText} */`,
},
];
};
export default [
{
input: join(__dirname, 'src', 'autoinit.js'),
output: getOutput(outputDir),
2021-01-17 14:32:59 -05:00
plugins: getPlugins(true),
},
{
2021-01-17 14:32:59 -05:00
input: join(__dirname, 'src', 'shareon.js'),
output: getOutput(join(outputDir, 'noinit')),
2021-01-17 14:32:59 -05:00
plugins: getPlugins(false),
},
];