Skip to content

Instantly share code, notes, and snippets.

@ShepetaAndrey
Created April 27, 2022 20:24
Show Gist options
  • Save ShepetaAndrey/faa0daa466ddb079f6786e575d081192 to your computer and use it in GitHub Desktop.
Save ShepetaAndrey/faa0daa466ddb079f6786e575d081192 to your computer and use it in GitHub Desktop.
Vue 3 + Vite + Bootstrap + PurgeCSS

Setup

Bootstrap

  1. Install package
npm i bootstrap
  1. Create bootstrap.sass [css, scss, ...] file in assets/ folder and import Bootstrap styles
@import '../../node_modules/bootstrap/scss/bootstrap'
  1. Import this file into src/main.js to plug in styles to your Vue app
import "./assets/bootstrap.sass";

PurgeCSS

  1. Install dependencies
npm i -D purgecss @fullhuman/postcss-purgecss
  1. Add postcss.config.js file to the root of the project (near package.json)
const postCssPurge = require("@fullhuman/postcss-purgecss");

const vuePath = /\.vue(\?.+)?$/;

module.exports = {
  plugins: [
    postCssPurge({
      contentFunction: (sourceInputFile) => {
        if (vuePath.test(sourceInputFile)) {
          return [sourceInputFile.replace(vuePath, ".vue")];
        }
        return ["src/**/*.vue", "index.html"];
      },
      defaultExtractor(content) {
        if (content.startsWith("<template")) {
          content = `${content.split("</template")[0]}</template>`;
        }
        return content.match(/[\w-/:]+(?<!:)/g) || [];
      },
    }),
  ],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment