Skip to content

Commit 9bb0c05

Browse files
authored
feat(compiler): support ESM for isolatedModules: false (#2269)
Related to #1709
1 parent a2ab478 commit 9bb0c05

File tree

8 files changed

+153
-241
lines changed

8 files changed

+153
-241
lines changed

src/__helpers__/path.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export function tempDir(ns: string): string {
1111

1212
return dir
1313
}
14+
15+
export const mockFolder = join(process.cwd(), 'src', '__mocks__')

src/__helpers__/processed-source.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { isAbsolute, relative } from 'path'
22

33
import type { RawSourceMap } from 'source-map'
44

5+
import { SOURCE_MAPPING_PREFIX } from '../compiler/compiler-utils'
6+
57
import { ROOT } from './path'
68
import { ParsedSourceWithMaps, parseSource, relativisePaths, rewriteSourceMaps } from './source-maps'
79

@@ -19,7 +21,7 @@ export default class ProcessedSource {
1921
return parseSource(this.output)
2022
}
2123
get outputCodeWithoutMaps(): string {
22-
return this.parsedSource.source
24+
return this.output.substring(0, this.output.indexOf(SOURCE_MAPPING_PREFIX))
2325
}
2426
get outputSourceMaps(): RawSourceMap | undefined {
2527
return this.parsedSource.sourceMaps
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`TsCompiler isolatedModule false allowJs option should compile js file for allowJs true with outDir 1`] = `
4-
===[ FILE: test-allow-js.js ]===================================================
5-
"use strict";
6-
Object.defineProperty(exports, "__esModule", { value: true });
7-
exports.default = 42;
8-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
9-
===[ INLINE SOURCE MAPS ]=======================================================
10-
file: test-allow-js.js
11-
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
12-
names: []
13-
sources:
14-
- test-allow-js.js
15-
sourcesContent:
16-
- export default 42
17-
version: 3
18-
================================================================================
4+
"\\"use strict\\";
5+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
6+
exports.default = 42;
7+
//# "
198
`;
209

2110
exports[`TsCompiler isolatedModule false allowJs option should compile js file for allowJs true without outDir 1`] = `
22-
===[ FILE: test-allow-js.js ]===================================================
23-
"use strict";
24-
Object.defineProperty(exports, "__esModule", { value: true });
25-
exports.default = 42;
26-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1hbGxvdy1qcy5qcyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWFsbG93LWpzLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
27-
===[ INLINE SOURCE MAPS ]=======================================================
28-
file: test-allow-js.js
29-
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
30-
names: []
31-
sources:
32-
- test-allow-js.js
33-
sourcesContent:
34-
- export default 42
35-
version: 3
36-
================================================================================
11+
"\\"use strict\\";
12+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
13+
exports.default = 42;
14+
//# "
3715
`;
3816

3917
exports[`TsCompiler isolatedModule false diagnostics should throw error when cannot compile 1`] = `
@@ -55,131 +33,54 @@ Array [
5533
`;
5634

5735
exports[`TsCompiler isolatedModule false jsx option should compile tsx file for jsx preserve 1`] = `
58-
===[ FILE: test-jsx.tsx ]=======================================================
59-
"use strict";
60-
const App = () => {
61-
return <>Test</>;
62-
};
63-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gudHN4IiwibWFwcGluZ3MiOiI7QUFDUSxNQUFNLEdBQUcsR0FBRyxHQUFHLEVBQUU7SUFDZixPQUFPLEVBQUUsSUFBSSxHQUFHLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtanN4LnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
64-
===[ INLINE SOURCE MAPS ]=======================================================
65-
file: test-jsx.tsx
66-
mappings: ';AACQ,MAAM,GAAG,GAAG,GAAG,EAAE;IACf,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
67-
names: []
68-
sources:
69-
- test-jsx.tsx
70-
sourcesContent:
71-
- |2-
72-
73-
const App = () => {
74-
return <>Test</>
75-
}
76-
77-
version: 3
78-
================================================================================
36+
"\\"use strict\\";
37+
const App = () => {
38+
return <>Test</>;
39+
};
40+
//# "
7941
`;
8042

8143
exports[`TsCompiler isolatedModule false jsx option should compile tsx file for other jsx options 1`] = `
82-
===[ FILE: test-jsx.tsx ]=======================================================
83-
"use strict";
84-
const App = () => {
85-
return React.createElement(React.Fragment, null, "Test");
86-
};
87-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gudHN4IiwibWFwcGluZ3MiOiI7QUFDUSxNQUFNLEdBQUcsR0FBRyxHQUFHLEVBQUU7SUFDZixPQUFPLGlEQUFTLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtanN4LnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
88-
===[ INLINE SOURCE MAPS ]=======================================================
89-
file: test-jsx.tsx
90-
mappings: ';AACQ,MAAM,GAAG,GAAG,GAAG,EAAE;IACf,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
91-
names: []
92-
sources:
93-
- test-jsx.tsx
94-
sourcesContent:
95-
- |2-
96-
97-
const App = () => {
98-
return <>Test</>
99-
}
100-
101-
version: 3
102-
================================================================================
44+
"\\"use strict\\";
45+
const App = () => {
46+
return React.createElement(React.Fragment, null, \\"Test\\");
47+
};
48+
//# "
49+
`;
50+
51+
exports[`TsCompiler isolatedModule false should compile codes with useESM true 1`] = `
52+
"export const thing = { a: 1, b: 2 };
53+
//# "
10354
`;
10455

10556
exports[`TsCompiler isolatedModule true diagnostics should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
10657

10758
exports[`TsCompiler isolatedModule true diagnostics should report diagnostics related to codes with pathRegex config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
10859

10960
exports[`TsCompiler isolatedModule true jsx option should compile tsx file for jsx preserve 1`] = `
110-
===[ FILE: foo.tsx ]============================================================
111-
"use strict";
112-
const App = () => {
113-
return <>Test</>;
114-
};
115-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsTUFBTSxHQUFHLEdBQUcsR0FBRyxFQUFFO0lBQ2YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJmb28udHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgICB9XG4gICAgICAiXSwidmVyc2lvbiI6M30=
116-
===[ INLINE SOURCE MAPS ]=======================================================
117-
file: foo.tsx
118-
mappings: ';AACQ,MAAM,GAAG,GAAG,GAAG,EAAE;IACf,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
119-
names: []
120-
sources:
121-
- foo.tsx
122-
sourcesContent:
123-
- |2-
124-
125-
const App = () => {
126-
return <>Test</>
127-
}
128-
129-
version: 3
130-
================================================================================
61+
"\\"use strict\\";
62+
const App = () => {
63+
return <>Test</>;
64+
};
65+
//# "
13166
`;
13267

13368
exports[`TsCompiler isolatedModule true jsx option should compile tsx file for other jsx options 1`] = `
134-
===[ FILE: foo.tsx ]============================================================
135-
"use strict";
136-
const App = () => {
137-
return React.createElement(React.Fragment, null, "Test");
138-
};
139-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsTUFBTSxHQUFHLEdBQUcsR0FBRyxFQUFFO0lBQ2YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJmb28udHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgICB9XG4gICAgICAiXSwidmVyc2lvbiI6M30=
140-
===[ INLINE SOURCE MAPS ]=======================================================
141-
file: foo.tsx
142-
mappings: ';AACQ,MAAM,GAAG,GAAG,GAAG,EAAE;IACf,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
143-
names: []
144-
sources:
145-
- foo.tsx
146-
sourcesContent:
147-
- |2-
148-
149-
const App = () => {
150-
return <>Test</>
151-
}
152-
153-
version: 3
154-
================================================================================
69+
"\\"use strict\\";
70+
const App = () => {
71+
return React.createElement(React.Fragment, null, \\"Test\\");
72+
};
73+
//# "
15574
`;
15675

15776
exports[`TsCompiler isolatedModule true should compile js file for allowJs true 1`] = `
158-
===[ FILE: foo.js ]=============================================================
159-
"use strict";
160-
Object.defineProperty(exports, "__esModule", { value: true });
161-
exports.default = 42;
162-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbImZvby5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ==
163-
===[ INLINE SOURCE MAPS ]=======================================================
164-
file: foo.js
165-
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
166-
names: []
167-
sources:
168-
- foo.js
169-
sourcesContent:
170-
- export default 42
171-
version: 3
172-
================================================================================
77+
"\\"use strict\\";
78+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
79+
exports.default = 42;
80+
//# "
17381
`;
17482

175-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 1`] = `99`;
176-
177-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 2`] = `99`;
178-
179-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 3`] = `99`;
180-
181-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 4`] = `1`;
182-
183-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 5`] = `1`;
184-
185-
exports[`TsCompiler isolatedModule true support ESM should transpile codes to correct syntax with supportsStaticESM and useESM options 6`] = `1`;
83+
exports[`TsCompiler isolatedModule true should transpile code with useESM true 1`] = `
84+
"export const thing = { a: 1, b: 2 };
85+
//# "
86+
`;

0 commit comments

Comments
 (0)