Skip to content

Commit

Permalink
Fixes fetcher with typescript safe imports via configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Del Core committed Feb 7, 2024
1 parent ea20de1 commit 401823f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-flowers-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypermod/cli': patch
---

Patches selection of transform via local config prompt
5 changes: 5 additions & 0 deletions .changeset/spotty-parrots-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypermod/fetcher': minor
---

Adds typescript support for config requires. Now a config can be in TS/TSX etc.
6 changes: 4 additions & 2 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ export default async function main(
if (config.transforms && config.transforms[answers.codemod]) {
Object.entries(config.transforms)
.filter(([key]) => semver.satisfies(key, `>=${answers.codemod}`))
.forEach(([, path]) => transforms.push(path));
.forEach(([, codemod]) =>
transforms.push(`${configFilePath}@${codemod}`),
);
} else if (config.presets && config.presets[answers.codemod]) {
transforms.push(config.presets[answers.codemod]);
transforms.push(`${configFilePath}#${answers.codemod}`);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/fetcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
"license": "MIT",
"repository": "https://github.com/hypermod-io/hypermod-community/tree/main/packages/fetcher",
"dependencies": {
"@babel/core": "^7.13.16",
"@babel/parser": "^7.13.16",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8",
"@babel/plugin-proposal-optional-chaining": "^7.13.12",
"@babel/plugin-transform-modules-commonjs": "^7.13.8",
"@babel/preset-flow": "^7.13.13",
"@babel/preset-typescript": "^7.13.16",
"@babel/register": "^7.13.16",
"@hypermod/types": "*",
"babel-core": "^7.0.0-bridge.0",
"chalk": "^4.1.0",
"fs-extra": "^9.1.0",
"globby": "^11.1.0",
Expand Down
34 changes: 34 additions & 0 deletions packages/fetcher/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import fs from 'fs';
import path from 'path';
import globby from 'globby';
import { PluginManager } from 'live-plugin-manager';

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

// This configuration allows us to require TypeScript config files directly
const { DEFAULT_EXTENSIONS } = require('@babel/core');
const presets = [];

let presetEnv;
try {
presetEnv = require('@babel/preset-env');
presets.push([presetEnv.default, { targets: { node: true } }]);
} catch (_) {}

require('@babel/register')({
configFile: false,
babelrc: false,
presets: [...presets, require('@babel/preset-typescript').default],
plugins: [
require('@babel/plugin-transform-class-properties').default,
require('@babel/plugin-transform-nullish-coalescing-operator').default,
require('@babel/plugin-transform-optional-chaining').default,
require('@babel/plugin-transform-modules-commonjs').default,
require('@babel/plugin-transform-private-methods').default,
],
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
// By default, babel register only compiles things inside the current working directory.
// https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
ignore: [
// Ignore parser related files
/@babel\/parser/,
/\/flow-parser\//,
/\/recast\//,
/\/ast-types\//,
],
});

export interface ConfigMeta {
filePath: string;
config: Config;
Expand Down

0 comments on commit 401823f

Please sign in to comment.