chore: Consolidate infrastructure of webpack to ts to make it scriptable

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Iku-tursoandJanne Savolainen committed 2023-05-03 09:31:24 +03:00
1 parent f2f6cc500c
commit 9a4b02becb
24 files changed
+230 -190

No files matched your search

@@ -1,9 +1,7 @@
const getDependencyName = (requireString) => {
export const getDependencyName = (requireString: string) => {
const [a, b] = requireString.split("/");
const scoped = a.startsWith("@");
return scoped ? `${a}/${b}` : a;
};
module.exports = { getDependencyName };
@@ -1,29 +0,0 @@
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 };
@@ -0,0 +1,26 @@
// @ts-ignore
import ExternalModuleFactoryPlugin from "webpack/lib/ExternalModuleFactoryPlugin";
import path from "path";
import { toModuleMatcherRegExp } from "./to-module-matcher-reg-exp/to-module-matcher-reg-exp";
import { readJsonSync } from "fs-extra";
export class MakePeerDependenciesExternalPlugin {
apply(compiler: any) {
compiler.hooks.compile.tap("compile", (params: any) => {
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 = readJsonSync(pathToPackageJson);
return Object.keys(packageJson.peerDependencies || {});
};
@@ -1,58 +0,0 @@
const path = require("path");
const { getDependencyName } = require("./get-dependency-name/get-dependency-name");
const pathToPackageJson = path.resolve(process.cwd(), "package.json");
class ProtectFromImportingNonDependencies {
apply(compiler) {
const dependencies = getDependenciesAndPeerDependencies();
const nodeModulesToBeResolved = new Set();
compiler.hooks.normalModuleFactory.tap("irrelevant", (normalModuleFactory) => {
normalModuleFactory.hooks.resolve.tap("irrelevant", (toBeResolved) => {
const isSassDependency = toBeResolved.request.endsWith(".scss");
const isLocalDependency = toBeResolved.request.startsWith(".");
const isDependencyOfDependency =
toBeResolved.context.includes("node_modules");
const dependencyName = getDependencyName(toBeResolved.request);
const dependencyWeAreInterested =
!isSassDependency && !isLocalDependency && !isDependencyOfDependency && dependencyName;
if (dependencyWeAreInterested) {
nodeModulesToBeResolved.add(dependencyName);
}
});
}
);
compiler.hooks.afterCompile.tap("compile", () => {
const notSpecifiedDependencies = [...nodeModulesToBeResolved].filter(
(x) => !dependencies.includes(x)
);
if (notSpecifiedDependencies.length) {
throw new Error(
`Tried to import dependencies that are not specified in the package.json "${pathToPackageJson}". Add "${notSpecifiedDependencies.join(
'", "'
)}" to dependencies or peerDependencies.`
);
}
});
}
}
const getDependenciesAndPeerDependencies = () => {
const packageJson = require(pathToPackageJson);
const dependencies = Object.keys(packageJson.dependencies || {});
const peerDependencies = Object.keys(packageJson.peerDependencies || {});
return [...dependencies, ...peerDependencies];
};
module.exports = {
ProtectFromImportingNonDependencies,
};
@@ -0,0 +1,63 @@
import path from "path";
import { getDependencyName } from "./get-dependency-name/get-dependency-name";
import { readJsonSync } from "fs-extra";
const pathToPackageJson = path.resolve(process.cwd(), "package.json");
export class ProtectFromImportingNonDependencies {
apply(compiler: any) {
const dependencies = getDependenciesAndPeerDependencies();
const nodeModulesToBeResolved = new Set();
compiler.hooks.normalModuleFactory.tap(
"irrelevant",
(normalModuleFactory: any) => {
normalModuleFactory.hooks.resolve.tap(
"irrelevant",
(toBeResolved: any) => {
const isSassDependency = toBeResolved.request.endsWith(".scss");
const isLocalDependency = toBeResolved.request.startsWith(".");
const isDependencyOfDependency =
toBeResolved.context.includes("node_modules");
const dependencyName = getDependencyName(toBeResolved.request);
const dependencyWeAreInterested =
!isSassDependency &&
!isLocalDependency &&
!isDependencyOfDependency &&
dependencyName;
if (dependencyWeAreInterested) {
nodeModulesToBeResolved.add(dependencyName);
}
}
);
}
);
compiler.hooks.afterCompile.tap("compile", () => {
const notSpecifiedDependencies = [...nodeModulesToBeResolved].filter(
(x: any) => !dependencies.includes(x)
);
if (notSpecifiedDependencies.length) {
throw new Error(
`Tried to import dependencies that are not specified in the package.json "${pathToPackageJson}". Add "${notSpecifiedDependencies.join(
'", "'
)}" to dependencies or peerDependencies.`
);
}
});
}
}
const getDependenciesAndPeerDependencies = () => {
const packageJson = readJsonSync(pathToPackageJson);
const dependencies = Object.keys(packageJson.dependencies || {});
const peerDependencies = Object.keys(packageJson.peerDependencies || {});
return [...dependencies, ...peerDependencies];
};
@@ -1,3 +0,0 @@
const toModuleMatcherRegExp = x => new RegExp(`^${x}(/.*)*$`);
module.exports = { toModuleMatcherRegExp };
@@ -1,7 +1,7 @@
import { toModuleMatcherRegExp } from "./to-module-matcher-reg-exp";
describe('to-module-matcher-reg-exp', () => {
let regExp;
let regExp: RegExp;
beforeEach(() => {
regExp = toModuleMatcherRegExp("some-package");
@@ -10,7 +10,7 @@ describe('to-module-matcher-reg-exp', () => {
it('given exactly matching package, matches', () => {
const targetString = 'some-package';
const [match] = targetString.match(regExp);
const [match] = targetString.match(regExp)!;
expect(match).toBeTruthy()
});
@@ -18,7 +18,7 @@ describe('to-module-matcher-reg-exp', () => {
it('given matching package with entrypoint, matches', () => {
const targetString = 'some-package/some-entrypoint';
const [match] = targetString.match(regExp);
const [match] = targetString.match(regExp)!;
expect(match).toBeTruthy()
});
@@ -26,7 +26,7 @@ describe('to-module-matcher-reg-exp', () => {
it('given matching package with directory, matches', () => {
const targetString = 'some-package/some-directory/some-other-directory';
const [match] = targetString.match(regExp);
const [match] = targetString.match(regExp)!;
expect(match).toBeTruthy()
});
@@ -0,0 +1 @@
export const toModuleMatcherRegExp = (x: string) => new RegExp(`^${x}(/.*)*$`);