Skip to content

Commit

Permalink
Add CJS file to /fn entry and fix legacy builds (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Nov 15, 2023
1 parent ea938e6 commit 3fe4be1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
"version": "0.4.5",
"description": "Let’s get serious about color",
"files": [
"dist/color.cjs",
"dist/color.cjs.map",
"dist/color.js",
"dist/color.js.map",
"dist/color.legacy.cjs",
"dist/color.legacy.cjs.map",
"dist/color.legacy.js",
"dist/color.legacy.js.map",
"dist/",
"src/",
"types/dist/",
"types/src/",
Expand All @@ -24,7 +17,8 @@
},
"./fn": {
"types": "./types/src/index-fn.d.ts",
"import": "./src/index-fn.js"
"import": "./src/index-fn.js",
"require": "./dist/color-fn.cjs"
},
"./dist/*": "./dist/*"
},
Expand Down
54 changes: 38 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import terser from "@rollup/plugin-terser";

let bundles = [
const bundles = [
{
"file": "dist/color.global.js",
"format": "iife",
Expand All @@ -20,22 +20,44 @@ let bundles = [
},
];

const fnBundles = [
{
"file": "dist/color-fn.cjs",
"format": "cjs",
"sourcemap": true,
"exports": "named",
},
];

// Add minified versions of every bundle
bundles = bundles.flatMap(bundle => {
let minBundle = Object.assign({}, bundle);
minBundle.file = minBundle.file.replace(/\.\w+$/, ".min$&");
minBundle.plugins ||= [];
minBundle.plugins.push(terser());
const addMinBundle = (bundles) => {
return bundles.flatMap(bundle => {
const minBundle = Object.assign({}, bundle);
minBundle.file = minBundle.file.replace(/\.\w+$/, ".min$&");
minBundle.plugins ||= [];
minBundle.plugins.push(terser());

return [bundle, minBundle];
});
return [bundle, minBundle];
});
};

export default {
input: "src/index.js",
output: bundles,
onwarn (warning, rollupWarn) {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
rollupWarn(warning);
export default [
{
input: "src/index.js",
output: addMinBundle(bundles),
onwarn (warning, rollupWarn) {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
rollupWarn(warning);
}
}
}
};
},
{
input: "src/index-fn.js",
output: addMinBundle(fnBundles),
onwarn (warning, rollupWarn) {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
rollupWarn(warning);
}
}
},
];
20 changes: 11 additions & 9 deletions rollup.legacy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { nodeResolve } from "@rollup/plugin-node-resolve";
import defaultConfig from "./rollup.config.js";

const legacyPlugins = [
commonjs(),
nodeResolve(),
commonjs({ strictRequires: true }),
nodeResolve({ ignoreSideEffectsForRoot: true }),
babel({ babelHelpers: "bundled", exclude: "node_modules/**" }),
];

export default Object.assign(defaultConfig, {
output: defaultConfig.output.map(bundle => ({
...bundle,
file: bundle.file.replace(/\.(?:min\.)?\w+$/, ".legacy$&"),
})),
plugins: [...(defaultConfig.plugins || []), ...legacyPlugins]
});
export default defaultConfig.map(config =>
Object.assign(config, {
output: config.output.map(bundle => ({
...bundle,
file: bundle.file.replace(/\.(?:min\.)?\w+$/, ".legacy$&"),
})),
plugins: [...(config.plugins || []), ...legacyPlugins]
})
);

0 comments on commit 3fe4be1

Please sign in to comment.