shareon/rollup.config.js

107 lines
2.4 KiB
JavaScript
Raw Normal View History

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