1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Merge branch 'master' into enhancement-ability-to-remove-subnamespaces

This commit is contained in:
Alex Andreev 2023-02-22 15:56:08 +03:00 committed by GitHub
commit bd59c1f075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 2564 additions and 334 deletions

3
.gitignore vendored
View File

@ -6,3 +6,6 @@ lens.log
docs/extensions/api
site/
lerna-debug.log
coverage
dist
node_modules

4
jest.config.js Normal file
View File

@ -0,0 +1,4 @@
const { monorepoRootConfig } = require("@k8slens/jest");
module.exports = monorepoRootConfig(__dirname);

2001
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,7 @@
"mkdocs:serve-local": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -it -p 8000:8000 -v ${PWD}:/docs mkdocs-serve-local:latest",
"mkdocs:verify": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -v ${PWD}:/docs mkdocs-serve-local:latest build --strict",
"test:unit": "lerna run --stream test:unit",
"test:unit:watch": "jest --watch",
"test:integration": "lerna run --stream test:integration",
"bump-version": "lerna version --no-git-tag-version --no-push",
"precreate-release-pr": "cd packages/release-tool && npm run build",

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -48,7 +48,7 @@ try {
});
}
await exec(`npm run bump-version --yes ${newVersion}`);
await exec(`npm run bump-version ${newVersion} -- --yes`);
} catch (error) {
console.error(error);
process.exit(1);

View File

@ -1,5 +1,3 @@
static/build/
build/webpack/
binaries/
dist/
node_modules/

View File

@ -126,7 +126,7 @@
"dependencies": {
"@astronautlabs/jsonpath": "^1.1.0",
"@hapi/call": "^9.0.1",
"@hapi/subtext": "^7.0.4",
"@hapi/subtext": "^7.1.0",
"@k8slens/node-fetch": "^6.4.0-beta.13",
"@kubernetes/client-node": "^0.18.1",
"@material-ui/styles": "^4.11.5",

View File

@ -3031,7 +3031,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -3108,7 +3108,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -3234,7 +3234,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
class="Select__menu-portal css-1no2yl0-MenuPortal"
>
<div
class="theme-outlined install-chart-version-select-for-some-first-tab-id-options Select__menu css-1nmdiq5-menu"
class="theme-dark install-chart-version-select-for-some-first-tab-id-options Select__menu css-1nmdiq5-menu"
id="react-select-install-chart-version-select-for-some-first-tab-id-listbox"
>
<div
@ -4073,7 +4073,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -4148,7 +4148,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -5089,7 +5089,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -5164,7 +5164,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -6100,7 +6100,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -6175,7 +6175,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -7111,7 +7111,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -7186,7 +7186,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -8144,7 +8144,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -8219,7 +8219,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -8347,7 +8347,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
class="Select__menu-portal css-1no2yl0-MenuPortal"
>
<div
class="NamespaceSelectMenu theme-outlined install-chart-namespace-select-for-some-first-tab-id-options Select__menu css-1nmdiq5-menu"
class="NamespaceSelectMenu theme-dark install-chart-namespace-select-for-some-first-tab-id-options Select__menu css-1nmdiq5-menu"
id="react-select-install-chart-namespace-select-for-some-first-tab-id-listbox"
>
<div
@ -9186,7 +9186,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -9261,7 +9261,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -10197,7 +10197,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -10272,7 +10272,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -13798,7 +13798,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -13873,7 +13873,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -15957,7 +15957,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -16034,7 +16034,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -17022,7 +17022,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -17097,7 +17097,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -18033,7 +18033,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -18108,7 +18108,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -19044,7 +19044,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -19119,7 +19119,7 @@ exports[`installing helm chart from new tab given tab for installing chart was n
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"

View File

@ -1133,7 +1133,7 @@ exports[`installing helm chart from previously opened tab given tab for installi
Version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -1208,7 +1208,7 @@ exports[`installing helm chart from previously opened tab given tab for installi
Namespace
</span>
<div
class="Select theme-outlined NamespaceSelect css-b62m3t-container"
class="Select theme-dark NamespaceSelect css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"

View File

@ -4103,7 +4103,7 @@ exports[`New Upgrade Helm Chart Dock Tab given a namespace is selected when navi
Upgrade version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"
@ -5098,7 +5098,7 @@ exports[`New Upgrade Helm Chart Dock Tab given a namespace is selected when navi
Upgrade version
</span>
<div
class="Select theme-outlined chart-version css-b62m3t-container"
class="Select theme-dark chart-version css-b62m3t-container"
>
<span
class="css-1f43avz-a11yText-A11yText"

View File

@ -78,7 +78,7 @@ class NonInjectedDeploymentReplicaSets extends React.Component<DeploymentReplica
sortSyncWithUrl={false}
className="box grow"
>
<TableHead flat>
<TableHead flat sticky={false}>
<TableCell className="name" sortBy={sortBy.name}>Name</TableCell>
<TableCell className="warning"/>
<TableCell className="namespace" sortBy={sortBy.namespace}>Namespace</TableCell>

View File

@ -190,7 +190,7 @@ class NonInjectedPodDetailsList extends React.Component<PodDetailsListProps & De
)}
className="box grow"
>
<TableHead flat>
<TableHead flat sticky={virtual}>
<TableCell className="name" sortBy={sortBy.name}>Name</TableCell>
<TableCell className="warning"/>
<TableCell className="node" sortBy={sortBy.node}>Node</TableCell>

View File

@ -142,7 +142,6 @@ class NonInjectedCreateResource extends React.Component<CreateResourceProps & De
options={this.props.createResourceTemplates.get()}
formatGroupLabel={group => group.label}
menuPlacement="top"
themeName="outlined"
onChange={(option) => {
if (option) {
this.props.createResourceTabStore.setData(this.tabId, option.value);

View File

@ -93,14 +93,12 @@ const NonInjectedInstallChart = observer(
options={version.options.get()}
onChange={(changed) => version.onChange(changed?.value)}
menuPlacement="top"
themeName="outlined"
id={`install-chart-version-select-for-${tabId}`}
/>
<span>Namespace</span>
<NamespaceSelect
showIcons={false}
menuPlacement="top"
themeName="outlined"
value={namespace.value.get()}
onChange={namespace.onChange}
id={`install-chart-namespace-select-for-${tabId}`}

View File

@ -81,7 +81,6 @@ export class NonInjectedUpgradeChart extends React.Component<UpgradeChartProps &
id="char-version-input"
className="chart-version"
menuPlacement="top"
themeName="outlined"
value={model.version.value.get()}
options={model.versionOptions.get()}
onChange={model.version.set}

View File

@ -43,10 +43,6 @@ html {
}
}
&__value-container {
margin-bottom: 1px;
}
&__single-value {
color: var(--textColorSecondary);
overflow: visible;
@ -93,6 +89,8 @@ html {
&__option {
white-space: nowrap;
cursor: pointer;
border-radius: 3px;
padding: 6px 8px;
&:active {
background: var(--primary);
@ -179,21 +177,6 @@ html {
}
}
&.theme-outlined {
.Select__control {
box-shadow: 0 0 0 1px var(--colorVague);
color: var(--primary);
.Select__value-container {
padding: 0 $padding * 0.5;
}
.Select__dropdown-indicator {
padding: 3px;
}
}
}
&.theme-lens {
:hover {
&.Select__control {

View File

@ -38,7 +38,6 @@
}
&.flat {
background-color: transparent;
font-weight: 600;
}
}

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -0,0 +1,55 @@
# @k8slens/jest
This package contains jest configurations and scripts for Lens packages.
## Install
```
$ npm install @k8slens/jest
```
## Features
### Package configurations
Shared configurations for minimal duplication.
#### Node
**./packages/<any-package>/jest.config.js**
```javascript
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
```
#### React
**./packages/<any-package>/jest.config.js**
```javascript
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
```
### Root configuration
You may want to enable testing of packages using single command from root level. This allows you to utilize `jest --watch` between all packages.
**./jest.config.js**
```javascript
module.exports = require("@k8slens/jest").monorepoRootConfig(__dirname);
```
### Scripts
#### lens-test
Test package with coverage enforcement. Automatically opens coverage report in case of failure.
**./packages/<any-package>/package.json**
```json
{
"scripts": {
"test": "lens-test"
}
}
```

View File

@ -0,0 +1,6 @@
jest --coverage --runInBand
result=$?
[ $result != 0 ] && [ -v $CI ] && open ./coverage/lcov-report/index.html
exit $result

View File

@ -0,0 +1,4 @@
module.exports = {
monorepoRootConfig: require("./monorepo-root-config"),
monorepoPackageConfig: require("./monorepo-package-config"),
};

View File

@ -0,0 +1,55 @@
module.exports = (rootDir) => {
const shared = {
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest", { cwd: rootDir }],
},
clearMocks: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
coverageReporters: ["lcov"],
collectCoverage: true,
testMatch: ["**/?(*.)+(test).{js,ts,tsx}"],
watchPathIgnorePatterns: ["/node_modules/", "/coverage/", "/build/"],
collectCoverageFrom: [
"<rootDir>/src/**/*.{ts,tsx}",
"!<rootDir>/src/**/*.no-coverage.ts",
],
moduleNameMapper: {
"^electron$": "identity-obj-proxy",
},
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
};
const configForNode = {
...shared,
testEnvironment: "node",
};
const configForReact = {
...shared,
moduleNameMapper: {
"\\.(css|scss)$": "identity-obj-proxy",
...shared.moduleNameMapper,
},
testEnvironment: "jsdom",
setupFilesAfterEnv: [`${__dirname}/setup-react-tests.ts`],
};
return {
configForReact,
configForNode,
};
};

View File

@ -0,0 +1,73 @@
const path = require("path");
const glob = require("glob");
const { omit } = require("lodash/fp");
const getProjectColor = (projectNumber) => {
const colors = [
"red",
"green",
"yellow",
"magenta",
"cyan",
"white",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright",
];
return colors[projectNumber % colors.length];
};
const nonMultiProjectConfigs = [
"coverageDirectory",
"coverageProvider",
"coverageReporters",
"collectCoverage",
"collectCoverageFrom",
"coveragePathIgnorePatterns",
"coverageThreshold",
];
const toJestMultiProjectConfig = (
{ packageJson, jestConfig, packagePath },
projectNumber
) => ({
rootDir: packagePath,
displayName: {
name: packageJson.name,
color: getProjectColor(projectNumber),
},
...omit(nonMultiProjectConfigs, jestConfig),
});
const getJestConfigsAndPackageJsons = (rootDir) => {
const packageJsonPaths = glob
.sync(`${rootDir}/packages/**/jest.config.js`, {
ignore: "./**/node_modules/**/*",
})
.map((filePath) => path.dirname(filePath));
return packageJsonPaths.map((packagePath) => ({
packagePath,
packageJson: require(`${packagePath}/package.json`),
jestConfig: require(`${packagePath}/jest.config.js`),
}));
};
module.exports = (rootDir) => ({
projects: getJestConfigsAndPackageJsons(rootDir).map(
toJestMultiProjectConfig
),
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname",
"jest-watch-select-projects",
],
});

View File

@ -0,0 +1,35 @@
{
"name": "@k8slens/jest",
"private": false,
"version": "0.0.1",
"description": "Jest configuration and scripts for Lens packages.",
"type": "commonjs",
"repository": {
"type": "git",
"url": "git+https://github.com/lensapp/lens.git"
},
"main": "index.js",
"author": {
"name": "OpenLens Authors",
"email": "info@k8slens.dev"
},
"license": "MIT",
"homepage": "https://github.com/lensapp/lens",
"bin": {
"lens-test": "bin/test.sh"
},
"dependencies": {
"@swc/core": "^1.3.20",
"@swc/jest": "^0.2.23",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/jest": "^29.2.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"jest-watch-select-projects": "^2.0.0",
"jest-watch-typeahead": "^2.2.1",
"lodash": "^4.17.21",
"ts-jest": "^29.0.3"
}
}

View File

@ -0,0 +1 @@
import "@testing-library/jest-dom";

View File

@ -0,0 +1,76 @@
# @k8slens/webpack
This package contains webpack configurations for Lens packages.
## Install
```
$ npm install @k8slens/webpack
```
## Features
### Configurations
### Node package
This configuration should be used when creating package that will be executed within **Node** environment.
**webpack.config.js**
```javascript
module.exports = require("@k8slens/webpack").configForNode;
```
### React package
This configuration should be used when creating package tha will be executed within **Browser** environment.
**webpack.config.js**
```javascript
module.exports = require("@k8slens/webpack").configForReact;
```
### Multi export package
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.
**webpack.config.js**
```javascript
const packageJson = require("./package.json");
module.exports = require("@k8slens/webpack").getMultiExportConfig(packageJson);
```
**package.json**
```json
{
"lensMultiExportConfig": {
"./main": {
"buildType": "node",
"entrypoint": "./src/main/index.ts"
},
"./renderer": {
"buildType": "react",
"entrypoint": "./src/renderer/index.ts"
}
},
"exports": {
"./main": {
"types": "./dist/main/index.d.ts",
"require": "./dist/main/index.js",
"import": "./dist/main/index.js",
"default": "./dist/main/index.js"
},
"./renderer": {
"types": "./dist/renderer/index.d.ts",
"require": "./dist/renderer/index.js",
"import": "./dist/renderer/index.js",
"default": "./dist/renderer/index.js"
}
}
}
```
## Scripts
1. `lens-build` which builds the packages
2. `lens-remove-build` which removes the build directory from packages. It's useful for cleaning up.

View File

@ -0,0 +1,2 @@
set -e
webpack $@

View File

@ -0,0 +1 @@
rm -rfv build

View File

@ -0,0 +1,5 @@
module.exports = {
configForNode: require("./src/node-config"),
configForReact: require("./src/react-config"),
getMultiExportConfig: require("./src/get-multi-export-config"),
};

View File

@ -0,0 +1,8 @@
const { configForNode } =
require("@k8slens/jest").monorepoPackageConfig(__dirname);
module.exports = {
...configForNode,
collectCoverageFrom: [...configForNode.collectCoverageFrom],
};

View File

@ -0,0 +1,36 @@
{
"name": "@k8slens/webpack",
"private": false,
"version": "0.0.1",
"description": "Webpack configurations and scripts for Lens packages.",
"type": "commonjs",
"repository": {
"type": "git",
"url": "git+https://github.com/lensapp/lens.git"
},
"main": "index.js",
"author": {
"name": "OpenLens Authors",
"email": "info@k8slens.dev"
},
"license": "MIT",
"homepage": "https://github.com/lensapp/lens",
"bin": {
"lens-build": "bin/build.sh",
"lens-remove-build": "bin/remove-build.sh"
},
"scripts": {
"test:unit": "lens-test"
},
"dependencies": {
"@types/webpack-env": "^1.18.0",
"css-loader": "^6.7.2",
"mini-css-extract-plugin": "^2.7.0",
"sass-loader": "^13.2.0",
"style-loader": "^3.3.1",
"ts-loader": "^9.4.1",
"webpack": "^5.75.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
}
}

View File

@ -0,0 +1,120 @@
const nodeConfig = require("./node-config");
const reactConfig = require("./react-config");
const path = require("path");
const {
map,
isEqual,
keys,
fromPairs,
toPairs,
reject,
values,
nth,
filter,
} = require("lodash/fp");
const { pipeline } = require("@ogre-tools/fp");
module.exports = (packageJson, dependencies = { nodeConfig, reactConfig }) => {
if (!packageJson.lensMultiExportConfig) {
throw new Error(
`Tried to get multi export config for package "${packageJson.name}" but configuration is missing.`
);
}
const validBuildTypes = ["node", "react"];
const invalidBuildTypes = pipeline(
packageJson.lensMultiExportConfig,
values,
map((config) => config.buildType),
reject((buildType) => validBuildTypes.includes(buildType))
);
if (invalidBuildTypes.length > 0) {
throw new Error(
`Tried to get multi export config for package "${
packageJson.name
}" but build types "${invalidBuildTypes.join(
'", "'
)}" were not any of "${validBuildTypes.join('", "')}".`
);
}
const exportsWithMissingEntrypoint = pipeline(
packageJson.lensMultiExportConfig,
toPairs,
filter(([, config]) => !config.entrypoint),
map(nth(0))
);
if (exportsWithMissingEntrypoint.length > 0) {
throw new Error(
`Tried to get multi export config for package "${
packageJson.name
}" but entrypoint was missing for "${exportsWithMissingEntrypoint.join(
'", "'
)}".`
);
}
const expectedExports = pipeline(
packageJson.lensMultiExportConfig,
keys,
map(toExpectedExport),
fromPairs
);
if (!isEqual(expectedExports, packageJson.exports)) {
throw new Error(
`Tried to get multi export config but exports of package.json for "${
packageJson.name
}" did not match exactly:\n\n${JSON.stringify(expectedExports, null, 2)}`
);
}
return pipeline(
packageJson.lensMultiExportConfig,
toPairs,
map(toExportSpecificWebpackConfigFor(dependencies))
);
};
const toExpectedExport = (externalImportPath) => {
const entrypointPath = `./${path.join(
"./dist",
externalImportPath,
"index.js"
)}`;
return [
externalImportPath,
{
types: `./${path.join("./dist", externalImportPath, "index.d.ts")}`,
default: entrypointPath,
import: entrypointPath,
require: entrypointPath,
},
];
};
const toExportSpecificWebpackConfigFor =
(dependencies) =>
([externalImportPath, { buildType, entrypoint }]) => {
const baseConfig =
buildType === "node" ? dependencies.nodeConfig : dependencies.reactConfig;
return {
...baseConfig,
name: entrypoint,
entry: {
index: entrypoint,
},
output: {
...baseConfig.output,
path: path.join(baseConfig.output.path, externalImportPath),
},
};
};

View File

@ -0,0 +1,184 @@
import getMultiExportConfig from "./get-multi-export-config";
describe("get-multi-export-config", () => {
let actual;
let maximalPackageJson;
beforeEach(() => {
maximalPackageJson = {
name: "some-name",
lensMultiExportConfig: {
".": {
buildType: "node",
entrypoint: "./index.ts",
},
"./some-entrypoint": {
buildType: "node",
entrypoint: "./some-entrypoint/index.ts",
},
"./some-other-entrypoint": {
buildType: "react",
entrypoint: "./some-other-entrypoint/index.ts",
},
},
exports: {
".": {
types: "./dist/index.d.ts",
require: "./dist/index.js",
import: "./dist/index.js",
default: "./dist/index.js",
},
"./some-entrypoint": {
types: "./dist/some-entrypoint/index.d.ts",
require: "./dist/some-entrypoint/index.js",
import: "./dist/some-entrypoint/index.js",
default: "./dist/some-entrypoint/index.js",
},
"./some-other-entrypoint": {
types: "./dist/some-other-entrypoint/index.d.ts",
require: "./dist/some-other-entrypoint/index.js",
import: "./dist/some-other-entrypoint/index.js",
default: "./dist/some-other-entrypoint/index.js",
},
},
};
});
it("given maximal package.json, when creating configuration, works", () => {
actual = getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
expect(actual).toEqual([
{
name: "./index.ts",
stub: "node",
entry: { index: "./index.ts" },
output: { some: "value", path: "/some-build-directory" },
},
{
name: "./some-entrypoint/index.ts",
stub: "node",
entry: { index: "./some-entrypoint/index.ts" },
output: {
some: "value",
path: "/some-build-directory/some-entrypoint",
},
},
{
name: "./some-other-entrypoint/index.ts",
stub: "react",
entry: { index: "./some-other-entrypoint/index.ts" },
output: {
some: "other-value",
path: "/some-build-directory/some-other-entrypoint",
},
},
]);
});
it("given maximal package.json but path for entrypoint in exports do not match output, when creating configuration, throws", () => {
maximalPackageJson.exports["./some-entrypoint"].default = "wrong-path";
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config but exports of package.json for "some-name" did not match exactly:'
);
});
it("given maximal package.json but exports do not match lens multi export config, when creating configuration, throws", () => {
maximalPackageJson.exports = {};
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config but exports of package.json for "some-name" did not match exactly:'
);
});
it("given maximal package.json but exports are missing, when creating configuration, throws", () => {
delete maximalPackageJson.exports;
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config but exports of package.json for "some-name" did not match exactly:'
);
});
it("given maximal package.json but lens multi export config is missing, when creating configuration, throws", () => {
delete maximalPackageJson.lensMultiExportConfig;
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config for package "some-name" but configuration is missing.'
);
});
it("given maximal package.json but a build type is incorrect, when creating configuration, throws", () => {
maximalPackageJson.lensMultiExportConfig["."].buildType = "some-invalid";
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config for package "some-name" but build types "some-invalid" were not any of "node", "react".'
);
});
it("given maximal package.json but entrypoint is missing, when creating configuration, throws", () => {
delete maximalPackageJson.lensMultiExportConfig["./some-entrypoint"]
.entrypoint;
expect(() => {
getMultiExportConfig(maximalPackageJson, {
nodeConfig: nodeConfigStub,
reactConfig: reactConfigStub,
});
}).toThrow(
'Tried to get multi export config for package "some-name" but entrypoint was missing for "./some-entrypoint".'
);
});
});
const nodeConfigStub = {
stub: "node",
output: {
some: "value",
path: "/some-build-directory",
},
};
const reactConfigStub = {
stub: "react",
output: {
some: "other-value",
path: "/some-build-directory",
},
};

