Rekọja si akoonu akọkọ Rekọja si lilọ kiri awọn docs
Check
in English

Bootstrap & Vite

Itọsọna osise fun bi o ṣe le pẹlu ati ṣajọpọ Bootstrap's CSS ati JavaScript ninu iṣẹ akanṣe rẹ nipa lilo Vite.

Ṣe o fẹ lati fo si opin? Ṣe igbasilẹ koodu orisun ati demo iṣẹ fun itọsọna yii lati ibi ipamọ twbs/awọn apẹẹrẹ . O tun le ṣii apẹẹrẹ ni StackBlitz fun ṣiṣatunṣe laaye.

Ṣeto

A n kọ iṣẹ akanṣe Vite pẹlu Bootstrap lati ibere, nitorinaa diẹ ninu awọn ohun pataki wa ati awọn igbesẹ iwaju ṣaaju ki a to le bẹrẹ gaan. Itọsọna yii nilo ki o fi Node.js sori ẹrọ ati imọ diẹ pẹlu ebute naa.

  1. Ṣẹda folda ise agbese ati iṣeto npm. A yoo ṣẹda my-projectfolda naa ki o bẹrẹ npm pẹlu -yariyanjiyan lati yago fun bibeere gbogbo awọn ibeere ibaraenisepo wa.

    mkdir my-project && cd my-project
    npm init -y
    
  2. Fi sori ẹrọ Vite. Ko dabi itọsọna Webpack wa, igbẹkẹle ohun elo ikọle ẹyọkan kan wa nibi. A lo --save-devlati ṣe ifihan pe igbẹkẹle yii jẹ fun lilo idagbasoke nikan kii ṣe fun iṣelọpọ.

    npm i --save-dev vite
    
  3. Fi Bootstrap sori ẹrọ. Bayi a le fi Bootstrap sori ẹrọ. A yoo tun fi Popper sori ẹrọ nitori awọn isọ silẹ wa, awọn agbejade, ati awọn imọran irinṣẹ da lori rẹ fun ipo wọn. Ti o ko ba gbero lori lilo awọn paati yẹn, o le fi Popper silẹ nibi.

    npm i --save bootstrap @popperjs/core
    
  4. Fi igbẹkẹle afikun sii. Ni afikun si Vite ati Bootstrap, a nilo igbẹkẹle miiran (Sass) lati gbe wọle daradara ati ṣajọpọ CSS Bootstrap.

    npm i --save-dev sass
    

Ni bayi pe a ni gbogbo awọn igbẹkẹle pataki ti fi sori ẹrọ ati iṣeto, a le gba lati ṣiṣẹ ṣiṣẹda awọn faili iṣẹ akanṣe ati gbigbe Bootstrap wọle.

Ilana ise agbese

A ti ṣẹda my-projectfolda tẹlẹ ati ṣe ipilẹṣẹ npm. Ni bayi a yoo tun ṣẹda srcfolda wa, iwe aṣa, ati faili JavaScript lati yika eto iṣẹ akanṣe naa. Ṣiṣe awọn atẹle lati my-project, tabi pẹlu ọwọ ṣẹda folda ati ọna faili ti o han ni isalẹ.

mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js

Nigbati o ba ti pari, iṣẹ akanṣe rẹ ni kikun yẹ ki o dabi eyi:

my-project/
├── src/
│   ├── js/
│   │   └── main.js
│   └── scss/
│   |   └── styles.scss
|   └── index.html
├── package-lock.json
├── package.json
└── vite.config.js

Ni aaye yii, ohun gbogbo wa ni aye to tọ, ṣugbọn Vite kii yoo ṣiṣẹ nitori a ko ti kun ninu wa vite.config.jssibẹsibẹ.

Tunto Vite

