Skip to content

Commit ec5609b

Browse files
authored
chore: convert to esm (#2261)
1 parent db88a15 commit ec5609b

8 files changed

Lines changed: 24 additions & 18 deletions

File tree

.eslintrc.js renamed to .eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { readGitignoreFiles } = require('eslint-gitignore');
1111
module.exports = defineConfig({
1212
ignorePatterns: [
1313
...readGitignoreFiles(),
14-
'.eslintrc.js', // Skip self linting
14+
'.eslintrc.cjs', // Skip self linting
1515
],
1616
root: true,
1717
env: {

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ jobs:
173173
env:
174174
CYPRESS_INSTALL_BINARY: 0
175175

176+
- name: Build types
177+
run: pnpm run build:types
178+
176179
- name: Check scripts
177180
run: pnpm run ts-check
178181

.github/workflows/commentCodeGeneration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import type { context as ctx, GitHub } from '@actions/github/lib/utils';
1010
* @param context An object containing the context of the workflow run
1111
* @param isSuccess A boolean indicating whether the workflow was successful
1212
*/
13-
module.exports = async (
13+
export async function script(
1414
github: InstanceType<typeof GitHub>,
1515
context: typeof ctx,
1616
isSuccess: boolean
17-
) => {
17+
): Promise<void> {
1818
const { data: comments } = await github.rest.issues.listComments({
1919
owner: context.repo.owner,
2020
repo: context.repo.repo,
@@ -45,4 +45,4 @@ module.exports = async (
4545
body,
4646
});
4747
}
48-
};
48+
}

.github/workflows/pr.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ jobs:
5050
continue-on-error: true
5151

5252
- name: Transpile ts
53-
run: pnpm exec tsc .github/workflows/commentCodeGeneration.ts --outDir .github/workflows
53+
run: pnpm tsup-node .github/workflows/commentCodeGeneration.ts --format cjs --clean false --out-dir .github/workflows
5454

5555
- name: Comment
5656
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
5757
with:
5858
script: |
59-
const script = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.js')
59+
const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs')
6060
await script(github, context, ${{ steps.generate.outcome == 'success' && steps.diff.outcome == 'success' }})
6161
6262
- name: Status

.prettierrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @ts-check
22

33
/**
4-
* @type {import('prettier').Options}
4+
* @type {import('prettier').Config}
55
*/
6-
module.exports = {
6+
export default {
77
plugins: ['prettier-plugin-organize-imports'],
88
singleQuote: true,
99
trailingComma: 'es5',
@@ -20,6 +20,7 @@ module.exports = {
2020
{
2121
files: '*.md',
2222
options: {
23+
// @ts-expect-error: known property
2324
organizeImportsSkipDestructiveCodeActions: true,
2425
},
2526
},

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
],
2929
"bugs": "https://github.com/faker-js/faker/issues",
3030
"license": "MIT",
31-
"main": "dist/index.js",
32-
"module": "dist/index.mjs",
31+
"type": "module",
32+
"main": "dist/index.cjs",
33+
"module": "dist/index.js",
3334
"types": "index.d.ts",
3435
"typesVersions": {
3536
">=4.0": {
@@ -41,13 +42,15 @@
4142
"exports": {
4243
".": {
4344
"types": "./dist/types/index.d.ts",
44-
"require": "./dist/index.js",
45-
"import": "./dist/index.mjs"
45+
"import": "./dist/index.js",
46+
"require": "./dist/index.cjs",
47+
"default": "./dist/index.js"
4648
},
4749
"./locale/*": {
4850
"types": "./dist/types/locale/*.d.ts",
49-
"require": "./dist/locale/*.js",
50-
"import": "./dist/locale/*.mjs"
51+
"import": "./dist/locale/*.js",
52+
"require": "./dist/locale/*.cjs",
53+
"default": "./dist/locale/*.js"
5154
},
5255
"./package.json": "./package.json"
5356
},

test/faker.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ describe('faker', () => {
1313
);
1414
});
1515

16-
it('should not log anything on startup', () => {
16+
it('should not log anything on startup', async () => {
1717
const spies: MockInstance[] = keys(console)
1818
.filter((key) => typeof console[key] === 'function')
1919
.map((methodName) =>
2020
vi.spyOn(console, methodName as keyof typeof console)
2121
);
2222

23-
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module -- Using import() requires types being build but the CI / TS-Check runs without them.
24-
require('..').faker;
23+
(await import('..')).default;
2524

2625
new Faker({ locale: { metadata: { title: '' } } });
2726

test/locale-imports.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { keys } from '../src/internal/keys';
66
describe.each(keys(allLocales))('locale imports', (locale) => {
77
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
88
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
9-
const { faker } = require(`../dist/locale/${locale}`) as {
9+
const { faker } = require(`../dist/locale/${locale}.cjs`) as {
1010
faker: Faker;
1111
};
1212

0 commit comments

Comments
 (0)