View File

@ -0,0 +1,68 @@
const nodeExternals = require("webpack-node-externals");
const path = require("path");
const buildDirectory = path.resolve(process.cwd(), "dist");
module.exports = {
entry: { index: "./index.ts" },
target: "node",
mode: "production",
performance: {
maxEntrypointSize: 100000,
hints: "error",
},
resolve: {
extensions: [".ts", ".tsx"],
},
output: {
path: buildDirectory,
filename: (pathData) =>
pathData.chunk.name === "index"
? "index.js"
: `${pathData.chunk.name}/index.js`,
libraryTarget: "commonjs2",
},
externals: [
nodeExternals({ modulesFromFile: true }),
nodeExternals({
modulesDir: path.resolve(
__dirname,
"..",
"..",
"..",
"..",
"node_modules"
),
}),
],
externalsPresets: { node: true },
node: {
__dirname: true,
__filename: true,
},
module: {
rules: [
{
test: /\.ts(x)?$/,
loader: "ts-loader",
options: {
compilerOptions: {
declaration: true,
declarationDir: "./dist",
},
},
},
],
},
};

View File

@ -0,0 +1,44 @@
const nodeConfig = require("./node-config");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
...nodeConfig,
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
],
module: {
...nodeConfig.module,
rules: [
...nodeConfig.module.rules,
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: false,
modules: {
auto: /\.module\./i, // https://github.com/webpack-contrib/css-loader#auto
mode: "local", // :local(.selector) by default
localIdentName: "[name]__[local]--[hash:base64:5]",
},
},
},
{
loader: "sass-loader",
options: {
sourceMap: false,
},
},
],
},
],
},
};

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -1,6 +1,4 @@
static/build
binaries/
dist/
node_modules/
*.mjs
*.mjs.map

View File

@ -1,2 +0,0 @@
dist/
node_modules/

View File

@ -1,2 +0,0 @@
dist/
node_modules/