Skip to content

Commit 401823f

Browse files
author
Daniel Del Core
committed
Fixes fetcher with typescript safe imports via configs
1 parent ea20de1 commit 401823f

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

.changeset/fair-flowers-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hypermod/cli': patch
3+
---
4+
5+
Patches selection of transform via local config prompt

.changeset/spotty-parrots-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hypermod/fetcher': minor
3+
---
4+
5+
Adds typescript support for config requires. Now a config can be in TS/TSX etc.

packages/cli/src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ export default async function main(
164164
if (config.transforms && config.transforms[answers.codemod]) {
165165
Object.entries(config.transforms)
166166
.filter(([key]) => semver.satisfies(key, `>=${answers.codemod}`))
167-
.forEach(([, path]) => transforms.push(path));
167+
.forEach(([, codemod]) =>
168+
transforms.push(`${configFilePath}@${codemod}`),
169+
);
168170
} else if (config.presets && config.presets[answers.codemod]) {
169-
transforms.push(config.presets[answers.codemod]);
171+
transforms.push(`${configFilePath}#${answers.codemod}`);
170172
}
171173
}
172174
}

packages/fetcher/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
"license": "MIT",
88
"repository": "https://github.com/hypermod-io/hypermod-community/tree/main/packages/fetcher",
99
"dependencies": {
10+
"@babel/core": "^7.13.16",
11+
"@babel/parser": "^7.13.16",
12+
"@babel/plugin-proposal-class-properties": "^7.13.0",
13+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8",
14+
"@babel/plugin-proposal-optional-chaining": "^7.13.12",
15+
"@babel/plugin-transform-modules-commonjs": "^7.13.8",
16+
"@babel/preset-flow": "^7.13.13",
17+
"@babel/preset-typescript": "^7.13.16",
18+
"@babel/register": "^7.13.16",
1019
"@hypermod/types": "*",
20+
"babel-core": "^7.0.0-bridge.0",
1121
"chalk": "^4.1.0",
1222
"fs-extra": "^9.1.0",
1323
"globby": "^11.1.0",

packages/fetcher/src/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
import fs from 'fs';
23
import path from 'path';
34
import globby from 'globby';
45
import { PluginManager } from 'live-plugin-manager';
56

67
import { Config } from '@hypermod/types';
78

9+
// This configuration allows us to require TypeScript config files directly
10+
const { DEFAULT_EXTENSIONS } = require('@babel/core');
11+
const presets = [];
12+
13+
let presetEnv;
14+
try {
15+
presetEnv = require('@babel/preset-env');
16+
presets.push([presetEnv.default, { targets: { node: true } }]);
17+
} catch (_) {}
18+
19+
require('@babel/register')({
20+
configFile: false,
21+
babelrc: false,
22+
presets: [...presets, require('@babel/preset-typescript').default],
23+
plugins: [
24+
require('@babel/plugin-transform-class-properties').default,
25+
require('@babel/plugin-transform-nullish-coalescing-operator').default,
26+
require('@babel/plugin-transform-optional-chaining').default,
27+
require('@babel/plugin-transform-modules-commonjs').default,
28+
require('@babel/plugin-transform-private-methods').default,
29+
],
30+
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
31+
// By default, babel register only compiles things inside the current working directory.
32+
// https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
33+
ignore: [
34+
// Ignore parser related files
35+
/@babel\/parser/,
36+
/\/flow-parser\//,
37+
/\/recast\//,
38+
/\/ast-types\//,
39+
],
40+
});
41+
842
export interface ConfigMeta {
943
filePath: string;
1044
config: Config;

0 commit comments

Comments
 (0)