From fbbef7e25e48295adf5b87bd4cccbface100ae31 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Jul 2023 19:56:02 +0200 Subject: [PATCH] chore: convert to esm --- .eslintrc.js => .eslintrc.cjs | 2 +- package.json | 9 ++++++--- scripts/apidoc/utils.ts | 3 +++ scripts/bundle.ts | 14 +++++--------- scripts/generateLocales.ts | 6 ++++-- test/scripts/apidoc/verify-jsdoc-tags.spec.ts | 3 +++ 6 files changed, 22 insertions(+), 15 deletions(-) rename .eslintrc.js => .eslintrc.cjs (98%) diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 98% rename from .eslintrc.js rename to .eslintrc.cjs index 2896766b2f1..191203b8f60 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -5,7 +5,7 @@ const { readGitignoreFiles } = require('eslint-gitignore'); module.exports = defineConfig({ ignorePatterns: [ ...readGitignoreFiles(), - '.eslintrc.js', // Skip self linting + '.eslintrc.cjs', // Skip self linting ], root: true, env: { diff --git a/package.json b/package.json index c54c6b23caf..b343b865256 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,9 @@ ], "bugs": "https://github.com/faker-js/faker/issues", "license": "MIT", + "type": "module", "main": "dist/cjs/index.js", - "module": "dist/esm/index.mjs", + "module": "dist/esm/index.js", "types": "index.d.ts", "typesVersions": { ">=4.0": { @@ -41,13 +42,15 @@ "exports": { ".": { "types": "./dist/types/index.d.ts", + "import": "./dist/esm/index.js", "require": "./dist/cjs/index.js", - "import": "./dist/esm/index.mjs" + "default": "./dist/esm/index.js" }, "./locale/*": { "types": "./dist/types/locale/*.d.ts", + "import": "./dist/esm/locale/*.js", "require": "./dist/cjs/locale/*.js", - "import": "./dist/esm/locale/*.mjs" + "default": "./dist/esm/locale/*.js" }, "./package.json": "./package.json" }, diff --git a/scripts/apidoc/utils.ts b/scripts/apidoc/utils.ts index 4f8d34404e6..2a013ae613c 100644 --- a/scripts/apidoc/utils.ts +++ b/scripts/apidoc/utils.ts @@ -1,7 +1,10 @@ import { createHash } from 'node:crypto'; import { resolve } from 'node:path'; +import { fileURLToPath, URL } from 'node:url'; import type { Method } from '../../docs/.vitepress/components/api-docs/method'; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); + // Types export type Page = { text: string; link: string }; diff --git a/scripts/bundle.ts b/scripts/bundle.ts index 8076f18da7e..6613f22264f 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -1,5 +1,4 @@ import { buildSync } from 'esbuild'; -import { globSync } from 'glob'; import { allLocales } from '../src'; console.log('Building dist for node (cjs)...'); @@ -7,14 +6,12 @@ console.log('Building dist for node (cjs)...'); const target = ['ES2019', 'node14.17']; buildSync({ - entryPoints: globSync('./src/**/*.ts'), - // We can use the following entry points when esbuild supports cjs+splitting - // entryPoints: [ - // './src/index.ts', - // ...Object.keys(locales).map((locale) => `./src/locale/${locale}.ts`), - // ], + entryPoints: [ + './src/index.ts', + ...Object.keys(allLocales).map((locale) => `./src/locale/${locale}.ts`), + ], outdir: './dist/cjs', - bundle: false, // Creates 390MiB bundle ... + bundle: true, // Creates 49MiB bundle ... sourcemap: false, minify: true, // splitting: true, // Doesn't work with cjs @@ -36,5 +33,4 @@ buildSync({ splitting: true, format: 'esm', target, - outExtension: { '.js': '.mjs' }, }); diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index 81cf4331506..8560d68193a 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -20,11 +20,14 @@ import { writeFileSync, } from 'node:fs'; import { resolve } from 'node:path'; +import { fileURLToPath, URL } from 'node:url'; import type { Options } from 'prettier'; import { format } from 'prettier'; import options from '../.prettierrc.js'; import type { LocaleDefinition, MetadataDefinition } from '../src/definitions'; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); + // Constants const pathRoot = resolve(__dirname, '..'); @@ -284,8 +287,7 @@ async function main(): Promise { const pathMetadata = resolve(pathModules, 'metadata.ts'); let localeTitle = 'No title found'; try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const metadata: MetadataDefinition = require(pathMetadata).default; + const metadata: MetadataDefinition = (await import(pathMetadata)).default; const { title } = metadata; if (!title) { throw new Error( diff --git a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts index 557504c8517..36bc08ed361 100644 --- a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts +++ b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts @@ -1,5 +1,6 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; import { resolve } from 'node:path'; +import { fileURLToPath, URL } from 'node:url'; import validator from 'validator'; import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; import { initMarkdownRenderer } from '../../../scripts/apidoc/markdown'; @@ -16,6 +17,8 @@ import { } from '../../../scripts/apidoc/typedoc'; import { loadProjectModules } from './utils'; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); + // This test ensures, that every method // - has working examples // - running these do not log anything, unless the method is deprecated