10000 Support .cts and .mts · avajs/typescript@4ec638c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ec638c

Browse files
committed
Support .cts and .mts
1 parent 34da7bf commit 4ec638c

21 files changed

+132
-58
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ You can enable compilation via the `compile` property. If `false`, AVA will assu
4747

4848
Output files are expected to have the `.js` extension.
4949

50-
AVA searches your entire project for `*.js`, `*.cjs`, `*.mjs` and `*.ts` files (or other extensions you've configured). It will ignore such files found in the `rewritePaths` targets (e.g. `build/`). If you use more specific paths, for instance `build/main/`, you may need to change AVA's `files` configuration to ignore other directories.
50+
AVA searches your entire project for `*.js`, `*.cjs`, `*.mjs`, `*.ts`, `*.cts` and `*.mts` files (or other extensions you've configured). It will ignore such files found in the `rewritePaths` targets (e.g. `build/`). If you use more specific paths, for instance `build/main/`, you may need to change AVA's `files` configuration to ignore other directories.
5151

5252
## ES Modules
5353

index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default function typescriptProvider({negotiateProtocol}) {
8383
validate(config, configProperties);
8484

8585
const {
86-
extensions = ['ts'],
86+
extensions = ['ts', 'cts', 'mts'],
8787
rewritePaths: relativeRewritePaths,
8888
compile,
8989
} = config;
@@ -118,7 +118,7 @@ export default function typescriptProvider({negotiateProtocol}) {
118118
return rewritePaths.some(([from]) => filePath.startsWith(from));
119119
},
120120

121-
resolveTestFile(testfile) {
121+
resolveTestFile(testfile) { // Used under AVA 3.2 protocol by legacy watcher implementation.
122122
if (!testFileExtension.test(testfile)) {
123123
return testfile;
124124
}
@@ -129,8 +129,14 @@ export default function typescriptProvider({negotiateProtocol}) {
129129
}
130130

131131
const [from, to] = rewrite;
132-
// TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html
133-
return `${to}${testfile.slice(from.length)}`.replace(testFileExtension, '.js');
132+
let newExtension = '.js';
133+
if (testfile.endsWith('.cts')) {
134+
newExtension = '.cjs';
135+
} else if (testfile.endsWith('.mts')) {
136+
newExtension = '.mjs';
137+
}
138+
139+
return `${to}${testfile.slice(from.length)}`.replace(testFileExtension, newExtension);
134140
},
135141

136142
updateGlobs({filePatterns, ignoredByWatcherPatterns}) {
@@ -142,15 +148,19 @@ export default function typescriptProvider({negotiateProtocol}) {
142148
],
143149
ignoredByWatcherPatterns: [
144150
...ignoredByWatcherPatterns,
145-
...Object.values(relativeRewritePaths).map(to => `${to}**/*.js.map`),
151+
...Object.values(relativeRewritePaths).flatMap(to => [
152+
`${to}**/*.js.map`,
153+
`${to}**/*.cjs.map`,
154+
`${to}**/*.mjs.map`,
155+
]),
146156
],
147157
};
148158
},
149159
};
150160
},
151161

152162
worker({extensionsToLoadAsModules, state: {extensions, rewritePaths}}) {
153-
const useImport = extensionsToLoadAsModules.includes('js');
163+
const importJs = extensionsToLoadAsModules.includes('js');
154164
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);
155165

156166
return {
@@ -160,8 +170,18 @@ export default function typescriptProvider({negotiateProtocol}) {
160170

161171
async load(ref, {requireFn}) {
162172
const [from, to] = rewritePaths.find(([from]) => ref.startsWith(from));
163-
// TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html
164-
const rewritten = `${to}${ref.slice(from.length)}`.replace(testFileExtension, '.js');
173+
let rewritten = `${to}${ref.slice(from.length)}`;
174+
let useImport = true;
175+
if (ref.endsWith('.cts')) {
176+
rewritten = rewritten.replace(/\.cts$/, '.cjs');
177+
useImport = false;
178+
} else if (ref.endsWith('.mts')) {
179+
rewritten = rewritten.replace(/\.mts$/, '.mjs');
180+
} else {
181+
rewritten = rewritten.replace(testFileExtension, '.js');
182+
useImport = importJs;
183+
}
184+
165185
return useImport ? import(pathToFileURL(rewritten)) : requireFn(rewritten); // eslint-disable-line node/no-unsupported-features/es-syntax
166186
},
167187
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
},
5353
"xo": {
5454
"ignores": [
55-
"test/broken-fixtures"
55+
"test/broken-fixtures",
56+
"test/fixtures/**/compiled/**"
5657
]
5758
}
5859
}

test/esm.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

test/fixtures/esm/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/esm/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/load/compiled/index.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
console.log('logged in fixtures/load/index.cts');

test/fixtures/load/compiled/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('logged in fixtures/load/index.ts');
2+
export {};

test/fixtures/load/compiled/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('logged in fixtures/load/index.mts');
2+
export {};

test/fixtures/load/index.cts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('logged in fixtures/load/index.cts');

test/fixtures/load/index.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('logged in fixtures/load/index.mts');

test/fixtures/load/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('logged in fixtures/load/index.ts');

test/fixtures/esm/tsconfig.json renamed to test/fixtures/load/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"module": "Node16",
34
"outDir": "compiled"
45
},
56
"include": [

test/load.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
3+
import test from 'ava';
4+
import execa from 'execa';
5+
import createProviderMacro from './_with-provider.js';
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
const withProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'fixtures'));
9+
10+
const setup = async provider => ({
11+
state: await provider.main({
12+
config: {
13+
rewritePaths: {
14+
'load/': 'load/compiled/',
15+
},
16+
compile: false,
17+
},
18+
}).compile(),
19+
});
20+
21+
test('worker(): load .cts', withProvider, async (t, provider) => {
22+
const {state} = await setup(provider);
23+
const {stdout, stderr} = await execa.node(
24+
path.join(__dirname, 'fixtures/install-and-load'),
25+
[JSON.stringify({state}), path.join(__dirname, 'fixtures/load', 'index.cts')],
26+
{cwd: path.join(__dirname, 'fixtures')},
27+
);
28+
if (stderr.length > 0) {
29+
t.log(stderr);
30+
}
31+
32+
t.snapshot(stdout);
33+
});
34+
35+
test('worker(): load .mts', withProvider, async (t, provider) => {
36+
const {state} = await setup(provider);
37+
const {stdout, stderr} = await execa.node(
38+
path.join(__dirname, 'fixtures/install-and-load'),
39+
[JSON.stringify({state}), path.join(__dirname, 'fixtures/load', 'index.mts')],
40+
{cwd: path.join(__dirname, 'fixtures')},
41+
);
42+
if (stderr.length > 0) {
43+
t.log(stderr);
44+
}
45+
46+
t.snapshot(stdout);
47+
});
48+
49+
test('worker(): load .ts', withProvider, async (t, provider) => {
50+
const {state} = await setup(provider);
51+
const {stdout, stderr} = await execa.node(
52+
path.join(__dirname, 'fixtures/install-and-load'),
53+
[JSON.stringify({extensionsToLoadAsModules: ['js'], state}), path.join(__dirname, 'fixtures/load', 'index.ts')],
54+
{cwd: path.join(__dirname, 'fixtures')},
55+
);
56+
if (stderr.length > 0) {
57+
t.log(stderr);
58+
}
59+
60+
t.snapshot(stdout);
61+
});

