Skip to content

Commit 9a1daa1

Browse files
authored
fix: only publish src directory when something references it (#506)
1 parent 691d6d4 commit 9a1daa1

4 files changed

Lines changed: 132 additions & 49 deletions

File tree

lib/npm_ignore.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Deno.test("should include declaration maps of test files", () => {
101101
runTest({
102102
sourceMaps: undefined,
103103
inlineSources: undefined,
104-
expectHasSrcFolder: true,
104+
expectHasSrcFolder: false,
105105
includeScriptModule: true,
106106
includeEsModule: true,
107107
declaration: "inline",
@@ -119,7 +119,7 @@ Deno.test("should include declaration maps of test files", () => {
119119
runTest({
120120
sourceMaps: undefined,
121121
inlineSources: undefined,
122-
expectHasSrcFolder: true,
122+
expectHasSrcFolder: false,
123123
includeScriptModule: true,
124124
includeEsModule: true,
125125
declaration: "separate",
@@ -137,6 +137,39 @@ Deno.test("should include declaration maps of test files", () => {
137137
});
138138
});
139139

140+
Deno.test("should keep the src directory when the declaration maps need it", () => {
141+
// declaration maps never inline their sources, so `inlineSources` does not
142+
// remove the need for the src directory like it does for source maps
143+
runTest({
144+
sourceMaps: true,
145+
inlineSources: true,
146+
expectHasSrcFolder: false,
147+
includeScriptModule: true,
148+
includeEsModule: true,
149+
declaration: "inline",
150+
declarationMap: true,
151+
});
152+
runTest({
153+
sourceMaps: "inline",
154+
inlineSources: true,
155+
expectHasSrcFolder: false,
156+
includeScriptModule: true,
157+
includeEsModule: true,
158+
declaration: "separate",
159+
declarationMap: true,
160+
});
161+
// nothing references the src directory, so it's excluded
162+
runTest({
163+
sourceMaps: true,
164+
inlineSources: true,
165+
expectHasSrcFolder: true,
166+
includeScriptModule: true,
167+
includeEsModule: true,
168+
declaration: "inline",
169+
declarationMap: false,
170+
});
171+
});
172+
140173
function runTest(options: {
141174
sourceMaps: SourceMapOptions | undefined;
142175
inlineSources: boolean | undefined;
@@ -163,6 +196,9 @@ function runTest(options: {
163196

164197
function getExpectedText() {
165198
let startText = options.expectHasSrcFolder ? "/src/\n" : "";
199+
if (!options.expectHasSrcFolder) {
200+
startText += "/src/mod.test.ts\n";
201+
}
166202
if (options.includeEsModule !== false) {
167203
startText += "/esm/mod.test.js\n";
168204
if (options.sourceMaps === true) {

lib/npm_ignore.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function getNpmIgnoreText(options: {
1616
// Try to make as little of this conditional in case a user edits settings
1717
// to exclude something, but then the output directory still has that file
1818
const lines = [];
19-
if (!isUsingSourceMaps() || options.inlineSources) {
19+
if (!isReferencingSrcDir()) {
2020
lines.push("/src/");
2121
}
2222
for (const fileName of getTestFileNames()) {
@@ -29,6 +29,10 @@ export function getNpmIgnoreText(options: {
2929
for (const file of options.testFiles) {
3030
const filePath = toJsFilePath(file.filePath);
3131
const dtsFilePath = toDtsFilePath(file.filePath);
32+
// the whole directory is excluded above when it's not published
33+
if (isReferencingSrcDir()) {
34+
yield `/src/${file.filePath}`;
35+
}
3236
if (options.includeEsModule) {
3337
const esmFilePath = `/esm/${filePath}`;
3438
yield esmFilePath;
@@ -65,6 +69,20 @@ export function getNpmIgnoreText(options: {
6569
yield "/test_runner.cjs";
6670
}
6771

72+
/** Whether any emitted map points back at the files in `/src/`, in which
73+
* case the directory needs to be published for the map to resolve. */
74+
function isReferencingSrcDir() {
75+
// `inlineSources` embeds the sources in the source map, so `/src/` is only
76+
// needed without it. It has no effect on declaration maps though, so those
77+
// always need the directory.
78+
return (isUsingSourceMaps() && !options.inlineSources) ||
79+
isEmittingDeclarationMaps();
80+
}
81+
82+
function isEmittingDeclarationMaps() {
83+
return options.declaration !== false && !!options.declarationMap;
84+
}
85+
6886
function isUsingSourceMaps() {
6987
return options?.sourceMap === "inline" ||
7088
options?.sourceMap === true;

mod.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ export interface BuildOptions {
8989
* @default "inline"
9090
*/
9191
declaration?: "inline" | "separate" | false;
92-
/** Create declaration map files. Defaults to `true` if `declaration` is enabled and `skipSourceOutput` is `false`.
92+
/** Create declaration map files so that "go to definition" in an editor
93+
* lands on the original TypeScript rather than the emitted declaration file.
94+
*
95+
* @remarks Enabling this causes the `src` directory to be published, because
96+
* that's what the declaration maps point at. Requires `declaration` to be
97+
* enabled and `skipSourceOutput` to be `false`.
98+
*
99+
* @default false
93100
*/
94101
declarationMap?: boolean;
95102
/** Include a CommonJS or UMD module.
@@ -292,8 +299,16 @@ export async function build(options: BuildOptions): Promise<void> {
292299
: options.declaration ?? "inline",
293300
};
294301
const cwd = Deno.cwd();
295-
const declarationMap = options.declarationMap ??
296-
(!!options.declaration && !options.skipSourceOutput);
302+
// the declaration maps point at the `src` directory, so they're only useful
303+
// when it's written out and published alongside them
304+
const declarationMap = (options.declarationMap ?? false) &&
305+
!!options.declaration && !options.skipSourceOutput;
306+
if (options.declarationMap && !declarationMap) {
307+
warn(
308+
`Ignoring the 'declarationMap' build option because it requires ` +
309+
`'declaration' to be enabled and 'skipSourceOutput' to be 'false'.`,
310+
);
311+
}
297312
const packageManager = options.packageManager ?? "npm";
298313
const scriptTarget = options.compilerOptions?.target ?? "ES2021";
299314
const polyfills = resolvePolyfillOptions(options.polyfills);

tests/integration.test.ts

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,46 +90,32 @@ Deno.test("should build test project - basic", async () => {
9090
`/src/
9191
/esm/mod.test.js
9292
/esm/mod.test.d.ts
93-
/esm/mod.test.d.ts.map
9493
/script/mod.test.js
9594
/script/mod.test.d.ts
96-
/script/mod.test.d.ts.map
9795
/esm/deps/deno.land/std@0.181.0/fmt/colors.js
9896
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts
99-
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
10097
/script/deps/deno.land/std@0.181.0/fmt/colors.js
10198
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts
102-
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
10399
/esm/deps/deno.land/std@0.181.0/testing/_diff.js
104100
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts
105-
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
106101
/script/deps/deno.land/std@0.181.0/testing/_diff.js
107102
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts
108-
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
109103
/esm/deps/deno.land/std@0.181.0/testing/_format.js
110104
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts
111-
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
112105
/script/deps/deno.land/std@0.181.0/testing/_format.js
113106
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts
114-
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
115107
/esm/deps/deno.land/std@0.181.0/testing/asserts.js
116108
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts
117-
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
118109
/script/deps/deno.land/std@0.181.0/testing/asserts.js
119110
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts
120-
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
121111
/esm/_dnt.test_polyfills.js
122112
/esm/_dnt.test_polyfills.d.ts
123-
/esm/_dnt.test_polyfills.d.ts.map
124113
/script/_dnt.test_polyfills.js
125114
/script/_dnt.test_polyfills.d.ts
126-
/script/_dnt.test_polyfills.d.ts.map
127115
/esm/_dnt.test_shims.js
128116
/esm/_dnt.test_shims.d.ts
129-
/esm/_dnt.test_shims.d.ts.map
130117
/script/_dnt.test_shims.js
131118
/script/_dnt.test_shims.d.ts
132-
/script/_dnt.test_shims.d.ts.map
133119
/test_runner.cjs
134120
yarn.lock
135121
pnpm-lock.yaml
@@ -183,25 +169,18 @@ Deno.test("should build test project without esm", async () => {
183169
`/src/
184170
/script/mod.test.js
185171
/types/mod.test.d.ts
186-
/types/mod.test.d.ts.map
187172
/script/deps/deno.land/std@0.181.0/fmt/colors.js
188173
/types/deps/deno.land/std@0.181.0/fmt/colors.d.ts
189-
/types/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
190174
/script/deps/deno.land/std@0.181.0/testing/_diff.js
191175
/types/deps/deno.land/std@0.181.0/testing/_diff.d.ts
192-
/types/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
193176
/script/deps/deno.land/std@0.181.0/testing/_format.js
194177
/types/deps/deno.land/std@0.181.0/testing/_format.d.ts
195-
/types/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
196178
/script/deps/deno.land/std@0.181.0/testing/asserts.js
197179
/types/deps/deno.land/std@0.181.0/testing/asserts.d.ts
198-
/types/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
199180
/script/_dnt.test_polyfills.js
200181
/types/_dnt.test_polyfills.d.ts
201-
/types/_dnt.test_polyfills.d.ts.map
202182
/script/_dnt.test_shims.js
203183
/types/_dnt.test_shims.d.ts
204-
/types/_dnt.test_shims.d.ts.map
205184
/test_runner.cjs
206185
yarn.lock
207186
pnpm-lock.yaml
@@ -334,7 +313,7 @@ Deno.test("should build test project with declarations inline by default", async
334313
}
335314
});
336315

337-
Deno.test("should build test project with declaration maps by default", async () => {
316+
Deno.test("should build test project without declaration maps by default", async () => {
338317
await runTest("test_project", {
339318
entryPoints: ["mod.ts"],
340319
outDir: "./npm",
@@ -346,16 +325,41 @@ Deno.test("should build test project with declaration maps by default", async ()
346325
name: "add",
347326
version: "1.0.0",
348327
},
328+
}, (output) => {
329+
output.assertNotExists("script/mod.d.ts.map");
330+
output.assertNotExists("esm/mod.d.ts.map");
331+
// nothing points at the sources, so they're not published
332+
assertStringIncludes(output.npmIgnore, "/src/\n");
333+
});
334+
});
335+
336+
Deno.test("should build test project with declaration maps when enabled", async () => {
337+
await runTest("test_project", {
338+
entryPoints: ["mod.ts"],
339+
outDir: "./npm",
340+
declaration: "inline",
341+
declarationMap: true,
342+
shims: {
343+
deno: "dev",
344+
},
345+
package: {
346+
name: "add",
347+
version: "1.0.0",
348+
},
349349
}, (output) => {
350350
output.assertNotExists("types/mod.d.ts");
351351
output.assertExists("script/mod.d.ts.map");
352352
output.assertExists("esm/mod.d.ts.map");
353+
// the declaration maps point at the sources, so they must be published
354+
output.assertExists("src/mod.ts");
355+
assertEquals(output.npmIgnore.includes("/src/\n"), false);
353356
});
354357

355358
await runTest("test_project", {
356359
entryPoints: ["mod.ts"],
357360
outDir: "./npm",
358361
declaration: "separate",
362+
declarationMap: true,
359363
shims: {
360364
deno: "dev",
361365
},
@@ -367,6 +371,29 @@ Deno.test("should build test project with declaration maps by default", async ()
367371
output.assertExists("types/mod.d.ts.map");
368372
output.assertNotExists("script/mod.d.ts.map");
369373
output.assertNotExists("esm/mod.d.ts.map");
374+
output.assertExists("src/mod.ts");
375+
assertEquals(output.npmIgnore.includes("/src/\n"), false);
376+
});
377+
});
378+
379+
Deno.test("should not create declaration maps when the sources are skipped", async () => {
380+
await runTest("test_project", {
381+
entryPoints: ["mod.ts"],
382+
outDir: "./npm",
383+
declaration: "inline",
384+
declarationMap: true,
385+
skipSourceOutput: true,
386+
shims: {
387+
deno: "dev",
388+
},
389+
package: {
390+
name: "add",
391+
version: "1.0.0",
392+
},
393+
}, (output) => {
394+
output.assertNotExists("src/mod.ts");
395+
output.assertNotExists("script/mod.d.ts.map");
396+
output.assertNotExists("esm/mod.d.ts.map");
370397
});
371398
});
372399

@@ -569,62 +596,55 @@ Deno.test("should build with source maps", async () => {
569596
output.assertExists("esm/mod.js.map");
570597
assertEquals(
571598
output.npmIgnore,
572-
`/esm/mod.test.js
599+
`/src/mod.test.ts
600+
/esm/mod.test.js
573601
/esm/mod.test.js.map
574602
/esm/mod.test.d.ts
575-
/esm/mod.test.d.ts.map
576603
/script/mod.test.js
577604
/script/mod.test.js.map
578605
/script/mod.test.d.ts
579-
/script/mod.test.d.ts.map
606+
/src/deps/deno.land/std@0.181.0/fmt/colors.ts
580607
/esm/deps/deno.land/std@0.181.0/fmt/colors.js
581608
/esm/deps/deno.land/std@0.181.0/fmt/colors.js.map
582609
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts
583-
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
584610
/script/deps/deno.land/std@0.181.0/fmt/colors.js
585611
/script/deps/deno.land/std@0.181.0/fmt/colors.js.map
586612
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts
587-
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
613+
/src/deps/deno.land/std@0.181.0/testing/_diff.ts
588614
/esm/deps/deno.land/std@0.181.0/testing/_diff.js
589615
/esm/deps/deno.land/std@0.181.0/testing/_diff.js.map
590616
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts
591-
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
592617
/script/deps/deno.land/std@0.181.0/testing/_diff.js
593618
/script/deps/deno.land/std@0.181.0/testing/_diff.js.map
594619
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts
595-
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
620+
/src/deps/deno.land/std@0.181.0/testing/_format.ts
596621
/esm/deps/deno.land/std@0.181.0/testing/_format.js
597622
/esm/deps/deno.land/std@0.181.0/testing/_format.js.map
598623
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts
599-
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
600624
/script/deps/deno.land/std@0.181.0/testing/_format.js
601625
/script/deps/deno.land/std@0.181.0/testing/_format.js.map
602626
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts
603-
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
627+
/src/deps/deno.land/std@0.181.0/testing/asserts.ts
604628
/esm/deps/deno.land/std@0.181.0/testing/asserts.js
605629
/esm/deps/deno.land/std@0.181.0/testing/asserts.js.map
606630
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts
607-
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
608631
/script/deps/deno.land/std@0.181.0/testing/asserts.js
609632
/script/deps/deno.land/std@0.181.0/testing/asserts.js.map
610633
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts
611-
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
634+
/src/_dnt.test_polyfills.ts
612635
/esm/_dnt.test_polyfills.js
613636
/esm/_dnt.test_polyfills.js.map
614637
/esm/_dnt.test_polyfills.d.ts
615-
/esm/_dnt.test_polyfills.d.ts.map
616638
/script/_dnt.test_polyfills.js
617639
/script/_dnt.test_polyfills.js.map
618640
/script/_dnt.test_polyfills.d.ts
619-
/script/_dnt.test_polyfills.d.ts.map
641+
/src/_dnt.test_shims.ts
620642
/esm/_dnt.test_shims.js
621643
/esm/_dnt.test_shims.js.map
622644
/esm/_dnt.test_shims.d.ts
623-
/esm/_dnt.test_shims.d.ts.map
624645
/script/_dnt.test_shims.js
625646
/script/_dnt.test_shims.js.map
626647
/script/_dnt.test_shims.d.ts
627-
/script/_dnt.test_shims.d.ts.map
628648
/test_runner.cjs
629649
yarn.lock
630650
pnpm-lock.yaml
@@ -691,11 +711,9 @@ Deno.test("should build with package mappings", async () => {
691711
/esm/mod.test.js
692712
/script/mod.test.js
693713
/types/mod.test.d.ts
694-
/types/mod.test.d.ts.map
695714
/esm/_dnt.test_shims.js
696715
/script/_dnt.test_shims.js
697716
/types/_dnt.test_shims.d.ts
698-
/types/_dnt.test_shims.d.ts.map
699717
/test_runner.cjs
700718
yarn.lock
701719
pnpm-lock.yaml
@@ -1380,16 +1398,12 @@ Deno.test("should build jsr project", async () => {
13801398
`/src/
13811399
/esm/mod.test.js
13821400
/esm/mod.test.d.ts
1383-
/esm/mod.test.d.ts.map
13841401
/script/mod.test.js
13851402
/script/mod.test.d.ts
1386-
/script/mod.test.d.ts.map
13871403
/esm/_dnt.test_shims.js
13881404
/esm/_dnt.test_shims.d.ts
1389-
/esm/_dnt.test_shims.d.ts.map
13901405
/script/_dnt.test_shims.js
13911406
/script/_dnt.test_shims.d.ts
1392-
/script/_dnt.test_shims.d.ts.map
13931407
/test_runner.cjs
13941408
yarn.lock
13951409
pnpm-lock.yaml

0 commit comments

Comments
 (0)