Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions lib/npm_ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Deno.test("should include declaration maps of test files", () => {
runTest({
sourceMaps: undefined,
inlineSources: undefined,
expectHasSrcFolder: true,
expectHasSrcFolder: false,
includeScriptModule: true,
includeEsModule: true,
declaration: "inline",
Expand All @@ -119,7 +119,7 @@ Deno.test("should include declaration maps of test files", () => {
runTest({
sourceMaps: undefined,
inlineSources: undefined,
expectHasSrcFolder: true,
expectHasSrcFolder: false,
includeScriptModule: true,
includeEsModule: true,
declaration: "separate",
Expand All @@ -137,6 +137,39 @@ Deno.test("should include declaration maps of test files", () => {
});
});

Deno.test("should keep the src directory when the declaration maps need it", () => {
// declaration maps never inline their sources, so `inlineSources` does not
// remove the need for the src directory like it does for source maps
runTest({
sourceMaps: true,
inlineSources: true,
expectHasSrcFolder: false,
includeScriptModule: true,
includeEsModule: true,
declaration: "inline",
declarationMap: true,
});
runTest({
sourceMaps: "inline",
inlineSources: true,
expectHasSrcFolder: false,
includeScriptModule: true,
includeEsModule: true,
declaration: "separate",
declarationMap: true,
});
// nothing references the src directory, so it's excluded
runTest({
sourceMaps: true,
inlineSources: true,
expectHasSrcFolder: true,
includeScriptModule: true,
includeEsModule: true,
declaration: "inline",
declarationMap: false,
});
});

function runTest(options: {
sourceMaps: SourceMapOptions | undefined;
inlineSources: boolean | undefined;
Expand All @@ -163,6 +196,9 @@ function runTest(options: {

function getExpectedText() {
let startText = options.expectHasSrcFolder ? "/src/\n" : "";
if (!options.expectHasSrcFolder) {
startText += "/src/mod.test.ts\n";
}
if (options.includeEsModule !== false) {
startText += "/esm/mod.test.js\n";
if (options.sourceMaps === true) {
Expand Down
20 changes: 19 additions & 1 deletion lib/npm_ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getNpmIgnoreText(options: {
// Try to make as little of this conditional in case a user edits settings
// to exclude something, but then the output directory still has that file
const lines = [];
if (!isUsingSourceMaps() || options.inlineSources) {
if (!isReferencingSrcDir()) {
lines.push("/src/");
}
for (const fileName of getTestFileNames()) {
Expand All @@ -29,6 +29,10 @@ export function getNpmIgnoreText(options: {
for (const file of options.testFiles) {
const filePath = toJsFilePath(file.filePath);
const dtsFilePath = toDtsFilePath(file.filePath);
// the whole directory is excluded above when it's not published
if (isReferencingSrcDir()) {
yield `/src/${file.filePath}`;
}
if (options.includeEsModule) {
const esmFilePath = `/esm/${filePath}`;
yield esmFilePath;
Expand Down Expand Up @@ -65,6 +69,20 @@ export function getNpmIgnoreText(options: {
yield "/test_runner.cjs";
}

/** Whether any emitted map points back at the files in `/src/`, in which
* case the directory needs to be published for the map to resolve. */
function isReferencingSrcDir() {
// `inlineSources` embeds the sources in the source map, so `/src/` is only
// needed without it. It has no effect on declaration maps though, so those
// always need the directory.
return (isUsingSourceMaps() && !options.inlineSources) ||
isEmittingDeclarationMaps();
}

function isEmittingDeclarationMaps() {
return options.declaration !== false && !!options.declarationMap;
}

function isUsingSourceMaps() {
return options?.sourceMap === "inline" ||
options?.sourceMap === true;
Expand Down
21 changes: 18 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ export interface BuildOptions {
* @default "inline"
*/
declaration?: "inline" | "separate" | false;
/** Create declaration map files. Defaults to `true` if `declaration` is enabled and `skipSourceOutput` is `false`.
/** Create declaration map files so that "go to definition" in an editor
* lands on the original TypeScript rather than the emitted declaration file.
*
* @remarks Enabling this causes the `src` directory to be published, because
* that's what the declaration maps point at. Requires `declaration` to be
* enabled and `skipSourceOutput` to be `false`.
*
* @default false
*/
declarationMap?: boolean;
/** Include a CommonJS or UMD module.
Expand Down Expand Up @@ -278,8 +285,16 @@ export async function build(options: BuildOptions): Promise<void> {
: options.declaration ?? "inline",
};
const cwd = Deno.cwd();
const declarationMap = options.declarationMap ??
(!!options.declaration && !options.skipSourceOutput);
// the declaration maps point at the `src` directory, so they're only useful
// when it's written out and published alongside them
const declarationMap = (options.declarationMap ?? false) &&
!!options.declaration && !options.skipSourceOutput;
if (options.declarationMap && !declarationMap) {
warn(
`Ignoring the 'declarationMap' build option because it requires ` +
`'declaration' to be enabled and 'skipSourceOutput' to be 'false'.`,
);
}
const packageManager = options.packageManager ?? "npm";
const scriptTarget = options.compilerOptions?.target ?? "ES2021";
const polyfills = resolvePolyfillOptions(options.polyfills);
Expand Down
100 changes: 57 additions & 43 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,32 @@ Deno.test("should build test project - basic", async () => {
`/src/
/esm/mod.test.js
/esm/mod.test.d.ts
/esm/mod.test.d.ts.map
/script/mod.test.js
/script/mod.test.d.ts
/script/mod.test.d.ts.map
/esm/deps/deno.land/std@0.181.0/fmt/colors.js
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
/script/deps/deno.land/std@0.181.0/fmt/colors.js
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
/esm/deps/deno.land/std@0.181.0/testing/_diff.js
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_diff.js
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
/esm/deps/deno.land/std@0.181.0/testing/_format.js
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_format.js
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
/esm/deps/deno.land/std@0.181.0/testing/asserts.js
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/asserts.js
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
/esm/_dnt.test_polyfills.js
/esm/_dnt.test_polyfills.d.ts
/esm/_dnt.test_polyfills.d.ts.map
/script/_dnt.test_polyfills.js
/script/_dnt.test_polyfills.d.ts
/script/_dnt.test_polyfills.d.ts.map
/esm/_dnt.test_shims.js
/esm/_dnt.test_shims.d.ts
/esm/_dnt.test_shims.d.ts.map
/script/_dnt.test_shims.js
/script/_dnt.test_shims.d.ts
/script/_dnt.test_shims.d.ts.map
/test_runner.cjs
yarn.lock
pnpm-lock.yaml
Expand Down Expand Up @@ -183,25 +169,18 @@ Deno.test("should build test project without esm", async () => {
`/src/
/script/mod.test.js
/types/mod.test.d.ts
/types/mod.test.d.ts.map
/script/deps/deno.land/std@0.181.0/fmt/colors.js
/types/deps/deno.land/std@0.181.0/fmt/colors.d.ts
/types/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_diff.js
/types/deps/deno.land/std@0.181.0/testing/_diff.d.ts
/types/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_format.js
/types/deps/deno.land/std@0.181.0/testing/_format.d.ts
/types/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/asserts.js
/types/deps/deno.land/std@0.181.0/testing/asserts.d.ts
/types/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
/script/_dnt.test_polyfills.js
/types/_dnt.test_polyfills.d.ts
/types/_dnt.test_polyfills.d.ts.map
/script/_dnt.test_shims.js
/types/_dnt.test_shims.d.ts
/types/_dnt.test_shims.d.ts.map
/test_runner.cjs
yarn.lock
pnpm-lock.yaml
Expand Down Expand Up @@ -334,7 +313,7 @@ Deno.test("should build test project with declarations inline by default", async
}
});

Deno.test("should build test project with declaration maps by default", async () => {
Deno.test("should build test project without declaration maps by default", async () => {
await runTest("test_project", {
entryPoints: ["mod.ts"],
outDir: "./npm",
Expand All @@ -346,16 +325,41 @@ Deno.test("should build test project with declaration maps by default", async ()
name: "add",
version: "1.0.0",
},
}, (output) => {
output.assertNotExists("script/mod.d.ts.map");
output.assertNotExists("esm/mod.d.ts.map");
// nothing points at the sources, so they're not published
assertStringIncludes(output.npmIgnore, "/src/\n");
});
});

Deno.test("should build test project with declaration maps when enabled", async () => {
await runTest("test_project", {
entryPoints: ["mod.ts"],
outDir: "./npm",
declaration: "inline",
declarationMap: true,
shims: {
deno: "dev",
},
package: {
name: "add",
version: "1.0.0",
},
}, (output) => {
output.assertNotExists("types/mod.d.ts");
output.assertExists("script/mod.d.ts.map");
output.assertExists("esm/mod.d.ts.map");
// the declaration maps point at the sources, so they must be published
output.assertExists("src/mod.ts");
assertEquals(output.npmIgnore.includes("/src/\n"), false);
});

await runTest("test_project", {
entryPoints: ["mod.ts"],
outDir: "./npm",
declaration: "separate",
declarationMap: true,
shims: {
deno: "dev",
},
Expand All @@ -367,6 +371,29 @@ Deno.test("should build test project with declaration maps by default", async ()
output.assertExists("types/mod.d.ts.map");
output.assertNotExists("script/mod.d.ts.map");
output.assertNotExists("esm/mod.d.ts.map");
output.assertExists("src/mod.ts");
assertEquals(output.npmIgnore.includes("/src/\n"), false);
});
});

Deno.test("should not create declaration maps when the sources are skipped", async () => {
await runTest("test_project", {
entryPoints: ["mod.ts"],
outDir: "./npm",
declaration: "inline",
declarationMap: true,
skipSourceOutput: true,
shims: {
deno: "dev",
},
package: {
name: "add",
version: "1.0.0",
},
}, (output) => {
output.assertNotExists("src/mod.ts");
output.assertNotExists("script/mod.d.ts.map");
output.assertNotExists("esm/mod.d.ts.map");
});
});

Expand Down Expand Up @@ -569,62 +596,55 @@ Deno.test("should build with source maps", async () => {
output.assertExists("esm/mod.js.map");
assertEquals(
output.npmIgnore,
`/esm/mod.test.js
`/src/mod.test.ts
/esm/mod.test.js
/esm/mod.test.js.map
/esm/mod.test.d.ts
/esm/mod.test.d.ts.map
/script/mod.test.js
/script/mod.test.js.map
/script/mod.test.d.ts
/script/mod.test.d.ts.map
/src/deps/deno.land/std@0.181.0/fmt/colors.ts
/esm/deps/deno.land/std@0.181.0/fmt/colors.js
/esm/deps/deno.land/std@0.181.0/fmt/colors.js.map
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts
/esm/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
/script/deps/deno.land/std@0.181.0/fmt/colors.js
/script/deps/deno.land/std@0.181.0/fmt/colors.js.map
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts
/script/deps/deno.land/std@0.181.0/fmt/colors.d.ts.map
/src/deps/deno.land/std@0.181.0/testing/_diff.ts
/esm/deps/deno.land/std@0.181.0/testing/_diff.js
/esm/deps/deno.land/std@0.181.0/testing/_diff.js.map
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts
/esm/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_diff.js
/script/deps/deno.land/std@0.181.0/testing/_diff.js.map
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts
/script/deps/deno.land/std@0.181.0/testing/_diff.d.ts.map
/src/deps/deno.land/std@0.181.0/testing/_format.ts
/esm/deps/deno.land/std@0.181.0/testing/_format.js
/esm/deps/deno.land/std@0.181.0/testing/_format.js.map
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts
/esm/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/_format.js
/script/deps/deno.land/std@0.181.0/testing/_format.js.map
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts
/script/deps/deno.land/std@0.181.0/testing/_format.d.ts.map
/src/deps/deno.land/std@0.181.0/testing/asserts.ts
/esm/deps/deno.land/std@0.181.0/testing/asserts.js
/esm/deps/deno.land/std@0.181.0/testing/asserts.js.map
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts
/esm/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
/script/deps/deno.land/std@0.181.0/testing/asserts.js
/script/deps/deno.land/std@0.181.0/testing/asserts.js.map
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts
/script/deps/deno.land/std@0.181.0/testing/asserts.d.ts.map
/src/_dnt.test_polyfills.ts
/esm/_dnt.test_polyfills.js
/esm/_dnt.test_polyfills.js.map
/esm/_dnt.test_polyfills.d.ts
/esm/_dnt.test_polyfills.d.ts.map
/script/_dnt.test_polyfills.js
/script/_dnt.test_polyfills.js.map
/script/_dnt.test_polyfills.d.ts
/script/_dnt.test_polyfills.d.ts.map
/src/_dnt.test_shims.ts
/esm/_dnt.test_shims.js
/esm/_dnt.test_shims.js.map
/esm/_dnt.test_shims.d.ts
/esm/_dnt.test_shims.d.ts.map
/script/_dnt.test_shims.js
/script/_dnt.test_shims.js.map
/script/_dnt.test_shims.d.ts
/script/_dnt.test_shims.d.ts.map
/test_runner.cjs
yarn.lock
pnpm-lock.yaml
Expand Down Expand Up @@ -691,11 +711,9 @@ Deno.test("should build with package mappings", async () => {
/esm/mod.test.js
/script/mod.test.js
/types/mod.test.d.ts
/types/mod.test.d.ts.map
/esm/_dnt.test_shims.js
/script/_dnt.test_shims.js
/types/_dnt.test_shims.d.ts
/types/_dnt.test_shims.d.ts.map
/test_runner.cjs
yarn.lock
pnpm-lock.yaml
Expand Down Expand Up @@ -1380,16 +1398,12 @@ Deno.test("should build jsr project", async () => {
`/src/
/esm/mod.test.js
/esm/mod.test.d.ts
/esm/mod.test.d.ts.map
/script/mod.test.js
/script/mod.test.d.ts
/script/mod.test.d.ts.map
/esm/_dnt.test_shims.js
/esm/_dnt.test_shims.d.ts
/esm/_dnt.test_shims.d.ts.map
/script/_dnt.test_shims.js
/script/_dnt.test_shims.d.ts
/script/_dnt.test_shims.d.ts.map
/test_runner.cjs
yarn.lock
pnpm-lock.yaml
Expand Down