Skip to content

Commit 347b1f1

Browse files
authored
Update build script to handle bad inputs and update the contributor list. (#215)
1 parent 7b0525b commit 347b1f1

File tree

9 files changed

+226
-28
lines changed

9 files changed

+226
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
dist
55
.DS_Store
66
package-lock.json
7+
.idea

config/jest/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// noinspection JSUnusedGlobalSymbols
12
import { dirname, join } from "path";
23
import { fileURLToPath } from "url";
34

config/jest/jest.environment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// noinspection JSUnusedGlobalSymbols
12
import Environment from "jest-environment-jsdom-global";
23
import { TextDecoder, TextEncoder } from "util";
34

config/rollup/rollup.config.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// noinspection JSUnusedGlobalSymbols
2+
import replace from "@rollup/plugin-replace";
13
import terser from "@rollup/plugin-terser";
24
import typescript from "@rollup/plugin-typescript";
35
import { dirname, join } from "path";
@@ -18,31 +20,60 @@ const plugins = [
1820
typescript: typescriptEngine,
1921
tsconfig: join(fileDirectory, "..", "typescript", "tsconfig.json")
2022
}),
21-
terser({
22-
format: {
23-
comments: false
23+
replace({
24+
preventAssignment: true,
25+
values: {
26+
"process.env.NODE_ENV": JSON.stringify("production")
2427
}
28+
}),
29+
terser({
30+
format: { comments: false }
2531
})
2632
];
2733

34+
/**
35+
* Generate a valid rollup bundle configuration.
36+
*
37+
* @param {string} filename
38+
* @param {string} format
39+
*
40+
* @returns {import("rollup").RollupOptions}
41+
*/
2842
function createBundleConfiguration(filename, format) {
29-
/** @type {import("rollup").RollupOptions} */
43+
const lowercaseFormat = format.toLowerCase();
44+
45+
if (![packageJSON.module, packageJSON.browser].includes(filename)) {
46+
throw new Error(`Invalid filename provided. Received: ${filename}`);
47+
}
48+
49+
if (!["esm", "cjs"].includes(lowercaseFormat)) {
50+
throw new Error(`Unrecognised output format provided. Received: ${format}`);
51+
}
52+
53+
if (lowercaseFormat === "cjs" && filename !== packageJSON.browser) {
54+
throw new Error("A CJS bundle can only be created for the main bundle.");
55+
}
56+
57+
if (lowercaseFormat === "esm" && filename !== packageJSON.module) {
58+
throw new Error("An ESM bundle can only be created for the module bundle.");
59+
}
60+
3061
return {
31-
input,
32-
plugins,
3362
external,
63+
input,
64+
onwarn(warning) {
65+
throw new Error(warning.message);
66+
},
3467
output: {
3568
file: filename,
3669
format,
3770
sourcemap: true
3871
},
39-
onwarn: warning => {
40-
throw new Error(warning.message);
41-
}
72+
plugins
4273
};
4374
}
4475

4576
const ESM = createBundleConfiguration(packageJSON.module, "esm");
46-
const CJS = createBundleConfiguration(packageJSON.main, "cjs");
77+
const CJS = createBundleConfiguration(packageJSON.browser, "cjs");
4778

4879
export default [ESM, CJS];

config/webpack/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// noinspection JSUnusedGlobalSymbols
12
import HtmlWebpackPlugin from "html-webpack-plugin";
23
import { dirname, join } from "path";
34
import { fileURLToPath } from "url";

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/P5-wrapper/react",
66
"license": "MIT",
77
"type": "module",
8-
"main": "dist/components/index.js",
8+
"browser": "dist/components/index.js",
99
"module": "dist/components/index.esm.js",
1010
"types": "dist/components/index.d.ts",
1111
"files": [
@@ -21,7 +21,7 @@
2121
"format:check": "pnpm prettier --check .",
2222
"format": "pnpm prettier --write .",
2323
"lint:fix": "pnpm lint --fix",
24-
"lint": "eslint --config config/eslint/eslint.json src/ --ignore-path .gitignore",
24+
"lint": "eslint --config config/eslint/eslint.json . --ignore-path .gitignore",
2525
"prettier": "prettier --config config/prettier/prettier.json --ignore-path .gitignore",
2626
"release": "pnpm build && gh-pages -d dist/demo && pnpm publish",
2727
"start": "webpack serve --config config/webpack/webpack.config.js --mode development",
@@ -45,14 +45,23 @@
4545
},
4646
{
4747
"name": "Andreas Wolf",
48-
"email": "[email protected]"
48+
"email": "[email protected]",
49+
"url": "https://github.com/and-who"
4950
},
5051
{
5152
"name": "Ivan Malyugin"
5253
},
5354
{
5455
"name": "Benjamin Saphier",
5556
"url": "https://github.com/bsaphier"
57+
},
58+
{
59+
"name": "Mark Fuller",
60+
"url": "https://github.com/MarkFuller1"
61+
},
62+
{
63+
"name": "Nathan Manousos",
64+
"url": "https://github.com/trafnar"
5665
}
5766
],
5867
"repository": {
@@ -63,8 +72,6 @@
6372
"url": "https://github.com/P5-wrapper/react/issues"
6473
},
6574
"dependencies": {
66-
"@rollup/plugin-terser": "^0.3.0",
67-
"@rollup/plugin-typescript": "^11.0.0",
6875
"microdiff": "^1.3.1",
6976
"p5": "^1.5.0"
7077
},
@@ -77,6 +84,9 @@
7784
"@babel/preset-env": "^7.20.2",
7885
"@babel/preset-react": "^7.18.6",
7986
"@babel/preset-typescript": "^7.18.6",
87+
"@rollup/plugin-replace": "^5.0.2",
88+
"@rollup/plugin-terser": "^0.3.0",
89+
"@rollup/plugin-typescript": "^11.0.0",
8090
"@testing-library/react": "^13.4.0",
8191
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
8292
"@types/jest": "^29.2.3",
@@ -85,6 +95,7 @@
8595
"@types/react-dom": "^18.0.9",
8696
"@typescript-eslint/eslint-plugin": "^5.44.0",
8797
"@typescript-eslint/parser": "^5.44.0",
98+
"@vue/compiler-sfc": "3",
8899
"babel-loader": "^9.1.0",
89100
"canvas": "^2.10.2",
90101
"css-loader": "^6.7.2",

0 commit comments

Comments
 (0)