shareon/rollup.config.js

128 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-07-31 12:54:51 -04:00
import consts from '@nickkaramoff/rollup-plugin-consts';
2020-06-26 10:51:13 -04:00
import license from 'rollup-plugin-license';
import postcss from 'rollup-plugin-postcss';
import postcssPluginBanner from 'postcss-banner';
2020-07-31 12:54:51 -04:00
import postcssPluginCalc from 'postcss-calc';
2020-06-26 10:51:13 -04:00
import postcssPluginCssnano from 'cssnano';
2020-07-31 12:54:51 -04:00
import postcssPluginMixins from 'postcss-mixins';
import postcssPluginVariables from 'postcss-css-variables';
2020-06-26 10:51:13 -04:00
import strip from '@rollup/plugin-strip';
import { terser } from 'rollup-plugin-terser';
2020-07-31 12:54:51 -04:00
import { urlBuilderMap } from './src/networks';
import networksMixin from './src/networksMixin';
2020-06-26 10:51:13 -04:00
const isDev = process.env.ROLLUP_WATCH || process.env.NODE_ENV === 'development';
const pkg = require('./package.json');
2020-07-26 08:17:16 -04:00
const outputDir = isDev ? './dev/' : './dist/';
2020-06-26 10:51:13 -04:00
const bannerText = `${pkg.name} v${pkg.version} by Nikita Karamov\n${pkg.homepage}`;
/**
* Plugins to build the project
*
* @type {Plugin[]}
2020-06-26 10:51:13 -04:00
*/
const plugins = [
2020-07-31 12:54:51 -04:00
consts({
urlBuilderMap,
}),
];
2020-06-26 10:51:13 -04:00
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({
2020-07-04 16:10:58 -04:00
extract: `${pkg.name}.min.css`,
2020-06-26 10:51:13 -04:00
plugins: [
2020-07-31 12:54:51 -04:00
postcssPluginMixins({
mixins: {
networks: networksMixin,
},
}),
postcssPluginVariables(),
postcssPluginCalc(),
2020-06-26 10:51:13 -04:00
(!isDev) && postcssPluginCssnano({
preset: 'default',
}),
postcssPluginBanner({
banner: bannerText,
important: true,
}),
],
}));
/**
* @typedef {import('rollup').OutputOptions} OutputOptions
2020-06-26 10:51:13 -04:00
*/
/**
*
* @param {string} baseDir base directory for the output files
* @return {OutputOptions[]} array of outputs
*/
const getOutputs = (baseDir) => {
2020-07-30 06:18:27 -04:00
const defaultParameters = {
name: pkg.name,
exports: 'default',
};
const result = [];
2020-07-23 05:13:21 -04:00
if (isDev) {
result.push({
2020-07-30 06:18:27 -04:00
...defaultParameters,
2020-07-26 08:17:16 -04:00
format: 'iife',
file: `${baseDir}${pkg.name}.js`,
});
} else {
2020-07-26 08:17:16 -04:00
result.push({
2020-07-30 06:18:27 -04:00
...defaultParameters,
2020-07-26 08:17:16 -04:00
format: 'cjs',
file: `${baseDir}${pkg.name}.cjs`,
});
result.push({
2020-07-30 06:18:27 -04:00
...defaultParameters,
format: 'esm',
2020-07-26 08:17:16 -04:00
file: `${baseDir}${pkg.name}.mjs`,
});
result.push({
2020-07-30 06:18:27 -04:00
...defaultParameters,
2020-07-26 08:17:16 -04:00
format: 'iife',
file: `${baseDir}${pkg.name}.min.js`,
2020-07-23 05:13:21 -04:00
plugins: [terser({ output: { comments: false } })],
});
}
2020-07-23 05:13:21 -04:00
return result;
};
const config = [
{
2020-07-31 12:18:17 -04:00
input: './src/autoinit.js',
output: getOutputs(`${outputDir}`),
plugins,
},
{
2020-07-31 12:18:17 -04:00
input: './src/shareon.js',
output: getOutputs(`${outputDir}noinit/`),
plugins: plugins.slice(0, -1),
},
];
2020-06-26 10:51:13 -04:00
2020-07-23 05:13:21 -04:00
export default config;