test/protocol-ava-3.2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ test('main() config validation: rewrite paths must end in a /', withProvider, (t
5353
validateConfig(t, provider, {rewritePaths: {'src/': 'build', compile: false}});
5454
});
5555

56-
test('main() extensions: defaults to [\'ts\']', withProvider, (t, provider) => {
57-
t.deepEqual(provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}).extensions, ['ts']);
56+
test('main() extensions: defaults to [\'ts\', \'cts\', \'mts\']', withProvider, (t, provider) => {
57+
t.deepEqual(provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}).extensions, ['ts', 'cts', 'mts']);
5858
});
5959

6060
test('main() extensions: returns configured extensions', withProvider, (t, provider) => {
@@ -76,6 +76,8 @@ test('main() ignoreChange()', withProvider, (t, provider) => {
7676
test('main() resolveTestfile()', withProvider, (t, provider) => {
7777
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}});
7878
t.is(main.resolveTestFile(path.join(__dirname, 'src/foo.ts')), path.join(__dirname, 'build/foo.js'));
79+
t.is(main.resolveTestFile(path.join(__dirname, 'src/foo.cts')), path.join(__dirname, 'build/foo.cjs'));
80+
t.is(main.resolveTestFile(path.join(__dirname, 'src/foo.mts')), path.join(__dirname, 'build/foo.mjs'));
7981
t.is(main.resolveTestFile(path.join(__dirname, 'build/foo.js')), path.join(__dirname, 'build/foo.js'));
8082
t.is(main.resolveTestFile(path.join(__dirname, 'foo/bar.ts')), path.join(__dirname, 'foo/bar.ts'));
8183
});

test/snapshots/esm.js.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/snapshots/esm.js.snap

-169 Bytes
Binary file not shown.

test/snapshots/load.js.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Snapshot report for `test/load.js`
2+
3+
The actual snapshot is saved in `load.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## worker(): load .cts
8+
9+
> Snapshot 1
10+
11+
'logged in fixtures/load/index.cts'
12+
13+
## worker(): load .mts
14+
15+
> Snapshot 1
16+
17+
'logged in fixtures/load/index.mts'
18+
19+
## worker(): load .ts
20+
21+
> Snapshot 1
22+
23+
'logged in fixtures/load/index.ts'

test/snapshots/load.js.snap

189 Bytes
Binary file not shown.

test/snapshots/protocol-ava-3.2.js.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,7 @@ Generated by [AVA](https://avajs.dev).
111111
ignoredByWatcherPatterns: [
112112
'assets/**',
113113
'build/**/*.js.map',
114+
'build/**/*.cjs.map',
115+
'build/**/*.mjs.map',
114116
],
115117
}
26 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0