Skip to content

Commit

Permalink
fix: resolve extraOutdir path
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Dec 10, 2024
1 parent 1983773 commit 5995c5e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,15 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
const { inputBase, entryMap } = resolveEntry(input, options.inputBase)
debug('[rollup] input base:', inputBase)

let { entryFileNames = '[name].js', dir: outDir } = outputOptions
const { entryFileNames = '[name].js', dir: outDir } = outputOptions
if (typeof entryFileNames !== 'string') {
return this.error('entryFileNames must be a string')
}

if (options.extraOutdir) {
entryFileNames = path.join(options.extraOutdir, entryFileNames)
}

for (const [srcFilename, { s, imports, map, ext }] of Object.entries(
outputFiles,
)) {
const emitName = rewriteImports(
let emitName = rewriteImports(
s,
options,
imports,
Expand All @@ -240,6 +236,9 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
source = patchCjsDefaultExport(source)
}

if (options.extraOutdir) {
emitName = path.join(options.extraOutdir || '', emitName)
}
debug('[rollup] emit dts file:', emitName)
if (options.sourceMap && map && outDir) {
source = appendMapUrl(source, emitName)
Expand Down Expand Up @@ -288,14 +287,11 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
extFormatMap.get(output.format || 'esm') || 'js',
)

let entryFileNames = output.entryFilename
if (options.extraOutdir) {
entryFileNames = path.join(options.extraOutdir, entryFileNames)
}
const entryFileNames = output.entryFilename
for (const [srcFilename, { s, imports, map, ext }] of Object.entries(
outputFiles,
)) {
const emitName = rewriteImports(
let emitName = rewriteImports(
s,
options,
imports,
Expand All @@ -310,6 +306,10 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
source = patchCjsDefaultExport(source)
}

if (options.extraOutdir) {
emitName = path.join(options.extraOutdir || '', emitName)
}

debug('[farm] emit dts file:', emitName)
const outDir = output.path
if (options.sourceMap && map && outDir) {
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/rolldown.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export { c, hello, num };
export declare function Component(): React.JSX.Element;
// temp/main.d.ts
import { type Num } from "../temp/types.js";
import { type Num } from "./types.js";
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
// temp/types.d.ts
import type { Num2 } from "../temp/types2.js";
import type { Num2 } from "./types2.js";
export type Num = Num2;
// temp/types2.d.ts
Expand Down
33 changes: 0 additions & 33 deletions tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,6 @@ export type Str = string;
"
`;

exports[`rollup > extraOutdir 1`] = `
"// main.js
function Component() {
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
}
function hello(s) {
return "hello" + s;
}
let c = Component;
let num = 1;
export { c, hello, num };
// types/component.d.ts
export declare function Component(): React.JSX.Element;
// types/main.d.ts
import { type Num } from '../types/types.js';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
// types/types.d.ts
import type { Num2 } from '../types/types2.js';
export type Num = Num2;
// types/types2.d.ts
export type Num2 = number;
"
`;

exports[`rollup > generate basic 1`] = `
"// component.d.ts
export declare function Component(): React.JSX.Element;
Expand Down
24 changes: 24 additions & 0 deletions tests/__snapshots__/rollup/extra-outdir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## main.js

```js


```
## types/main.d.ts

```ts
export * from './utils/types.js';

```
## types/test.d.ts

```ts
export type Str = string;

```
## types/utils/types.d.ts

```ts
export * from '../test.js';

```
1 change: 1 addition & 0 deletions tests/fixtures/extra-outdir/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './utils/types'
1 change: 1 addition & 0 deletions tests/fixtures/extra-outdir/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Str = string
1 change: 1 addition & 0 deletions tests/fixtures/extra-outdir/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../test'
7 changes: 4 additions & 3 deletions tests/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ describe('rollup', () => {
})

test(`extraOutdir`, async () => {
const dir = 'basic'
const dir = 'extra-outdir'
const input = path.resolve(fixtures, dir, 'main.ts')
const dist = path.resolve(TEST_SANDBOX_FOLDER, dir)

const bundle = await rollup({
input,
plugins: [UnpluginIsolatedDecl({ extraOutdir: 'types' }), esbuild()],
logLevel: 'silent',
})
const result = await bundle.generate({})
await bundle.write({ dir: dist })

expect(outputToSnapshot(result.output)).toMatchSnapshot()
await expectSnapshot(dist, `rollup/${dir}`)
})

test('write entry-points (#22)', async () => {
Expand Down

0 comments on commit 5995c5e

Please sign in to comment.