Skip to content

Commit

Permalink
removes dependency whitelisting from hypermod.configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Del Core committed Mar 6, 2024
1 parent 30a1424 commit 2c9ae26
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-bags-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypermod/cli': minor
---

Dependency fetching is now done via the loader module on install via reading a small config in a package.json.
5 changes: 5 additions & 0 deletions .changeset/old-sloths-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypermod/fetcher': minor
---

Removes whitelist dependency fetching, this approach no longer works.
51 changes: 38 additions & 13 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,54 @@ import { mergeConfigs } from './utils/merge-configs';
import { fetchConfigsForWorkspaces, getPackageJson } from './utils/file-system';
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';

const ExperimentalModuleLoader = () => ({
install: async (packageName: string) =>
await installPackage(packageName, {
cwd: __dirname,
packageManager: 'npm',
additionalArgs: ['--force'],
}),
require: (packageName: string) => require(packageName),
getInfo: (packageName: string) => {
const ExperimentalModuleLoader = () => {
const getInfo = (packageName: string) => {
const entryPath = require.resolve(packageName);
const location = entryPath.split(packageName)[0] + packageName;
const packageJsonRaw = fs.readFileSync(
const pkgJsonRaw = fs.readFileSync(
path.join(location, 'package.json'),
'utf8',
);
const pkgJson = JSON.parse(pkgJsonRaw);

return {
location,
entryPath,
pkgJson: JSON.parse(packageJsonRaw),
pkgJson,
};
},
});
};

const install = async (packageName: string) => {
await installPackage(packageName, {
cwd: __dirname,
packageManager: 'npm',
additionalArgs: ['--force'],
});

const { pkgJson } = getInfo(packageName);

// Install whitelisted devDependencies
if (pkgJson?.hypermod?.dependencies) {
await Promise.all(
pkgJson.hypermod.dependencies.map((dep: string) => {
const version = pkgJson.devDependencies[dep];
if (!version) return;
return installPackage(`${dep}@${version}`, {
cwd: __dirname,
packageManager: 'npm',
additionalArgs: ['--force'],
});
}),
);
}
};

return {
install,
getInfo,
require: (packageName: string) => require(packageName),
};
};

export default async function main(
paths: string[],
Expand Down
20 changes: 0 additions & 20 deletions packages/fetcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,6 @@ export async function fetchRemotePackage(
const pkg = packageManager.require(packageName);
const configExport = resolveConfigExport(pkg);

// Install whitelisted deps
if (
// @ts-expect-error legacy module loader doesn't know about these properties
info.pkgJson &&
// @ts-expect-error legacy module loader doesn't know about these properties
info.pkgJson.devDependencies &&
configExport.dependencies
) {
await Promise.all(
configExport.dependencies.map(dep => {
// @ts-expect-error legacy module loader doesn't know about these properties
const version = info.pkgJson.devDependencies[dep];

if (!version) return;

return packageManager.install(`${dep}@${version}`);
}),
);
}

if (configExport.transforms || configExport.presets) {
return {
filePath: info.location,
Expand Down
12 changes: 0 additions & 12 deletions website/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,3 @@ Targets list the packages that the codemod package targets.
This is useful for Hypermod packages that have codemods targeting multiple related packages at the same time, such as packages from a monorepo.

For example: `targets: ['@foo/bar', '@foo/baz']`

### `dependencies`

(Experimental feature, this will only work when the `--experimental-loader` flag is specified)

A list of dependencies to be installed before running the transform. These are useful when co-locating codemods with an existing
package and want to specify a whitelist of `devDependencies` to be installed only when run via `@hypermod/cli`.
This avoids codemod-related dependencies from unnecessarily increasing the bundlesize for regular consumers of the package.

Note: the versions installed are based on what's specified in the `package.json`

Example: `dependencies: ['@hypermod/utils', 'postcss', 'postcss-less']`

0 comments on commit 2c9ae26

Please sign in to comment.