Skip to content

Commit 77439a2

Browse files
author
Daniel Del Core
committed
implements alternate module loader
1 parent 5bcbaa0 commit 77439a2

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"start:dev": "ts-node src/index.ts"
1818
},
1919
"dependencies": {
20+
"@antfu/install-pkg": "^0.1.1",
2021
"@hypermod/core": "^0.2.0",
2122
"@hypermod/fetcher": "^0.5.0",
2223
"@hypermod/initializer": "^0.5.2",

packages/cli/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ program
6666
'--registryToken <value>',
6767
'Define an authentication token to use as credentials for the registry',
6868
)
69+
.option(
70+
'--experimental-loader',
71+
'Enables the experimental package downloader',
72+
)
6973
.addOption(
7074
new Option(
7175
'--verbose <parser>',

packages/cli/src/main.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import chalk from 'chalk';
55
import findUp from 'find-up';
66
import inquirer from 'inquirer';
77
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
8+
import { installPackage } from '@antfu/install-pkg';
89

910
import * as core from '@hypermod/core';
1011
import { CodeshiftConfig } from '@hypermod/types';
@@ -15,6 +16,14 @@ import { fetchPackages } from './utils/fetch-package';
1516
import { mergeConfigs } from './utils/merge-configs';
1617
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';
1718

19+
const ExperimentalModuleLoader = () => ({
20+
install: async (packageName: string) => await installPackage(packageName),
21+
require: (packageName: string) => require(packageName),
22+
getInfo: (packageName: string) => ({
23+
location: require.resolve(packageName),
24+
}),
25+
});
26+
1827
export default async function main(
1928
paths: string[],
2029
flags: Partial<core.Flags>,
@@ -26,7 +35,7 @@ export default async function main(
2635
}
2736

2837
const pluginManagerConfig: Partial<PluginManagerOptions> = {
29-
pluginsPath: path.join(__dirname, '..', 'node_modules'),
38+
pluginsPath: path.join(__dirname, '..', '.tmp', 'node_modules'),
3039
};
3140

3241
// If a registry is provided in the CLI flags, use it for the pluginManagers configuration.
@@ -41,7 +50,9 @@ export default async function main(
4150
};
4251
}
4352

44-
const packageManager = new PluginManager(pluginManagerConfig);
53+
const packageManager = flags.experimentalLoader
54+
? ExperimentalModuleLoader()
55+
: new PluginManager(pluginManagerConfig);
4556

4657
let transforms: string[] = [];
4758

@@ -193,6 +204,7 @@ export default async function main(
193204

194205
const { community, remote } = await fetchPackages(
195206
pkgName,
207+
// @ts-expect-error Experimental loader
196208
packageManager,
197209
);
198210

packages/core/src/runner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ function getAllFiles(paths: string[], filter: (name: string) => boolean) {
153153
});
154154
}
155155

156-
export function run(entrypointPath: string, paths: string[], options: Flags) {
156+
export function run(
157+
entrypointPath: string,
158+
paths: string[],
159+
options: Omit<Flags, 'experimentalLoader'>,
160+
) {
157161
const cpus = options.cpus
158162
? Math.min(availableCpus, options.cpus)
159163
: availableCpus;

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ export interface Flags {
6161
silent: boolean;
6262
stdin: boolean;
6363
verbose?: 0 | 1 | 2;
64+
experimentalLoader: boolean;
6465
}

0 commit comments

Comments
 (0)