Skip to content

Commit 99867eb

Browse files
julien-deramondXhmikosR
andauthoredMar 23, 2023
Update Vite, Parcel and Webpack guides (#38305)
Co-authored-by: XhmikosR <xhmikosr@gmail.com>
1 parent b3faa0c commit 99867eb

File tree

3 files changed

+52
-50
lines changed

3 files changed

+52
-50
lines changed
 

‎site/content/docs/5.3/getting-started/parcel.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Importing Bootstrap into Parcel requires two imports, one into our `styles.scss`
123123

124124
```scss
125125
// Import all of Bootstrap's CSS
126-
@import "~bootstrap/scss/bootstrap";
126+
@import "bootstrap/scss/bootstrap";
127127
```
128128

129129
*You can also import our stylesheets individually if you want. [Read our Sass import docs]({{< docsref "/customize/sass#importing" >}}) for details.*

‎site/content/docs/5.3/getting-started/vite.md

+8-25
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ With dependencies installed and our project folder ready for us to start coding,
8282

8383
export default {
8484
root: path.resolve(__dirname, 'src'),
85+
build: {
86+
outDir: '../dist'
87+
},
8588
server: {
8689
port: 8080,
8790
hot: true
@@ -98,13 +101,13 @@ With dependencies installed and our project folder ready for us to start coding,
98101
<meta charset="utf-8">
99102
<meta name="viewport" content="width=device-width, initial-scale=1">
100103
<title>Bootstrap w/ Vite</title>
104+
<script type="module" src="./js/main.js"></script>
101105
</head>
102106
<body>
103107
<div class="container py-4 px-3 mx-auto">
104108
<h1>Hello, Bootstrap and Vite!</h1>
105109
<button class="btn btn-primary">Primary button</button>
106110
</div>
107-
<script type="module" src="./js/main.js"></script>
108111
</body>
109112
</html>
110113
```
@@ -136,36 +139,16 @@ In the next and final section to this guide, we’ll import all of Bootstrap’s
136139

137140
## Import Bootstrap
138141

139-
1. **Set up Bootstrap's Sass import in `vite.config.js`.** Your configuration file is now complete and should match the snippet below. The only new part here is the `resolve` section—we use this to add an alias to our source files inside `node_modules` to keep imports as simple as possible.
140-
141-
<!-- eslint-skip -->
142-
```js
143-
const path = require('path')
144-
145-
export default {
146-
root: path.resolve(__dirname, 'src'),
147-
resolve: {
148-
alias: {
149-
'~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
150-
}
151-
},
152-
server: {
153-
port: 8080,
154-
hot: true
155-
}
156-
}
157-
```
158-
159-
2. **Now, let's import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
142+
1. **Import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
160143

161144
```scss
162145
// Import all of Bootstrap's CSS
163-
@import "~bootstrap/scss/bootstrap";
146+
@import "bootstrap/scss/bootstrap";
164147
```
165148

166149
*You can also import our stylesheets individually if you want. [Read our Sass import docs]({{< docsref "/customize/sass#importing" >}}) for details.*
167150

168-
3. **Next we load the CSS and import Bootstrap's JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap's JS. Popper will be imported automatically through Bootstrap.
151+
2. **Next we load the CSS and import Bootstrap's JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap's JS. Popper will be imported automatically through Bootstrap.
169152

170153
<!-- eslint-skip -->
171154
```js
@@ -188,7 +171,7 @@ In the next and final section to this guide, we’ll import all of Bootstrap’s
188171

189172
*[Read our JavaScript docs]({{< docsref "/getting-started/javascript/" >}}) for more information on how to use Bootstrap's plugins.*
190173

191-
4. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this.
174+
3. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this.
192175

193176
<img class="img-fluid" src="/docs/{{< param docs_version >}}/assets/img/guides/vite-dev-server-bootstrap.png" alt="Vite dev server running with Bootstrap">
194177

‎site/content/docs/5.3/getting-started/webpack.md

+43-24
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ We're building a Webpack project with Bootstrap from scratch, so there are some
2424
npm init -y
2525
```
2626

27-
2. **Install Webpack.** Next we need to install our Webpack development dependencies: `webpack` for the core of Webpack, `webpack-cli` so we can run Webpack commands from the terminal, and `webpack-dev-server` so we can run a local development server. We use `--save-dev` to signal that these dependencies are only for development use and not for production.
27+
2. **Install Webpack.** Next we need to install our Webpack development dependencies: `webpack` for the core of Webpack, `webpack-cli` so we can run Webpack commands from the terminal, and `webpack-dev-server` so we can run a local development server. Additionally, we'll install `html-webpack-plugin` to be able to store our `index.html` in `src` directory instead of the default `dist` one. We use `--save-dev` to signal that these dependencies are only for development use and not for production.
2828

2929
```sh
30-
npm i --save-dev webpack webpack-cli webpack-dev-server
30+
npm i --save-dev webpack webpack-cli webpack-dev-server html-webpack-plugin
3131
```
3232

3333
3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Popper since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don't plan on using those components, you can omit Popper here.
@@ -49,21 +49,20 @@ Now that we have all the necessary dependencies installed, we can get to work cr
4949
We've already created the `my-project` folder and initialized npm. Now we'll also create our `src` and `dist` folders to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
5050

5151
```sh
52-
mkdir {dist,src,src/js,src/scss}
53-
touch dist/index.html src/js/main.js src/scss/styles.scss webpack.config.js
52+
mkdir {src,src/js,src/scss}
53+
touch src/index.html src/js/main.js src/scss/styles.scss webpack.config.js
5454
```
5555

5656
When you're done, your complete project should look like this:
5757

5858
```text
5959
my-project/
60-
├── dist/
61-
│ └── index.html
6260
├── src/
6361
│ ├── js/
6462
│ │ └── main.js
65-
│ └── scss/
66-
│ └── styles.scss
63+
│ ├── scss/
64+
│ │ └── styles.scss
65+
│ └── index.html
6766
├── package-lock.json
6867
├── package.json
6968
└── webpack.config.js
@@ -78,7 +77,10 @@ With dependencies installed and our project folder ready for us to start coding,
7877
1. **Open `webpack.config.js` in your editor.** Since it's blank, we'll need to add some boilerplate config to it so we can start our server. This part of the config tells Webpack where to look for our project's JavaScript, where to output the compiled code to (`dist`), and how the development server should behave (pulling from the `dist` folder with hot reload).
7978

8079
```js
80+
'use strict'
81+
8182
const path = require('path')
83+
const HtmlWebpackPlugin = require('html-webpack-plugin')
8284

8385
module.exports = {
8486
mode: 'development',
@@ -91,11 +93,14 @@ With dependencies installed and our project folder ready for us to start coding,
9193
static: path.resolve(__dirname, 'dist'),
9294
port: 8080,
9395
hot: true
94-
}
96+
},
97+
plugins: [
98+
new HtmlWebpackPlugin({ template: './src/index.html' })
99+
]
95100
}
96101
```
97102

98-
2. **Next we fill in our `dist/index.html`.** This is the HTML page Webpack will load in the browser to utilize the bundled CSS and JS we'll add to it in later steps. Before we can do that, we have to give it something to render and include the `output` JS from the previous step.
103+
2. **Next we fill in our `src/index.html`.** This is the HTML page Webpack will load in the browser to utilize the bundled CSS and JS we'll add to it in later steps. Before we can do that, we have to give it something to render and include the `output` JS from the previous step.
99104

100105
```html
101106
<!doctype html>
@@ -148,7 +153,11 @@ Importing Bootstrap into Webpack requires the loaders we installed in the first
148153
1. **Set up the loaders in `webpack.config.js`.** Your configuration file is now complete and should match the snippet below. The only new part here is the `module` section.
149154

150155
```js
156+
'use strict'
157+
151158
const path = require('path')
159+
const autoprefixer = require('autoprefixer')
160+
const HtmlWebpackPlugin = require('html-webpack-plugin')
152161

153162
module.exports = {
154163
mode: 'development',
@@ -162,6 +171,9 @@ Importing Bootstrap into Webpack requires the loaders we installed in the first
162171
port: 8080,
163172
hot: true
164173
},
174+
plugins: [
175+
new HtmlWebpackPlugin({ template: './src/index.html' })
176+
],
165177
module: {
166178
rules: [
167179
{
@@ -181,7 +193,7 @@ Importing Bootstrap into Webpack requires the loaders we installed in the first
181193
options: {
182194
postcssOptions: {
183195
plugins: () => [
184-
require('autoprefixer')
196+
autoprefixer
185197
]
186198
}
187199
}
@@ -203,7 +215,7 @@ Importing Bootstrap into Webpack requires the loaders we installed in the first
203215

204216
```scss
205217
// Import all of Bootstrap's CSS
206-
@import "~bootstrap/scss/bootstrap";
218+
@import "bootstrap/scss/bootstrap";
207219
```
208220

209221
*You can also import our stylesheets individually if you want. [Read our Sass import docs]({{< docsref "/customize/sass#importing" >}}) for details.*
@@ -256,20 +268,27 @@ npm install --save-dev mini-css-extract-plugin
256268
Then instantiate and use the plugin in the Webpack configuration:
257269

258270
```diff
259-
--- a/webpack/webpack.config.js
260-
+++ b/webpack/webpack.config.js
261-
@@ -1,8 +1,10 @@
262-
+const miniCssExtractPlugin = require('mini-css-extract-plugin')
271+
--- a/webpack.config.js
272+
+++ b/webpack.config.js
273+
@@ -3,6 +3,7 @@
263274
const path = require('path')
264-
275+
const autoprefixer = require('autoprefixer')
276+
const HtmlWebpackPlugin = require('html-webpack-plugin')
277+
+const miniCssExtractPlugin = require('mini-css-extract-plugin')
278+
265279
module.exports = {
266280
mode: 'development',
267-
entry: './src/js/main.js',
268-
+ plugins: [new miniCssExtractPlugin()],
269-
output: {
270-
filename: "main.js",
271-
path: path.resolve(__dirname, "dist"),
272-
@@ -18,8 +20,8 @@ module.exports = {
281+
@@ -17,7 +18,8 @@ module.exports = {
282+
hot: true
283+
},
284+
plugins: [
285+
- new HtmlWebpackPlugin({ template: './src/index.html' })
286+
+ new HtmlWebpackPlugin({ template: './src/index.html' }),
287+
+ new miniCssExtractPlugin()
288+
],
289+
module: {
290+
rules: [
291+
@@ -25,8 +27,8 @@ module.exports = {
273292
test: /\.(scss)$/,
274293
use: [
275294
{
@@ -305,7 +324,7 @@ Configure Webpack to extract inline SVG files like this:
305324
```diff
306325
--- a/webpack/webpack.config.js
307326
+++ b/webpack/webpack.config.js
308-
@@ -16,6 +16,14 @@ module.exports = {
327+
@@ -23,6 +23,14 @@ module.exports = {
309328
},
310329
module: {
311330
rules: [

0 commit comments

Comments
 (0)