【问题标题】:Bundle JS and CSS into single file with Vite用 Vite 将 JS 和 CSS 打包成一个文件
【发布时间】:2021-08-19 04:46:15
【问题描述】:

我正忙着弄清楚如何在我的 Svelte 项目中从 Vite 构建单个 .js 文件,其中包括我的 Svelte 项目中构建的所有 javascript 和 CSS。默认情况下,Vite 将应用程序捆绑到一个 html 文件(这没关系)、两个 .js 文件(为什么??)和一个 .css 文件(只是希望将其捆绑到一个 js 文件中)。

我运行了这个非常基本的命令来获得一个入门项目:
npx degit sveltejs/template myproject

我尝试添加几个插件,但我添加的所有插件都没有达到我想要的结果。首先,我发现的插件似乎想要创建一个包含所有内容的 HTML 文件。似乎 PostCSS 可能会有所帮助,但我不明白我可以通过 Vite 设置什么配置来让它做我想做的事情。

什么是神奇的插件和配置集,可以输出单个 HTML 文件和单个 js 文件,将我的 Svelte 应用程序及其 CSS 呈现到页面上?

【问题讨论】:

  • 您的项目结构是什么样的?你的vite配置?您是否使用任何动态imports?

标签: javascript css bundle svelte vite


【解决方案1】:

我为这个问题创建了一个样板 Vite 项目:
https://github.com/mvsde/svelte-micro-frontend

也许配置对您的情况有帮助:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'

export default defineConfig({
  plugins: [
    svelte({
      emitCss: false
    })
  ],

  build: {
    assetsDir: '',
    sourcemap: true,
    lib: {
      entry: 'src/main.js',
      formats: ['iife'],
      name: 'SvelteMicroFrontend',
      fileName: 'svelte-micro-frontend'
    }
  }
})

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,可以通过编辑 vite.config.ts 来修复,如下在 vite@2.3.8 上测试过的

    export default {
      build: {
        rollupOptions: {
          output: {
            manualChunks: undefined,
          },
        },
      },
    };
    

    【讨论】:

      【解决方案3】:

      这并不是 vite 开箱即用的,但您可以编写一个快速插件来实现这一点

      const bundle_filename = ''
      const css_filename = 'style.css'
      
      defineConfig({
        build: {
          lib: {
            entry: 'src/mycomponent.js',
            name: 'mycomponent.js',
            fileName: () => 'mycomponent.js',
            formats: ['iife'],
          },
          cssCodeSplit: false,
          rollupOptions: {
            plugins: [
              {
                apply: 'build',
                enforce: 'post',
                name: 'pack-css',
                generateBundle(opts, bundle) {
                  const {
                    [css_filename]: { source: rawCss },
                    [bundle_filename]: component,
                  } = bundle
      
                  const IIFEcss = `
                  (function() {
                    try {
                        var elementStyle = document.createElement('style');
                        elementStyle.innerText = ${JSON.stringify(rawCss)}
                        document.head.appendChild(elementStyle)
                    } catch(error) {
                      console.error(error, 'unable to concat style inside the bundled file')
                    }
                  })()`
      
                  component.code += IIFEcss
      
                  // remove from final bundle
                  delete bundle[css_filename]
                },
              },
            ],
          },
        },
      })
      

      【讨论】:

        猜你喜欢
        • 2023-03-07
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2013-06-28
        • 2011-12-21
        • 2014-05-16
        • 1970-01-01
        相关资源
        最近更新 更多