Importing HTML, CSS and Assets
Now we will demonstrate how to import separate HTML and CSS files. Both of them will include assets like images or import further stylesheet files which will be extracted and handled separately.
As we need some example assets we will copy for the sake of simplicity the default favicon.png
from the static
directory into a subdirectory src/images
:
First we create an HTML structure for our container. Instead of creating each element by code like
in the chapters before, we will outsource the HTML structure in an own file. This file will be
placed in the src
folder to include its content as variable in our library.
In this HTML structure we will point to:
- An image which we have copied in the
src/images
folder. - A SVG graphics files which was taken from the installed package
bootstrap-icons
.
Let's create the HTML file:
src/amazing-button.html
- Note the
~
at the beginning. This means that the resource should be searched from the packages in thenode_modules
directory.
Now we create a stylesheet file. As in the JavaScript files we will use some really useful CSS proposals like nesting classes and variables. All these proposals will be transpiled into CSS that runs in mostly each relevant browser out there.
In this CSS we will point also to the image favicon.png
. Of course, SVG files can also be
referenced here.
src/amazing-button.css
Finally, we will install the Bootstrap package to import its stylesheets. First, the package must be installed:
Then we write a global style.css
in which we import all Bootstrap stylesheets:
style.css
Remarks:
- We import here the complete library. You could also import only parts of them. Read more about importing Bootstrap here
- We have to use the
@import url("...")
syntax instead of@import ""
like noted in their official documentation. - As you can see we can mix CSS and SCSS however we want. You do not need to use the file
extension
scss
. -
Instead of importing the Bootstrap SCSS we could inject the precompiled CSS of Bootstrap by extending the extrenals configuration with:
This style.css
we will simply import in our main index.js
entrypoint. This results in this
stylesheet being included in our CSS bundle:
src/index.js
To avoid of bundling any Bootstrap JavaScript libraries in our bundle we should exclude them (even if we do not use them in our walkthrough):
webpack.config.js
Now we are finished with the preparations.
We write now a new class that imports the HTML structure and insert it into the container element. The CSS file will be simply imported:
src/amazing-button-import.js
- Import HTML file as text.
- Import CSS file to get included in our CSS bundle.
- Use the HTML text to put into a DOM element.
Let's add another container in which we will start the Bootstrap based application:
src/index.js
When you run npm run start
you should see the running example in your browser.
When you run npm run build:dev
you can see the results of importing assets from HTML or CSS:
📁 dist
├─ walkthrough-env.<hash>.js
├─ walkthrough-env.<hash>.css
├─ index.html
├─ 📁 manifest
├─ 📁 vendor
└─ 📁 assets
-
Assets: Look into the subdirectory
assets
. You will find there the Imagefavicon.png
. Subfolders likeimages
was kept, thus the final target directory isassets/images
.Note also that a hash value has been added to the file name. Whenever the image is changed, this value changes and this guarantees that the browser always loads the most recent version instead of the one that may be stored in a cache.
But where is the included SVG file? It was not outsourced into an own external file, because it was small enough to be included into your JavaScript bundle. This means that the SVG file is included directly in the HTML code instead of loading it later.
-
CSS Bundle: You will also find a new CSS file. This is your CSS Bundle. In this file all stylesheets was extracted that you have imported. In addition, all stylesheets that were included via
@import
statements are also included.