Skip to content

fix(dts): force emit dts for build-in vue loader #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/loaders/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
].filter((item) => !!item);

// generate dts
await context.loadFile({
const files = await context.loadFile({
path: `${input.path}.js`,
srcPath: `${input.srcPath}.js`,
extension: ".js",
getContents: () => "export default {}",
});
addOutput(...(files?.filter((f) => f.declaration) || []));

const results = await Promise.all(
blocks.map(async (data) => {
Expand All @@ -99,7 +100,14 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
);

if (!modified) {
return;
addOutput({
path: input.path,
srcPath: input.srcPath,
extension: ".vue",
contents: raw,
declaration: false,
});
return output;
}

// skiped blocks
Expand Down
180 changes: 177 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
afterAll,
} from "vitest";
import { createLoader } from "../src/loader";
import { afterEach } from "vitest";

describe("mkdist", () => {
let mkdist: typeof import("../src/make").mkdist;
Expand Down Expand Up @@ -639,7 +638,7 @@ describe("mkdist", () => {

describe("mkdist with fallback vue loader", () => {
const consoleWarnSpy = vi.spyOn(console, "warn");
beforeAll(() => {
beforeAll(async () => {
vi.resetModules();
vi.doMock("vue-sfc-transformer/mkdist", async () => {
throw new Error("vue-sfc-transformer is not installed");
Expand All @@ -650,7 +649,7 @@ describe("mkdist with fallback vue loader", () => {
vi.doUnmock("vue-sfc-transformer/mkdist");
});

afterEach(() => {
beforeEach(() => {
consoleWarnSpy.mockReset();
});

Expand Down Expand Up @@ -731,6 +730,181 @@ describe("mkdist with fallback vue loader", () => {
}
});

describe("mkdist with fallback vue loader (emit types)", () => {
let mkdist: typeof import("../src/make").mkdist;

const consoleWarnSpy = vi.spyOn(console, "warn");
beforeAll(async () => {
mkdist = (await import("../src/make")).mkdist;

vi.resetModules();
vi.doMock("vue-sfc-transformer/mkdist", async () => {
throw new Error("vue-sfc-transformer is not installed");
});
});

afterAll(() => {
vi.doUnmock("vue-sfc-transformer/mkdist");
});

beforeEach(() => {
consoleWarnSpy.mockReset();
});

it("emit types", async () => {
const rootDir = resolve(__dirname, "fixture");
const { writtenFiles } = await mkdist({
rootDir,
declaration: true,
addRelativeDeclarationExtensions: true,
});
expect(writtenFiles.sort()).toEqual(
[
"dist/README.md",
"dist/bar.d.ts",
"dist/bar.mjs",
"dist/demo.css",
"dist/dir-export.d.ts",
"dist/dir-export.mjs",
"dist/foo.mjs",
"dist/foo.d.ts",
"dist/index.mjs",
"dist/index.d.ts",
"dist/star/index.mjs",
"dist/star/index.d.ts",
"dist/star/other.mjs",
"dist/star/other.d.ts",
"dist/types.d.ts",
"dist/components/index.mjs",
"dist/components/index.d.ts",
"dist/components/blank.vue",
"dist/components/blank.vue.d.ts",
"dist/components/define-model.vue",
"dist/components/define-model.vue.d.ts",
"dist/components/emit-and-with-default.vue",
"dist/components/emit-and-with-default.vue.d.ts",
"dist/components/js.vue",
"dist/components/js.vue.d.ts",
"dist/components/script-multi-block.vue",
"dist/components/script-multi-block.vue.d.ts",
"dist/components/script-setup-ts.vue",
"dist/components/script-setup-ts.vue.d.ts",
"dist/components/ts.vue",
"dist/components/ts.vue.d.ts",
"dist/components/jsx.mjs",
"dist/components/tsx.mjs",
"dist/components/jsx.d.ts",
"dist/components/tsx.d.ts",
"dist/bar/index.mjs",
"dist/bar/index.d.ts",
"dist/bar/esm.mjs",
"dist/bar/esm.d.mts",
"dist/ts/test1.mjs",
"dist/ts/test2.mjs",
"dist/ts/test1.d.mts",
"dist/ts/test2.d.cts",
"dist/nested.css",
"dist/prop-types/index.mjs",
"dist/prop-types/index.d.ts",
]
.map((f) => resolve(rootDir, f))
.sort(),
);

expect(await readFile(resolve(rootDir, "dist/foo.d.ts"), "utf8")).toMatch(
"manual declaration",
);

expect(await readFile(resolve(rootDir, "dist/star/index.d.ts"), "utf8"))
.toMatchInlineSnapshot(`
"export * from "./other.js";
export type { Other } from "./other.js";
export declare function wonder(twinkle: import("./other.js").Other): string;
"
`);

expect(await readFile(resolve(rootDir, "dist/dir-export.d.ts"), "utf8"))
.toMatchInlineSnapshot(`
"export { default as bar } from "./bar.js";
export * from "./star/index.js";
"
`);

expect(
await readFile(resolve(rootDir, "dist/bar/esm.d.mts"), "utf8"),
).toMatch("declare");

expect(
await readFile(resolve(rootDir, "dist/components/index.d.ts"), "utf8"),
).toMatchInlineSnapshot(`
"export * as jsx from "./jsx.jsx.js";
export * as tsx from "./tsx.tsx.js";
export * as blank from "./blank.vue.js";
export * as scriptSetupTS from "./script-setup-ts.vue.js";
export * as scriptMultiBlock from "./script-multi-block.vue.js";
export * as ts from "./ts.vue.js";
"
`);

expect(
await readFile(resolve(rootDir, "dist/components/ts.vue.d.ts"), "utf8"),
).toMatchInlineSnapshot(`
"declare const _default: import("vue").DefineComponent<{}, {}, {
test: string;
str: "test";
}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
export default _default;
"
`);

expect(
await readFile(
resolve(rootDir, "dist/components/blank.vue.d.ts"),
"utf8",
),
).toMatchInlineSnapshot(`
"declare const _default: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
export default _default;
"
`);

expect(
await readFile(
resolve(rootDir, "dist/components/script-multi-block.vue.d.ts"),
"utf8",
),
).toMatchInlineSnapshot(`
"interface MyComponentProps {
msg: string;
}
declare const _default: import("vue").DefineComponent<MyComponentProps, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<MyComponentProps> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
export default _default;
"
`);

expect(
await readFile(
resolve(rootDir, "dist/components/script-setup-ts.vue.d.ts"),
"utf8",
),
).toMatchInlineSnapshot(`
"import { Color } from "#prop-types";
type __VLS_Props = {
msg: string;
color: Color;
};
declare const _default: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
export default _default;
"
`);

expect(consoleWarnSpy).toHaveBeenCalledWith(
"[mkdist] vue-sfc-transformer is not installed. mkdist will not transform typescript syntax in Vue SFCs.",
);
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
}, 50_000);
});

describe("mkdist with vue-tsc v1", () => {
beforeAll(() => {
vi.resetModules();
Expand Down
Loading