question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
l1shucommented, 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://github.com/bluwy/vite-plugin-iso-import though. Perhaps that might help.

Its helpful for me, Thanks, Stared 👍

0reactions
bluwycommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code snippet getting auto removed from Additional Comments
Solved: When trying to add a code snippet into the Additional Comments on an Incident, ServiceNow removes it and only displays For example,...
Read more >
Does removing comments improve code performance ...
Removing comments will make the Javascript file smaller and easier to download. Other than that, it will not affect noticably performance at all....
Read more >
Best practices for writing code comments - Stack Overflow Blog
Rule 1: Comments should not duplicate the code. Rule 2: Good comments do not excuse unclear code. Rule 3: If you can't write...
Read more >
Clarifying Code with Javascript Comments | Udacity
This strategy allows you to locate bugs, progressively removing comments until you find the problematic code. Commenting Out Function Calls.
Read more >
Putting comments in code: the good, the bad, and the ugly.
Often, a clarification comment is a code smell. It tells you that your code is too complex. You should strive to remove clarification...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found