Bootstrap5 Parcel

2021-09-01 16:53 更新

了解如何在你的Bootstrap項(xiàng)目中使用 Parcel。

安裝Parcel

安裝Parcel bundler

安裝Bootstrap

使用 npm 將Bootstrap安裝為 Node.js 模塊。

Bootstrap 依賴于 Popper,它在?peerDependencies?屬性中指定。這意味著您必須確保使用?npm install@popperjs/core?將它們添加到?package.json?中。

全部完成后,您的項(xiàng)目的結(jié)構(gòu)將如下所示:

project-name/
├── build/
├── node_modules/
│   └── bootstrap/
│   └── popper.js/
├── scss/
│   └── custom.scss
├── src/
│   └── index.html
│   └── index.js
└── package.json

導(dǎo)入 JavaScript

在應(yīng)用程序的入口點(diǎn)導(dǎo)入 Bootstrap JavaScript(通常是?src/index.js?)。您可以在一個(gè)文件中導(dǎo)入我們的所有插件,如果您只需要其中的一個(gè)子集,也可以單獨(dú)導(dǎo)入。

// Import all plugins
import * as bootstrap from 'bootstrap';

// Or import only needed plugins
import { Tooltip as Tooltip, Toast as Toast, Popover as Popover } from 'bootstrap';

// Or import just one
import Alert as Alert from '../node_modules/bootstrap/js/dist/alert';

導(dǎo)入 CSS

要充分利用 Bootstrap 的潛力并根據(jù)您的需要對(duì)其進(jìn)行自定義,請(qǐng)將源文件用作項(xiàng)目捆綁過程的一部分。

創(chuàng)建自己?scss/custom.scss?的導(dǎo)入 Bootstrap 的 Sass 文件,然后覆蓋內(nèi)置的自定義變量

構(gòu)建app

在結(jié)束標(biāo)記?</body>?之前包含?src/index.js?

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <script src="./index.js"></script>
  </body>
</html>

編輯 package.json

在您的?package.json?文件中添加?dev?和?build?腳本。

"scripts": {
  "dev": "parcel ./src/index.html",
  "prebuild": "npx rimraf build",
  "build": "parcel build --public-url ./ ./src/index.html --experimental-scope-hoisting --out-dir build"
}

運(yùn)行dev腳本

您的應(yīng)用程序?qū)⑦\(yùn)行在?http://127.0.0.1:1234?。

npm run dev

構(gòu)建應(yīng)用程序文件

構(gòu)建的文件在?build/?文件夾中。

npm run build


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)