vite icon indicating copy to clipboard operation
vite copied to clipboard

Allow preview mode for libraries

Open yarinsa opened this issue 2 years ago • 7 comments

Clear and concise description of the problem

I am building a web SDK. I used the lib mode to configure my outputs. I am developing the library in esm and my library is gonna be served in cjs. In order to make sure it's working (tests and manually) I need to copy/create index.html file and reference the output script (vite does it on regular mode OOO).

Suggested solution

Therefore I want to allow 'lib preview' let me explain:

  1. Developer run yarn vite preview
  2. Preview server is starting (same as today), but it copies the HTML file to the dist and do the following (this piece of code I wrote in a vite plugin)
    transformIndexHtml: async (htmlString) => {
      const htmlStringWithScript = htmlString.replace(
        `<script type="module" src="./src/index.ts"></script>`,
        `<script src="sdk.cjs.js"></script>`,
      );
      return htmlStringWithScript;
    },

Alternative

Allow configuring 'preview' options to serve libraries

Additional context

I am also willing to contribute and open PR, if the authors will see this feature as meaningful to the platform

Validations

yarinsa avatar Feb 20 '22 15:02 yarinsa