Pẹlu awọn igbẹkẹle ti a fi sori ẹrọ ati folda agbese wa ti ṣetan fun wa lati bẹrẹ ifaminsi, a le tunto Vite ni bayi ati ṣiṣe iṣẹ akanṣe wa ni agbegbe.

  1. Ṣii vite.config.jsninu olootu rẹ. Niwọn bi o ti ṣofo, a yoo nilo lati ṣafikun atunto igbomikana diẹ si ki a le bẹrẹ olupin wa. Apakan ti atunto naa sọ fun Vite lati wa JavaScript ti iṣẹ akanṣe wa ati bii olupin idagbasoke ṣe yẹ ki o huwa (yilọ lati srcfolda pẹlu atunbere gbona).

    const path = require('path')
    
    export default {
      root: path.resolve(__dirname, 'src'),
      server: {
        port: 8080,
        hot: true
      }
    }
    
  2. Nigbamii ti a fọwọsi src/index.html. Eyi ni oju-iwe HTML Vite yoo kojọpọ ninu ẹrọ aṣawakiri lati lo CSS ti o ṣajọpọ ati JS a yoo ṣafikun si ni awọn igbesẹ nigbamii.

    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap w/ Vite</title>
      </head>
      <body>
        <div class="container py-4 px-3 mx-auto">
          <h1>Hello, Bootstrap and Vite!</h1>
          <button class="btn btn-primary">Primary button</button>
        </div>
        <script type="module" src="./js/main.js"></script>
      </body>
    </html>
    

    A n pẹlu diẹ diẹ ti iselona Bootstrap nibi pẹlu div class="container"ati <button>nitorinaa a rii nigbati Bootstrap's CSS ti kojọpọ nipasẹ Vite.

  3. Bayi a nilo iwe afọwọkọ npm lati ṣiṣẹ Vite. Ṣii package.jsonati ṣafikun iwe startafọwọkọ ti o han ni isalẹ (o yẹ ki o ti ni iwe afọwọkọ idanwo tẹlẹ). A yoo lo iwe afọwọkọ yii lati bẹrẹ olupin Vite dev agbegbe wa.

    {
      // ...
      "scripts": {
        "start": "vite",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      // ...
    }
    
  4. Ati nikẹhin, a le bẹrẹ Vite. Lati my-projectfolda ninu ebute rẹ, ṣiṣẹ iwe afọwọkọ npm tuntun ti a ṣafikun:

    npm start
    
    Vite dev olupin nṣiṣẹ

Ni apakan atẹle ati ipari si itọsọna yii, a yoo gbe gbogbo Bootstrap's CSS ati JavaScript wọle.

Gbe Bootstrap wọle

  1. Ṣeto agbewọle Bootstrap's Sass sinu vite.config.js. Faili iṣeto rẹ ti pari ati pe o yẹ ki o baamu snippet ni isalẹ. Apakan tuntun nikan nibi ni resolveapakan-a lo eyi lati ṣafikun inagijẹ si awọn faili orisun wa inu node_moduleslati jẹ ki awọn agbewọle gbe wọle rọrun bi o ti ṣee.

    const path = require('path')
    
    export default {
      root: path.resolve(__dirname, 'src'),
      resolve: {
        alias: {
          '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
        }
      },
      server: {
        port: 8080,
        hot: true
      }
    }
    
  2. Bayi, jẹ ki a gbe Bootstrap's CSS wọle. Ṣafikun atẹle naa src/scss/styles.scsslati gbe gbogbo orisun Bootstrap Sass wọle.

    // Import all of Bootstrap's CSS
    @import "~bootstrap/scss/bootstrap";
    

    O tun le gbe awọn iwe aṣa wa wọle lọkọọkan ti o ba fẹ. Ka awọn iwe aṣẹ agbewọle Sass wa fun awọn alaye.

  3. Nigbamii ti a fifuye CSS ati gbe Bootstrap JavaScript wọle. Ṣafikun atẹle naa src/js/main.jslati ṣaja CSS ati gbe gbogbo Bootstrap's JS wọle. Popper yoo wa ni akowọle laifọwọyi nipasẹ Bootstrap.

    // Import our custom CSS
    import '../scss/styles.scss'
    
    // Import all of Bootstrap's JS
    import * as bootstrap from 'bootstrap'
    

    O tun le gbe awọn afikun JavaScript wọle lọkọọkan bi o ṣe nilo lati tọju awọn iwọn lapapo si isalẹ:

    import Alert from 'bootstrap/js/dist/alert';
    
    // or, specify which plugins you need:
    import { Tooltip, Toast, Popover } from 'bootstrap';
    

    Ka awọn iwe aṣẹ JavaScript wa fun alaye diẹ sii lori bi a ṣe le lo awọn afikun Bootstrap.

  4. Ati pe o ti pari! 🎉 Pẹlu orisun Bootstrap Sass ati JS ti kojọpọ ni kikun, olupin idagbasoke agbegbe rẹ yẹ ki o dabi eyi.

    olupin Vite dev nṣiṣẹ pẹlu Bootstrap

    Bayi o le bẹrẹ fifi eyikeyi awọn paati Bootstrap ti o fẹ lati lo. Rii daju lati ṣayẹwo iṣẹ apẹẹrẹ Vite pipe fun bii o ṣe le pẹlu afikun aṣa Sass ki o mu kikọ rẹ pọ si nipa gbigbe wọle nikan awọn apakan ti Bootstrap's CSS ati JS ti o nilo.


Ri nkankan ti ko tọ tabi ti ọjọ nibi? Jọwọ ṣii ọrọ kan lori GitHub . Nilo iranlọwọ laasigbotitusita? Wa tabi bẹrẹ ijiroro lori GitHub.