Skip to content

Commit c09c712

Browse files
authored
Add package for sharing webpack configurations for upcoming Features (lensapp#7199)
* Add package for shared webpack configurations for Lens applications, Features and libraries Signed-off-by: Janne Savolainen <[email protected]> * Remove duplication from gitignores Signed-off-by: Janne Savolainen <[email protected]> --------- Signed-off-by: Janne Savolainen <[email protected]>
1 parent 73230c8 commit c09c712

File tree

21 files changed

+624
-120
lines changed

21 files changed

+624
-120
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ docs/extensions/api
77
site/
88
lerna-debug.log
99
coverage
10+
dist
11+
node_modules

package-lock.json

+78-102
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/bump-version-for-cron/.gitignore

-2
This file was deleted.

packages/core/.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
static/build/
22
build/webpack/
33
binaries/
4-
dist/
5-
node_modules/

packages/ensure-binaries/.gitignore

-2
This file was deleted.

packages/extension-api/.gitignore

-2
This file was deleted.

packages/generate-tray-icons/.gitignore

-2
This file was deleted.
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# @k8slens/webpack
2+
3+
This package contains webpack configurations for Lens packages.
4+
5+
## Install
6+
7+
```
8+
$ npm install @k8slens/webpack
9+
```
10+
11+
## Features
12+
13+
### Configurations
14+
15+
### Node package
16+
This configuration should be used when creating package that will be executed within **Node** environment.
17+
18+
**webpack.config.js**
19+
```javascript
20+
module.exports = require("@k8slens/webpack").configForNode;
21+
```
22+
### React package
23+
This configuration should be used when creating package tha will be executed within **Browser** environment.
24+
25+
**webpack.config.js**
26+
```javascript
27+
module.exports = require("@k8slens/webpack").configForReact;
28+
```
29+
30+
### Multi export package
31+
32+
This configuration should be used when package contains **multiple entrypoint** e.g. for different environments. You need to add `lensMultiExportConfig` to `package.json` with configuration. Note that also `exports` property needs to be set, but the correct values are generated from `lensMultiExportConfig` when using `lens-build` -script.
33+
34+
**webpack.config.js**
35+
```javascript
36+
const packageJson = require("./package.json");
37+
38+
module.exports = require("@k8slens/webpack").getMultiExportConfig(packageJson);
39+
```
40+
41+
**package.json**
42+
```json
43+
{
44+
"lensMultiExportConfig": {
45+
"./main": {
46+
"buildType": "node",
47+
"entrypoint": "./src/main/index.ts"
48+
},
49+
"./renderer": {
50+
"buildType": "react",
51+
"entrypoint": "./src/renderer/index.ts"
52+
}
53+
},
54+
55+
"exports": {
56+
"./main": {
57+
"types": "./dist/main/index.d.ts",
58+
"require": "./dist/main/index.js",
59+
"import": "./dist/main/index.js",
60+
"default": "./dist/main/index.js"
61+
},
62+
"./renderer": {
63+
"types": "./dist/renderer/index.d.ts",
64+
"require": "./dist/renderer/index.js",
65+
"import": "./dist/renderer/index.js",
66+
"default": "./dist/renderer/index.js"
67+
}
68+
}
69+
}
70+
```
71+
72+
## Scripts
73+
74+
1. `lens-build` which builds the packages
75+
2. `lens-remove-build` which removes the build directory from packages. It's useful for cleaning up.
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set -e
2+
webpack $@
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rm -rfv build
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
configForNode: require("./src/node-config"),
3+
configForReact: require("./src/react-config"),
4+
getMultiExportConfig: require("./src/get-multi-export-config"),
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { configForNode } =
2+
require("@k8slens/jest").monorepoPackageConfig(__dirname);
3+
4+
module.exports = {
5+
...configForNode,
6+
7+
collectCoverageFrom: [...configForNode.collectCoverageFrom],
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@k8slens/webpack",
3+
"private": false,
4+
"version": "0.0.1",
5+
"description": "Webpack configurations and scripts for Lens packages.",
6+
"type": "commonjs",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/lensapp/lens.git"
10+
},
11+
"main": "index.js",
12+
"author": {
13+
"name": "OpenLens Authors",
14+
"email": "[email protected]"
15+
},
16+
"license": "MIT",
17+
"homepage": "https://github.com/lensapp/lens",
18+
"bin": {
19+
"lens-build": "bin/build.sh",
20+
"lens-remove-build": "bin/remove-build.sh"
21+
},
22+
"scripts": {
23+
"test:unit": "lens-test"
24+
},
25+
"dependencies": {
26+
"@types/webpack-env": "^1.18.0",
27+
"css-loader": "^6.7.2",
28+
"mini-css-extract-plugin": "^2.7.0",
29+
"sass-loader": "^13.2.0",
30+
"style-loader": "^3.3.1",
31+
"ts-loader": "^9.4.1",
32+
"webpack": "^5.75.0",
33+
"webpack-cli": "^4.10.0",
34+
"webpack-node-externals": "^3.0.0"
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const nodeConfig = require("./node-config");
2+
const reactConfig = require("./react-config");
3+
const path = require("path");
4+
const {
5+
map,
6+
isEqual,
7+
keys,
8+
fromPairs,
9+
toPairs,
10+
reject,
11+
values,
12+
nth,
13+
filter,
14+
} = require("lodash/fp");
15+
const { pipeline } = require("@ogre-tools/fp");
16+
17+
module.exports = (packageJson, dependencies = { nodeConfig, reactConfig }) => {
18+
if (!packageJson.lensMultiExportConfig) {
19+
throw new Error(
20+
`Tried to get multi export config for package "${packageJson.name}" but configuration is missing.`
21+
);
22+
}
23+
24+
const validBuildTypes = ["node", "react"];
25+
26+
const invalidBuildTypes = pipeline(
27+
packageJson.lensMultiExportConfig,
28+
values,
29+
map((config) => config.buildType),
30+
reject((buildType) => validBuildTypes.includes(buildType))
31+
);
32+
33+
if (invalidBuildTypes.length > 0) {
34+
throw new Error(
35+
`Tried to get multi export config for package "${
36+
packageJson.name
37+
}" but build types "${invalidBuildTypes.join(
38+
'", "'
39+
)}" were not any of "${validBuildTypes.join('", "')}".`
40+
);
41+
}
42+
43+
const exportsWithMissingEntrypoint = pipeline(
44+
packageJson.lensMultiExportConfig,
45+
toPairs,
46+
filter(([, config]) => !config.entrypoint),
47+
map(nth(0))
48+
);
49+
50+
if (exportsWithMissingEntrypoint.length > 0) {
51+
throw new Error(
52+
`Tried to get multi export config for package "${
53+
packageJson.name
54+
}" but entrypoint was missing for "${exportsWithMissingEntrypoint.join(
55+
'", "'
56+
)}".`
57+
);
58+
}
59+
60+
const expectedExports = pipeline(
61+
packageJson.lensMultiExportConfig,
62+
keys,
63+
map(toExpectedExport),
64+
fromPairs
65+
);
66+
67+
if (!isEqual(expectedExports, packageJson.exports)) {
68+
throw new Error(
69+
`Tried to get multi export config but exports of package.json for "${
70+
packageJson.name
71+
}" did not match exactly:\n\n${JSON.stringify(expectedExports, null, 2)}`
72+
);
73+
}
74+
75+
return pipeline(
76+
packageJson.lensMultiExportConfig,
77+
toPairs,
78+
map(toExportSpecificWebpackConfigFor(dependencies))
79+
);
80+
};
81+
82+
const toExpectedExport = (externalImportPath) => {
83+
const entrypointPath = `./${path.join(
84+
"./dist",
85+
externalImportPath,
86+
"index.js"
87+
)}`;
88+
89+
return [
90+
externalImportPath,
91+
{
92+
types: `./${path.join("./dist", externalImportPath, "index.d.ts")}`,
93+
94+
default: entrypointPath,
95+
import: entrypointPath,
96+
require: entrypointPath,
97+
},
98+
];
99+
};
100+
101+
const toExportSpecificWebpackConfigFor =
102+
(dependencies) =>
103+
([externalImportPath, { buildType, entrypoint }]) => {
104+
const baseConfig =
105+
buildType === "node" ? dependencies.nodeConfig : dependencies.reactConfig;
106+
107+
return {
108+
...baseConfig,
109+
name: entrypoint,
110+
111+
entry: {
112+
index: entrypoint,
113+
},
114+
115+
output: {
116+
...baseConfig.output,
117+
path: path.join(baseConfig.output.path, externalImportPath),
118+
},
119+
};
120+
};

0 commit comments

Comments
 (0)