|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
1 | 2 | import fs from 'fs'; |
2 | 3 | import path from 'path'; |
3 | 4 | import globby from 'globby'; |
4 | 5 | import { PluginManager } from 'live-plugin-manager'; |
5 | 6 |
|
6 | | -import { CodeshiftConfig } from '@hypermod/types'; |
| 7 | +import { Config } from '@hypermod/types'; |
| 8 | + |
| 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 | +}); |
7 | 41 |
|
8 | 42 | export interface ConfigMeta { |
9 | 43 | filePath: string; |
10 | | - config: CodeshiftConfig; |
| 44 | + config: Config; |
11 | 45 | } |
12 | 46 |
|
13 | | -function resolveConfigExport(pkg: any): CodeshiftConfig { |
| 47 | +function resolveConfigExport(pkg: any): Config { |
14 | 48 | return pkg.default ? pkg.default : pkg; |
15 | 49 | } |
16 | 50 |
|
@@ -62,9 +96,7 @@ export async function fetchConfigs(filePath: string): Promise<ConfigMeta[]> { |
62 | 96 | return configs; |
63 | 97 | } |
64 | 98 |
|
65 | | -export async function fetchConfigAtPath( |
66 | | - filePath: string, |
67 | | -): Promise<CodeshiftConfig> { |
| 99 | +export async function fetchConfigAtPath(filePath: string): Promise<Config> { |
68 | 100 | const resolvedFilePath = path.resolve(filePath); |
69 | 101 | const exists = fs.existsSync(resolvedFilePath); |
70 | 102 |
|
|
0 commit comments