Skip to content

Commit e048960

Browse files
committed
Convert this library to ES Modules
1 parent 03b91e0 commit e048960

24 files changed

+342
-150
lines changed

.eslintrc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
]
1717
}
1818
],
19-
"import/extensions": ["error", "never", {
20-
"json": "always",
21-
}],
19+
// Disabled due to https://github.com/import-js/eslint-plugin-import/issues/2104
20+
"import/extensions": [0, "never"],
2221
// This rule conflicts with our convention.
2322
"jest/valid-describe": "off",
2423
// This is not needed for this project.
@@ -32,20 +31,13 @@
3231
"@typescript-eslint/explicit-function-return-type": "off",
3332
"@typescript-eslint/no-explicit-any": "error",
3433
"@typescript-eslint/explicit-member-accessibility": "off",
35-
"import/no-unresolved": ["error", {
36-
ignore: ['estree'],
37-
}],
34+
// This is sad but we have to disable this rule to allow TS to generate
35+
// correct a ESM package.
36+
"import/no-unresolved": "off",
3837
// This rule is annoying and does not add any value.
3938
"@typescript-eslint/camelcase": "off",
4039
"@typescript-eslint/ban-ts-comment": ["error", {
4140
"ts-expect-error": "allow-with-description",
42-
}],
43-
},
44-
"settings": {
45-
"import/resolver": {
46-
"node": {
47-
"extensions": [".js", ".ts"]
48-
}
49-
}
41+
}]
5042
}
5143
}

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
module.exports = {
1+
export default {
22
preset: 'ts-jest',
3+
moduleNameMapper: {
4+
// Replace imports ending with `.js` with imports without extensions.
5+
'^(\\.{1,2}/.*)\\.js$': '$1',
6+
},
37
testEnvironment: 'node',
48
testPathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/node_modules/'],
59
};

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
"name": "addons-scanner-utils",
33
"version": "9.0.0",
44
"description": "Various addons related helpers to build CLIs.",
5-
"main": "dist/index.js",
6-
"types": "dist/index.d.ts",
5+
"type": "module",
6+
"main": "./dist/index.js",
7+
"types": "./dist/index.d.ts",
8+
"exports": {
9+
".": "./dist/index.js",
10+
"./io": "./dist/io/index.js",
11+
"./test-helpers": "./dist/test-helpers.js"
12+
},
713
"files": [
8-
"dist/**/*"
14+
"dist/**"
915
],
1016
"author": "Mozilla Add-ons Team",
1117
"license": "MPL-2.0",
@@ -42,12 +48,12 @@
4248
"@types/express": "4.17.17",
4349
"@types/jest": "^29.0.0",
4450
"@types/node": "^18.0.0",
45-
"@types/node-fetch": "^2.6.2",
51+
"@types/node-fetch": "^2.6.3",
4652
"@types/safe-compare": "^1.1.0",
4753
"@types/sinon": "^10.0.0",
4854
"@types/supertest": "^2.0.8",
49-
"@typescript-eslint/eslint-plugin": "^5.0.0",
50-
"@typescript-eslint/parser": "^5.0.0",
55+
"@typescript-eslint/eslint-plugin": "^5.58.0",
56+
"@typescript-eslint/parser": "^5.58.0",
5157
"body-parser": "1.20.2",
5258
"eslint": "^8.1.0",
5359
"eslint-config-amo": "^5.0.0",

src/@types/safe-compare/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The `@types/safe-compare` package does not declare a module and that breaks
2+
// the type checker...
3+
declare module 'safe-compare' {
4+
function safeCompare(a: string, b: string): boolean;
5+
6+
export default safeCompare;
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ECMA_VERSION } from './const';
1+
import { ECMA_VERSION } from './constants.js';
22

3-
describe(__filename, () => {
3+
describe('constants', () => {
44
it('exports an ECMA_VERSION constant', () => {
55
expect(ECMA_VERSION).toEqual(13);
66
});
File renamed without changes.

src/functions.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import {
1111
RequestWithFiles,
1212
createApiError,
1313
createExpressApp,
14-
} from './functions';
14+
} from './functions.js';
1515

1616
jest.mock('node-fetch');
1717

18-
describe(__filename, () => {
18+
describe('functions', () => {
1919
describe('createApiError', () => {
2020
it('returns an API error with a message', () => {
2121
const message = 'oops, error';

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from './const';
2-
export * from './functions';
3-
export * from './io';
4-
export * from './stdio';
5-
export * from './errors';
1+
export * from './constants.js';
2+
export * from './functions.js';
3+
export * from './io/index.js';
4+
export * from './stdio.js';
5+
export * from './errors.js';

src/io/base.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { IOBase } from './base';
2-
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH } from './const';
3-
import { createFakeStderr } from '../test-helpers';
1+
import { IOBase } from './base.js';
2+
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH } from './constants.js';
3+
import { createFakeStderr } from '../test-helpers.js';
44

5-
describe(__filename, () => {
5+
describe('io/base', () => {
66
const createIOBase = ({
77
filePath = 'foo/bar/',
88
stderr = createFakeStderr(),

src/io/base.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Readable } from 'stream';
22

33
import { oneLine } from 'common-tags';
44

5-
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH, MAX_FILE_SIZE_MB } from './const';
6-
import { Stderr } from '../stdio';
5+
import {
6+
FLAGGED_FILE_MAGIC_NUMBERS_LENGTH,
7+
MAX_FILE_SIZE_MB,
8+
} from './constants.js';
9+
import { Stderr } from '../stdio.js';
710

811
type ScanFileFunction = (_path: string, isDirectory: boolean) => boolean;
912

0 commit comments

Comments
 (0)