diff --git a/.changeset/hungry-spies-build.md b/.changeset/hungry-spies-build.md new file mode 100644 index 00000000..68ef5e8b --- /dev/null +++ b/.changeset/hungry-spies-build.md @@ -0,0 +1,6 @@ +--- +'@hypermod/validator': patch +'@hypermod/cli': patch +--- + +Various configuration changes in support of ESM. diff --git a/jest.config.js b/jest.config.js index 239ff437..48a2e68c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,9 +1,22 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { + testEnvironment: 'node', transform: { - '^.+\\.ts$': [ + '\\.[jt]sx?$': [ 'ts-jest', { tsconfig: 'tsconfig.json', + diagnostics: { ignoreCodes: [1343] }, + astTransformers: { + before: [ + { + path: 'node_modules/ts-jest-mock-import-meta', + options: { + metaObjectReplacement: { url: 'https://www.url.com' }, + }, + }, + ], + }, }, ], }, @@ -15,6 +28,7 @@ module.exports = { 'jest-watch-typeahead/testname', ], moduleNameMapper: { + '(.+)\\.js$': '$1', '@hypermod/(.*)$': '/packages/$1/src', '@codeshift/(.*)$': '/packages/$1/src', }, diff --git a/package.json b/package.json index 6a81d8c0..c54fe1fc 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "semver": "^7.3.5", "tar": "^6.1.0", "ts-jest": "^29.2.5", + "ts-jest-mock-import-meta": "^1.2.0", "ts-loader": "^6.2.1", "ts-node": "^10.9.1", "tslib": "^2.2.0", diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index bfeb954e..bd076c4e 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -112,21 +112,33 @@ export default async function main( */ const configFilePath = await findUp([ 'hypermod.config.js', + 'hypermod.config.mjs', + 'hypermod.config.cjs', 'hypermod.config.ts', 'hypermod.config.tsx', 'src/hypermod.config.js', + 'src/hypermod.config.mjs', + 'src/hypermod.config.cjs', 'src/hypermod.config.ts', 'src/hypermod.config.tsx', 'codemods/hypermod.config.js', + 'codemods/hypermod.config.mjs', + 'codemods/hypermod.config.cjs', 'codemods/hypermod.config.ts', 'codemods/hypermod.config.tsx', 'codeshift.config.js', + 'codeshift.config.mjs', + 'codeshift.config.cjs', 'codeshift.config.ts', 'codeshift.config.tsx', 'src/codeshift.config.js', + 'src/codeshift.config.mjs', + 'src/codeshift.config.cjs', 'src/codeshift.config.ts', 'src/codeshift.config.tsx', 'codemods/codeshift.config.js', + 'codemods/codeshift.config.mjs', + 'codemods/codeshift.config.cjs', 'codemods/codeshift.config.ts', 'codemods/codeshift.config.tsx', ]); diff --git a/packages/fetcher/src/index.spec.ts b/packages/fetcher/src/index.spec.ts index ca191032..cab513d3 100644 --- a/packages/fetcher/src/index.spec.ts +++ b/packages/fetcher/src/index.spec.ts @@ -15,7 +15,7 @@ const mockBasePath = path.join(__dirname, 'path', 'to'); const mockConfig = { transforms: { - '10.0.0': 'path/to/transform.js', + '10.0.0': 'path/to/transform.ts', }, }; @@ -23,7 +23,7 @@ describe('fetcher', () => { let mockMatchedPaths: string[] = []; beforeEach(() => { - mockMatchedPaths = [path.join(mockBasePath, 'hypermod.config.js')]; + mockMatchedPaths = [path.join(mockBasePath, 'hypermod.config.ts')]; (globby as unknown as jest.Mock).mockImplementation(() => Promise.resolve(mockMatchedPaths), @@ -38,10 +38,10 @@ describe('fetcher', () => { describe('fetchConfig', () => { it('fetches config with default export', async () => { - const mockFilePath = `${__dirname}/path/to/hypermod.config.js`; + const mockFilePath = `${__dirname}/path/to/hypermod.config.ts`; jest.mock( - `${__dirname}/path/to/hypermod.config.js`, + `${__dirname}/path/to/hypermod.config.ts`, () => ({ __esModule: true, default: mockConfig }), { virtual: true }, ); @@ -54,7 +54,7 @@ describe('fetcher', () => { it('fetches config with named export', async () => { jest.mock( - path.join(mockBasePath, 'hypermod.config.js'), + path.join(mockBasePath, 'hypermod.config.ts'), () => mockConfig, { virtual: true, @@ -65,7 +65,7 @@ describe('fetcher', () => { expect(configMeta!.config).toEqual(mockConfig); expect(configMeta!.filePath).toEqual( - path.join(mockBasePath, 'hypermod.config.js'), + path.join(mockBasePath, 'hypermod.config.ts'), ); }); @@ -106,7 +106,7 @@ describe('fetcher', () => { describe('fetchPackage', () => { it('correctly fetches package and returns a config', async () => { - const mockFilePath = 'path/to/config.hypermod.js'; + const mockFilePath = 'path/to/config.hypermod.ts'; const mockPackageManager = { install: jest.fn(), getInfo: jest.fn().mockReturnValue({ location: mockFilePath }), @@ -154,7 +154,7 @@ describe('fetcher', () => { expect(configMeta!.config).toEqual(mockConfig); expect(configMeta!.filePath).toEqual( - mockBasePath + '/hypermod.config.js', + mockBasePath + '/hypermod.config.ts', ); }); @@ -177,7 +177,7 @@ describe('fetcher', () => { install: jest.fn(), require: jest.fn().mockReturnValueOnce(mockConfig), getInfo: jest.fn().mockReturnValue({ - location: mockBasePath + '/index.js', + location: mockBasePath + '/index.ts', }), }; @@ -187,7 +187,7 @@ describe('fetcher', () => { ); expect(configMeta!.config).toEqual(mockConfig); - expect(configMeta!.filePath).toEqual(mockBasePath + '/index.js'); + expect(configMeta!.filePath).toEqual(mockBasePath + '/index.ts'); }); it('throws if entrypoint-based config does not contain a valid config (and there are no config files available elsewhere)', async () => { @@ -195,7 +195,7 @@ describe('fetcher', () => { install: jest.fn(), require: jest.fn().mockReturnValueOnce({}), getInfo: jest.fn().mockReturnValue({ - location: mockBasePath + '/index.js', + location: mockBasePath + '/index.ts', }), }; diff --git a/packages/validator/src/index.ts b/packages/validator/src/index.ts index 1e279f7f..2e1b69ed 100644 --- a/packages/validator/src/index.ts +++ b/packages/validator/src/index.ts @@ -13,7 +13,7 @@ function hasValidPresets(config: Config): boolean { if (!config.presets) return true; return Object.entries(config.presets).every(([key]) => - key.match(/^[0-9a-zA-Z\-]+$/), + key.match(/^[0-9a-zA-Z\-\.]+$/), ); } diff --git a/yarn.lock b/yarn.lock index 8ae2faee..52408ee3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7981,6 +7981,11 @@ trim-newlines@^3.0.0: resolved "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.1.tgz" integrity sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw== +ts-jest-mock-import-meta@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/ts-jest-mock-import-meta/-/ts-jest-mock-import-meta-1.2.0.tgz#6fb12a1fa47f70f9335a288e888cbacf592c4dc1" + integrity sha512-r2+TH6d8LHBXqLTXjJh1KfTZoMvGV0hdn9gwickNVcwS2Co2/mewGjj0XDVEPLg5MVfZVHUFQ9O09anURA3KCw== + ts-jest@^29.2.5: version "29.2.5" resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.2.5.tgz#591a3c108e1f5ebd013d3152142cb5472b399d63"