Skip to content

Commit 4d3b134

Browse files
Fix rollup build (#18)
* Declare ES "module" in package.json * Provide CommonJS output as fall-back * Declare all dependencies as external to allow tree-shaking by consumers
1 parent 0b86620 commit 4d3b134

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- declaration of ES module in package.json
10+
- provide CommonJS output as fall-back
811

912
## [3.0.0] - 2019-10-09
1013
### Changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"prepublishOnly": "rm -rf build && npm run lint && npm run test --bail && npm run build"
2020
},
2121
"main": "build/index.js",
22+
"module": "build/index.es.js",
2223
"files": [
2324
"/build"
2425
],
@@ -61,9 +62,9 @@
6162
"rollup-plugin-babel": "~4.3.3",
6263
"rollup-plugin-commonjs": "~10.1.0",
6364
"rollup-plugin-node-resolve": "~5.2.0",
64-
"rollup-plugin-peer-deps-external": "~2.2.0",
6565
"rollup-plugin-postcss": "~2.0.3",
6666
"rollup-plugin-terser": "~5.1.2",
67+
"rollup-plugin-uglify": "~6.0.3",
6768
"sass-loader": "~8.0.0",
6869
"style-loader": "~1.0.0",
6970
"stylelint": "~11.1.1",

rollup.config.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,64 @@
11
import babel from "rollup-plugin-babel";
22
import commonjs from "rollup-plugin-commonjs";
3-
import external from "rollup-plugin-peer-deps-external";
43
import postcss from "rollup-plugin-postcss";
54
import resolve from "rollup-plugin-node-resolve";
65
import { terser } from "rollup-plugin-terser";
6+
import { uglify } from "rollup-plugin-uglify";
77

88
import packageJSON from "./package.json";
99

10+
const input = "./src/index.js";
11+
const external = [
12+
// ensure peer dependencies are declared as externals
13+
...Object.keys(packageJSON.peerDependencies),
14+
// same for actual dependencies, to allow for dependency resolution/de-duplication by consumers
15+
...Object.keys(packageJSON.dependencies)
16+
];
17+
const postcssOptions = {
18+
extract: false,
19+
extensions: [".scss"],
20+
minimize: true
21+
};
22+
const babelOptions = {
23+
exclude: "node_modules/**"
24+
};
25+
1026
export default [
27+
// CommonJS
1128
{
12-
input: "./src/index.js",
29+
input,
1330
output: {
1431
file: packageJSON.main,
32+
format: "cjs"
33+
},
34+
external,
35+
plugins: [
36+
// collect styles from SCSS, minimise them and include them in the JS module (i.e. not as separate .css file)
37+
postcss(postcssOptions),
38+
// transpile for compatibility
39+
babel(babelOptions),
40+
// resolve dependencies that are ES modules
41+
resolve(),
42+
// resolve dependencies that are (legacy) CommonJS modules
43+
commonjs(),
44+
// minify to reduce size
45+
uglify()
46+
]
47+
},
48+
// ES Module
49+
{
50+
input,
51+
output: {
52+
file: packageJSON.module,
1553
format: "es",
1654
exports: "named"
1755
},
56+
external,
1857
plugins: [
19-
// ensure peer dependencies are declared as externals
20-
external(),
2158
// collect styles from SCSS, minimise them and include them in the JS module (i.e. not as separate .css file)
22-
postcss({
23-
extract: false,
24-
extensions: [".scss"],
25-
minimize: true
26-
}),
59+
postcss(postcssOptions),
2760
// transpile for compatibility
28-
babel({
29-
exclude: "node_modules/**"
30-
}),
61+
babel(babelOptions),
3162
// resolve dependencies that are ES modules
3263
resolve(),
3364
// resolve dependencies that are (legacy) CommonJS modules

0 commit comments

Comments
 (0)