Skip to content

Commit d0d7874

Browse files
committed
refactor!: drop autoAddExts option
It will always be `true`, and cannot be disabled.
1 parent 1375605 commit d0d7874

File tree

13 files changed

+90
-135
lines changed

13 files changed

+90
-135
lines changed

README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ export interface Options {
123123
ignoreErrors?: boolean
124124
/** An extra directory layer for output files. */
125125
extraOutdir?: string
126-
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
127-
autoAddExts?: boolean
128126

129127
rewriteImports?: (
130128
id: string,
@@ -157,22 +155,6 @@ export default {
157155
}
158156
```
159157

160-
### `autoAddExts`
161-
162-
Automatically add `.js` extension to resolve in Node 16+ ESM mode. (esbuild support is not available)
163-
164-
```ts
165-
// index.d.ts
166-
import {} from './foo'
167-
```
168-
169-
With `autoAddExts`, it will be transformed to:
170-
171-
```ts
172-
// index.d.ts
173-
import {} from './foo.js'
174-
```
175-
176158
### `patchCjsDefaultExport`
177159

178160
Patch `export default` in `.d.cts` to `export =`

src/core/ast.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
2-
import { debug, guessSuffix, stripExt } from './utils'
2+
import { debug, stripExt } from './utils'
3+
import type { OptionsResolved } from './options'
34
import type * as OxcTypes from '@oxc-project/types'
45
import type MagicString from 'magic-string'
56

@@ -9,7 +10,7 @@ export type OxcImport = (
910
| OxcTypes.ExportNamedDeclaration
1011
) & {
1112
source: OxcTypes.StringLiteral
12-
suffix?: string
13+
shouldAddIndex?: boolean
1314
}
1415

1516
export function filterImports(program: OxcTypes.Program): OxcImport[] {
@@ -24,6 +25,7 @@ export function filterImports(program: OxcTypes.Program): OxcImport[] {
2425

2526
export function rewriteImports(
2627
s: MagicString,
28+
options: OptionsResolved,
2729
imports: OxcImport[],
2830
entryMap: Record<string, string> | undefined,
2931
inputBase: string,
@@ -50,25 +52,25 @@ export function rewriteImports(
5052

5153
for (const i of imports) {
5254
const { source } = i
53-
const srcIdRel = stripExt(source.value)
55+
let srcIdRel = source.value
5456
if (srcIdRel[0] !== '.') continue
5557

58+
if (i.shouldAddIndex) srcIdRel += '/index'
59+
5660
const srcId = path.resolve(srcDir, srcIdRel)
57-
const importAlias = entryMap?.[srcId]
61+
const importAlias = entryMap?.[stripExt(srcId)]
5862

5963
let id: string
6064
if (importAlias) {
61-
const resolved = stripExt(
62-
entryFileNames.replaceAll('[name]', importAlias),
65+
const resolved = entryFileNames.replaceAll(
66+
'[name]',
67+
stripExt(importAlias),
6368
)
6469
id = pathRelative(srcDirRel, resolved)
6570
} else {
66-
id = stripExt(entryFileNames.replaceAll('[name]', srcIdRel))
71+
id = entryFileNames.replaceAll('[name]', stripExt(srcIdRel))
6772
}
6873

69-
const suffix = i.suffix || guessSuffix(source.value, source.value)
70-
if (suffix) id += suffix
71-
7274
let final = path.normalize(path.join(offset, id))
7375
if (final !== path.normalize(source.value)) {
7476
debug('Patch import in', srcRel, ':', srcIdRel, '->', final)

src/core/options.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export type Options = {
1919
ignoreErrors?: boolean
2020
/** An extra directory layer for output files. */
2121
extraOutdir?: string
22-
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
23-
autoAddExts?: boolean
2422
/** Patch `export default` in `.d.cts` to `export = ` */
2523
patchCjsDefaultExport?: boolean
2624
/** Base directory for input files. */
@@ -64,7 +62,6 @@ export function resolveOptions(options: Options): OptionsResolved {
6462
transformer: options.transformer || 'typescript',
6563
ignoreErrors: options.ignoreErrors || false,
6664
extraOutdir: options.extraOutdir,
67-
autoAddExts: options.autoAddExts || false,
6865
patchCjsDefaultExport: options.patchCjsDefaultExport || false,
6966
rewriteImports: options.rewriteImports,
7067
inputBase: options.inputBase,

src/core/utils.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ function endsWithIndex(s: string) {
6060
return /(?:^|[/\\])index\..+$/.test(s)
6161
}
6262

63-
export function guessSuffix(id: string, resolved: string): string {
64-
let suffix = ''
65-
if (!endsWithIndex(id) && endsWithIndex(resolved)) {
66-
suffix += '/index'
67-
}
68-
const ext = path
69-
.extname(resolved)
70-
.slice(1)
71-
.replace(/^([cm]?)ts/, (_, $1) => `${$1}js`)
72-
if (ext) suffix += `.${ext}`
73-
return suffix
63+
export function shouldAddIndex(id: string, resolved: string): boolean {
64+
return !endsWithIndex(id) && endsWithIndex(resolved)
7465
}

src/index.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import {
2626
} from './core/transformer'
2727
import {
2828
debug,
29-
guessSuffix,
3029
lowestCommonAncestor,
3130
resolveEntry,
31+
shouldAddIndex,
3232
stripExt,
3333
} from './core/utils'
3434
import type {
@@ -147,29 +147,26 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
147147
const imports = filterImports(program)
148148

149149
const s = new MagicString(dts)
150-
if (options.autoAddExts || options.rewriteImports) {
151-
for (const i of imports) {
152-
const { source } = i
153-
let { value } = source
154-
155-
if (options.rewriteImports) {
156-
const result = options.rewriteImports(value, id)
157-
if (typeof result === 'string') {
158-
source.value = value = result
159-
s.overwrite(source.start + 1, source.end - 1, result)
160-
}
161-
}
162150

163-
if (
164-
options.autoAddExts &&
165-
(path.isAbsolute(value) || value[0] === '.')
166-
) {
167-
const resolved = await resolve(context, value, stripExt(id))
168-
if (!resolved || resolved.external) continue
169-
i.suffix = guessSuffix(value, resolved.id)
151+
for (const i of imports) {
152+
const { source } = i
153+
let { value } = source
154+
155+
if (options.rewriteImports) {
156+
const result = options.rewriteImports(value, id)
157+
if (typeof result === 'string') {
158+
source.value = value = result
159+
s.overwrite(source.start + 1, source.end - 1, result)
170160
}
171161
}
162+
163+
if (path.isAbsolute(value) || value[0] === '.') {
164+
const resolved = await resolve(context, value, stripExt(id))
165+
if (!resolved || resolved.external) continue
166+
i.shouldAddIndex = shouldAddIndex(value, resolved.id)
167+
}
172168
}
169+
173170
addOutput(id, { s, imports, map })
174171

175172
const typeImports = program.body.filter((node): node is OxcImport => {
@@ -230,6 +227,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
230227
)) {
231228
const emitName = rewriteImports(
232229
s,
230+
options,
233231
imports,
234232
entryMap,
235233
inputBase,
@@ -299,6 +297,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
299297
)) {
300298
const emitName = rewriteImports(
301299
s,
300+
options,
302301
imports,
303302
entryMap,
304303
inputBase,
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`rollup > auto add exts 1`] = `
3+
exports[`rollup > custom rewriter 1`] = `
4+
"// index.d.ts
5+
export type * from './test.js';
6+
7+
// index.js
8+
9+
10+
// test.d.ts
11+
export type Str = string;
12+
"
13+
`;
14+
15+
exports[`rollup > extraOutdir 1`] = `
416
"// main.js
517
function Component() {
618
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
@@ -14,45 +26,66 @@ let num = 1;
1426
1527
export { c, hello, num };
1628
17-
// temp/component.d.ts
29+
// types/component.d.ts
1830
export declare function Component(): React.JSX.Element;
1931
20-
// temp/main.d.ts
21-
import { type Num } from '../temp/types.js';
32+
// types/main.d.ts
33+
import { type Num } from '../types/types.js';
2234
export type Str = string;
2335
export declare function hello(s: Str): Str;
2436
export declare let c: React.JSX.Element;
2537
export declare let num: Num;
2638
27-
// temp/types.d.ts
28-
import type { Num2 } from '../temp/types2.js';
39+
// types/types.d.ts
40+
import type { Num2 } from '../types/types2.js';
2941
export type Num = Num2;
3042
31-
// temp/types2.d.ts
43+
// types/types2.d.ts
3244
export type Num2 = number;
3345
"
3446
`;
3547

36-
exports[`rollup > custom rewriter 1`] = `
37-
"// index.d.ts
38-
export type * from './test.js';
48+
exports[`rollup > generate basic 1`] = `
49+
"// component.d.ts
50+
export declare function Component(): React.JSX.Element;
3951
40-
// index.js
52+
// main.d.ts
53+
import { type Num } from './types.js';
54+
export type Str = string;
55+
export declare function hello(s: Str): Str;
56+
export declare let c: React.JSX.Element;
57+
export declare let num: Num;
4158
59+
// main.js
60+
function Component() {
61+
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
62+
}
4263
43-
// test.d.ts
44-
export type Str = string;
64+
function hello(s) {
65+
return "hello" + s;
66+
}
67+
let c = Component;
68+
let num = 1;
69+
70+
export { c, hello, num };
71+
72+
// types.d.ts
73+
import type { Num2 } from './types2.js';
74+
export type Num = Num2;
75+
76+
// types2.d.ts
77+
export type Num2 = number;
4578
"
4679
`;
4780

48-
exports[`rollup > don't add exts, but keep 1`] = `
81+
exports[`rollup > keep ext 1`] = `
4982
"// main.d.ts
5083
export type {} from './types.js';
5184
export type {} from './types.js';
5285
export type {} from './tsx.js';
53-
export type {} from './tsx.jsx';
5486
export type {} from './tsx.js';
55-
export type {} from './tsx.jsx';
87+
export type {} from './tsx.js';
88+
export type {} from './tsx.js';
5689
5790
// main.js
5891
@@ -64,36 +97,3 @@ export type A = string;
6497
export type Str = string;
6598
"
6699
`;
67-
68-
exports[`rollup > generate basic 1`] = `
69-
"// main.js
70-
function Component() {
71-
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
72-
}
73-
74-
function hello(s) {
75-
return "hello" + s;
76-
}
77-
let c = Component;
78-
let num = 1;
79-
80-
export { c, hello, num };
81-
82-
// temp/component.d.ts
83-
export declare function Component(): React.JSX.Element;
84-
85-
// temp/main.d.ts
86-
import { type Num } from '../temp/types';
87-
export type Str = string;
88-
export declare function hello(s: Str): Str;
89-
export declare let c: React.JSX.Element;
90-
export declare let num: Num;
91-
92-
// temp/types.d.ts
93-
import type { Num2 } from '../temp/types2';
94-
export type Num = Num2;
95-
96-
// temp/types2.d.ts
97-
export type Num2 = number;
98-
"
99-
`;

tests/__snapshots__/rollup/entry-points-22.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export declare const a = "b";
4444
## a/index.d.ts
4545

4646
```ts
47-
export { ComponentA } from "./ComponentA/ComponentA/index.jsx";
47+
export { ComponentA } from "./ComponentA/ComponentA/index.js";
4848
//# sourceMappingURL=index.d.ts.map
4949
```
5050
## a/index.d.ts.map
@@ -90,7 +90,7 @@ export { ComponentB };
9090
## b/index.d.ts
9191

9292
```ts
93-
export { ComponentB } from "./ComponentB/ComponentB/index.jsx";
93+
export { ComponentB } from "./ComponentB/ComponentB/index.js";
9494
//# sourceMappingURL=index.d.ts.map
9595
```
9696
## b/index.d.ts.map
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)