Skip to content
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

test: resolve ts extension when query params used #12586

Merged
merged 1 commit into from Mar 26, 2023

Conversation

AriPerkkio
Copy link
Contributor

Description

Adds test case for the accidental fix included in #12571. (cc @patak-dev 👋)

├── source.ts
└── target.mts
// source.ts
import { hello } from "./target.mjs"; // Notice extension

// target.mts
export const hello = () => "Hello from .mts";
// OK
await server.transformRequest("./source.ts")

// Fails
await server.transformRequest("./source.ts?v=123")

To see the added test case fail, change following:

const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/

-const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/
+const knownTsRE = /\.(?:ts|mts|cts|tsx)$/

The minimal reproduction case below fails with latest release 4.3.0-beta.0. The change described in #12571 (comment) fixed it.

import { writeFileSync } from "fs";
import { fileURLToPath } from "url";
import { createServer } from "vite";

writeFileSync(
  "./source.ts",
  `import { hello } from "./target.mjs"; // Change this to ".mts" or omit extension and it works too
  hello();`,
  "utf8"
);
writeFileSync(
  "./target.mts",
  `export const hello = () => "Hello from .mts";`,
  "utf8"
);

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const server = await createServer({
  configFile: false,
  root: __dirname,
  server: {
    port: 1337,
  },
});
await server.listen();

const first = await server.transformRequest("./source.ts");
console.log("First worked!", first.code);

await new Promise((resolve) => setTimeout(resolve, 2000));

const second = await server
  .transformRequest("./source.ts?v=123")
  .then((result) => console.log("Second worked!", result.code))
  .catch((e) => console.error("Second failed", e));

await server.close();
process.exit();

Additional context

I'm not that familiar with Vite's test cases and the playground setup.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Mar 26, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up with the test case! 🙏🏼

@patak-dev patak-dev merged commit 2d9e691 into vitejs:main Mar 26, 2023
11 checks passed
@AriPerkkio AriPerkkio deleted the test/resolve-ts-with-query branch March 26, 2023 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants