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

feat: Allow dependencies to be bundled

After this, only peerDependencies are externals.

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-04-13 10:43:01 +03:00
parent f127a07916
commit 35965505e7
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
6 changed files with 94 additions and 19 deletions

4
package-lock.json generated
View File

@ -37282,6 +37282,7 @@
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/webpack-node-externals/-/webpack-node-externals-3.0.0.tgz",
"integrity": "sha512-LnL6Z3GGDPht/AigwRh2dvL9PQPFQ8skEpVrWZXLWBYmqcaojHNN0onvHzie6rq7EWKrrBfPYqNEzTJgiwEQDQ==",
"dev": true,
"engines": {
"node": ">=6"
}
@ -42278,8 +42279,7 @@
"tailwindcss": "^3.3.1",
"ts-loader": "^9.4.1",
"webpack": "^5.77.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
"webpack-cli": "^4.10.0"
}
},
"packages/infrastructure/webpack/node_modules/@types/estree": {

View File

@ -33,7 +33,6 @@
"tailwindcss": "^3.3.1",
"ts-loader": "^9.4.1",
"webpack": "^5.77.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
"webpack-cli": "^4.10.0"
}
}

View File

@ -1,4 +1,5 @@
const ForkTsCheckerPlugin = require("fork-ts-checker-webpack-plugin");
const { MakePeerDependenciesExternalPlugin } = require("./plugins/make-peer-dependencies-external");
const { ProtectFromImportingNonDependencies } = require("./plugins/protect-from-importing-non-dependencies");
module.exports = ({ entrypointFilePath, outputDirectory }) => ({
@ -17,6 +18,7 @@ module.exports = ({ entrypointFilePath, outputDirectory }) => ({
},
plugins: [
new MakePeerDependenciesExternalPlugin(),
new ProtectFromImportingNonDependencies(),
new ForkTsCheckerPlugin({
@ -46,21 +48,6 @@ module.exports = ({ entrypointFilePath, outputDirectory }) => ({
libraryTarget: "commonjs2",
},
externals: [
nodeExternals({ modulesFromFile: true }),
nodeExternals({
modulesDir: path.resolve(
__dirname,
"..",
"..",
"..",
"..",
"node_modules"
),
}),
],
externalsPresets: { node: true },
node: {

View File

@ -0,0 +1,29 @@
const ExternalModuleFactoryPlugin = require("webpack/lib/ExternalModuleFactoryPlugin");
const path = require("path");
const {
toModuleMatcherRegExp,
} = require("./to-module-matcher-reg-exp/to-module-matcher-reg-exp");
class MakePeerDependenciesExternalPlugin {
apply(compiler) {
compiler.hooks.compile.tap("compile", (params) => {
const peerDependencies = getPeerDependencies();
new ExternalModuleFactoryPlugin(
compiler.options.output.library.type,
peerDependencies.map(toModuleMatcherRegExp)
).apply(params.normalModuleFactory);
});
}
}
const getPeerDependencies = () => {
const pathToPackageJson = path.resolve(process.cwd(), "package.json");
const packageJson = require(pathToPackageJson);
return Object.keys(packageJson.peerDependencies || {});
};
module.exports = { MakePeerDependenciesExternalPlugin };

View File

@ -0,0 +1,3 @@
const toModuleMatcherRegExp = x => new RegExp(`^${x}(/.*)*$`);
module.exports = { toModuleMatcherRegExp };

View File

@ -0,0 +1,57 @@
import { toModuleMatcherRegExp } from "./to-module-matcher-reg-exp";
describe('to-module-matcher-reg-exp', () => {
let regExp;
beforeEach(() => {
regExp = toModuleMatcherRegExp("some-package");
});
it('given exactly matching package, matches', () => {
const targetString = 'some-package';
const [match] = targetString.match(regExp);
expect(match).toBeTruthy()
});
it('given matching package with entrypoint, matches', () => {
const targetString = 'some-package/some-entrypoint';
const [match] = targetString.match(regExp);
expect(match).toBeTruthy()
});
it('given matching package with directory, matches', () => {
const targetString = 'some-package/some-directory/some-other-directory';
const [match] = targetString.match(regExp);
expect(match).toBeTruthy()
});
it('given package that starts with same name but is still different, does not match', () => {
const targetString = 'some-package-but-still-different';
const actual = targetString.match(regExp);
expect(actual).toBeNull()
});
it('given package that starts with something else, does not match', () => {
const targetString = 'different-some-package';
const actual = targetString.match(regExp);
expect(actual).toBeNull()
});
it('given irrelevant package, does not match', () => {
const targetString = 'some-other-package';
const actual = targetString.match(regExp);
expect(actual).toBeNull()
});
});