Parcel
了解如何使用Parcel在項(xiàng)目中包含bootstrap。
On this page
安裝 Parcel
Install Parcel Bundler.
Install Bootstrap
Install bootstrap as a Node.js module using npm.
Bootstrap依賴于 Popper,Popper在peerDependencies
屬性中指定。這意味著您必須確保將它們用npm install popper.js
都添加到您的package.json
包。
當(dāng)所有工作都完成時(shí),您的項(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)入引導(dǎo)程序的JavaScript, 你可以在一個(gè)文件中導(dǎo)入我們所有的插件,如果你只需要它們的一個(gè)子集,也可以單獨(dú)導(dǎo)入。
// 導(dǎo)入所有插件
import * as bootstrap from 'bootstrap';
// 導(dǎo)入需要的幾個(gè)插件
import { Tooltip as Tooltip, Toast as Toast, Popover as Popover } from 'bootstrap';
// 導(dǎo)入一個(gè)插件
import Alert as Alert from '../node_modules/bootstrap/js/dist/alert';
導(dǎo)入 CSS
T要充分利用Bootstrap的潛力并根據(jù)您的需要進(jìn)行定制,請(qǐng)將源文件當(dāng)作項(xiàng)目的一部分。
創(chuàng)建自己的scss/custom.scss
,導(dǎo)入Bootstrap的Sass文件,然后重寫(xiě)內(nèi)置的自定義變量。
創(chuàng)建應(yīng)用
在 </body>
標(biāo)簽前包含 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)行代碼
你可以通過(guò) http://127.0.0.1:1234
訪問(wèn)你的應(yīng)用。
npm run dev
創(chuàng)建應(yīng)用文件
創(chuàng)建的文件在 build/
目錄。
npm run build