Skip to content

Build a simple yet powerful webpack boilerplate, which I can later use as a starting point in your projects

License

Notifications You must be signed in to change notification settings

fickryiman/setup-project-with-webpack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

setup-project-with-webpack

Build a simple yet powerful webpack boilerplate, which I can later use as a starting point in your projects

Instructions:

Initialize a new project and install webpack

Add HTML

  • You already know that all the distribution files will be placed in /dist directory. You also know that you should not create files manually in the /dist folder, as there's a risk they will be overwritten. Therefore, install the HtmlWebpackPlugin to automatically create the index.html file in the /dist directory.
  • Follow the instructions from the setting up HtmlWebpackPlugin guide (https://webpack.js.org/guides/output-management/#setting-up-htmlwebpackplugin). Be extra careful when updating the module.exports object in your webpack.config.js file, to not to make any nesting mistakes.
  • Now delete all the files from the /dist directory and run: npm run build
  • Check your /dist folder. If it contains a new index.html file, it means you were successful.

HTML template

  • If you plan to write some HTML in your project, it's easiest to do it with a template. Create a /src/index.html in which you can write your markup. Add some basic page markup, like:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Wbpack Exercise</title>
</head>
<body>
    <h1>Hello webpack!</h1>
</body>
</html>
  • Then modify webpack.config.js to point HtmlWebpackPlugin towards your template file:
plugins: [
  new HtmlWebpackPlugin({
-   title: 'Output Management',
+   template: './src/index.html',
  }),
],
  • You could remove the title property as well (as shown above), because you have set the page title in your /src/index.html.
  • Run npm run build to update the /dist/index.html. View the /dist/index.html file in a code editor and notice how webpack inserted a <script> tag with correct path and minified the HTML for better performance.

Add CSS

The next step in building your webpack boilerplate is to add some style to it. Follow the steps in loading CSS guide (https://webpack.js.org/guides/asset-management/#loading-css).

  • In your style.css file add a generic rule, like: body { background-color: bisque; }
  • Next, execute npm run build and check if the HTML body style has changed.

Setup local dev server

Finally, it's time to improve your developer experience. When working on the project you will not want to run the build command from the terminal every time you make a change in the code. Therefore go ahead and install a webpack dev server, which will watch your source files, generate compiled distribution files and even refresh the browser every time you save changes in the source code.

About

Build a simple yet powerful webpack boilerplate, which I can later use as a starting point in your projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published