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

[2.0.0-alpha.5:yarn dev]: Code trasform result contains type definition when transforming .ts file #1278

Closed
6maple opened this issue Jan 1, 2021 · 0 comments

Comments

@6maple
Copy link

6maple commented Jan 1, 2021

Code trasform result contains type definition when transform .ts file

When export both type definition and others may cause this bug.
This bug only occurs in yarn dev, yarn build works fine.

After checking file response in Devtool Network, I got:

  • main.ts
import {createApp} from "/node_modules/.vite/vue.1a6c6345.js";
import {logPrefix} from "/src/utils/index.ts";
const msg = {
    date: new Date(),
    text: "hello vite2"
};
console.log(logPrefix, msg.text);
createApp({
    render: ()=>null
}).mount("#app");
  • utils/index.ts
export * from "/src/utils/types.ts";
export const logPrefix = "user-vite2:";
  • utils/types.ts
export interface Message {
  date: Date;
  text: string;
}

Then, I debug vite2.0.0-alpha.5 and found something maybe the reason of bug.

When id is utils/types.ts, transformResult.code is '' and typeof transformResult === 'object' && !transformResult.code will be true, the transform result will be treated as no tramsform applied, so the returned code is source code which contains type definition and cause the error in browser console.

Maybe !transformResult.code can change to transformResult.code === code?

node_modules/vite/dist/node/chunks/dep-95ff8110.js line 66700 to 66723.transformRequest

// transform
const transformStart = Date.now();
const transformResult = await pluginContainer.transform(code, id);
if (transformResult == null ||
    (typeof transformResult === 'object' && !transformResult.code)) {
    // no transform applied, keep code as-is
    isDebug$3 &&
        debugTransform(timeFrom(transformStart) + source.dim(` [skipped] ${prettyUrl}`));
}
else {
    isDebug$3 && debugTransform(`${timeFrom(transformStart)} ${prettyUrl}`);
    if (typeof transformResult === 'object') {
        code = transformResult.code;
        map = transformResult.map;
    }
    else {
        code = transformResult;
    }
}
return (mod.transformResult = {
    code,
    map,
    etag: etag_1(code, { weak: true })
});

Reproduction

https://github.com/6maple/test-vite/tree/vite2.0.0-alpha.5-demo-bug2021.1.1

System Info

  • required vite version: 2.0.0-alpha.5
  • required Operating System: windows 10
  • required Node version: 12.14.1
@6maple 6maple changed the title [2.0.0-alpha.5:yarn dev]: Code trasform result contains type definition when transform .ts file [2.0.0-alpha.5:yarn dev]: Code trasform result contains type definition when transforming .ts file Jan 1, 2021
aleclarson pushed a commit to aleclarson/vite that referenced this issue Jan 2, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant