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

@vitejs/plugin-vue will remove the code comment when <script lang="ts"> #7036

Closed
7 tasks done
l1shu opened this issue Feb 22, 2022 · 5 comments
Closed
7 tasks done

@vitejs/plugin-vue will remove the code comment when <script lang="ts"> #7036

l1shu opened this issue Feb 22, 2022 · 5 comments
Labels
pending triage wontfix This will not be worked on

Comments

@l1shu
Copy link

l1shu commented Feb 22, 2022

Describe the bug

my vite plugin:

{
    name: 'client-only',
    transform(code, id, { ssr } = {}) {
        if (ssr) {
            // In ssr case, I need to do some transform according to the code comment. eg: //...
            return transform(code);
        }
    },
}

my vite config:

plugins: [
  vue(),
  clientOnly() // after the vue plugin
]

but I found if the *.vue' file's <script> with lang='ts', the comment in code will be remove:
image
image

btw, the <script> without lang='ts', will keep the comment
image
image

I try to trace the source code
image
Its like doing something in options.compiler.rewriteDefault

How should I keep the comment? Thanks all

Reproduction

https://stackblitz.com/edit/vitejs-vite-myfppw?file=vite.config.ts&terminal=dev

System Info

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 636.03 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 16.4.1 - ~/.nvm/versions/node/v16.4.1/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 7.18.1 - ~/.nvm/versions/node/v16.4.1/bin/npm
  Browsers:
    Chrome: 98.0.4758.102
    Safari: 14.0.3

Used Package Manager

yarn

Logs

No response

Validations

@Miguel-Bento-Github
Copy link

Not sure if this is due to your tsconfig but in your compiler options do you have the "removeComments": true option?

@l1shu
Copy link
Author

l1shu commented Feb 22, 2022

Not sure if this is due to your tsconfig but in your compiler options do you have the "removeComments": true option?

I don't have this option, It still doesnt work even if I set it to false. This is my tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "sourceMap": false,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": [
      "esnext",
      "dom"
    ],
    "skipLibCheck": true,
    "declaration": true,
    "removeComments": false, // set it false
    "paths": {
      "@demo/*": [
        "packages/demo/*"
      ],
      "@shared/*": [
        "packages/shared/*"
      ],
      "@share/*": [
        "packages/share/*"
      ]
    }
  },
  "include": [
    "packages/**/*.ts",
    "packages/**/*.d.ts",
    "packages/**/*.vue",
    "senna.config.ts"
  ],
  "compileOnSave": true
}

@bluwy
Copy link
Member

bluwy commented Feb 22, 2022

TS in Vue is being processed via esbuild. esbuild drops comments by default (evanw/esbuild#221) and there isn't a way to configure it, so I don't think there's a way around it, other than changing your strategy.

It seems like you're doing something like https:/bluwy/vite-plugin-iso-import though. Perhaps that might help.

@l1shu
Copy link
Author

l1shu commented Feb 23, 2022

TS in Vue is being processed via esbuild. esbuild drops comments by default (evanw/esbuild#221) and there isn't a way to configure it, so I don't think there's a way around it, other than changing your strategy.

It seems like you're doing something like https:/bluwy/vite-plugin-iso-import though. Perhaps that might help.

Its helpful for me, Thanks, Stared 👍

@bluwy
Copy link
Member

bluwy commented Feb 23, 2022

I don't think this will be fixed by esbuild soon, and it may as well be intended behaviour for them, as custom comments usually shouldn't affect the bundling output (except special pure comments etc). So I'll close this for now.

@bluwy bluwy closed this as completed Feb 23, 2022
@bluwy bluwy added the wontfix This will not be worked on label Feb 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Mar 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pending triage wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants