Skip to content

Commit

Permalink
chore: convert to esm
Browse files Browse the repository at this point in the history
fix generate:locales

fix scripts apidoc utils

adjust package exports

fix test scripts apidoc

use cjs bundling
  • Loading branch information
Shinigami92 committed Jul 18, 2023
1 parent 3df98a0 commit 614c0d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
],
"bugs": "https://github.com/faker-js/faker/issues",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"types": "index.d.ts",
"typesVersions": {
">=4.0": {
Expand All @@ -41,13 +42,15 @@
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.mjs"
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.cjs",
"default": "./dist/esm/index.js"
},
"./locale/*": {
"types": "./dist/types/locale/*.d.ts",
"require": "./dist/cjs/locale/*.js",
"import": "./dist/esm/locale/*.mjs"
"import": "./dist/esm/locale/*.js",
"require": "./dist/cjs/locale/*.cjs",
"default": "./dist/esm/locale/*.js"
},
"./package.json": "./package.json"
},
Expand Down
3 changes: 3 additions & 0 deletions scripts/apidoc/utils.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
15 changes: 6 additions & 9 deletions scripts/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { buildSync } from 'esbuild';
import { globSync } from 'glob';
import { allLocales } from '../src';

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
format: 'cjs',
platform: 'node',
target,
outExtension: { '.js': '.cjs' },
});

console.log('Building dist for node type=module (esm)...');
Expand All @@ -36,5 +34,4 @@ buildSync({
splitting: true,
format: 'esm',
target,
outExtension: { '.js': '.mjs' },
});
6 changes: 4 additions & 2 deletions scripts/generateLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.cjs';
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

// Constants

const pathRoot = resolve(__dirname, '..');
Expand Down Expand Up @@ -281,8 +284,7 @@ for (const locale of locales) {
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(`No title property found on ${JSON.stringify(metadata)}`);
Expand Down
3 changes: 3 additions & 0 deletions test/scripts/apidoc/verify-jsdoc-tags.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down

0 comments on commit 614c0d1

Please sign in to comment.