Webpack build for dist (#2812)

Co-authored-by: Christian Oliff <christianoliff@pm.me>
This commit is contained in:
Rob Larsen
2023-03-22 15:00:56 -04:00
committed by GitHub
parent 48f92c41cd
commit 2154114f92
14 changed files with 192 additions and 41 deletions

9
dist/package.json vendored
View File

@@ -10,12 +10,15 @@
"author": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open --port 8080"
"start": "webpack serve --open --config webpack.config.dev.js",
"build": "webpack --config webpack.config.prod.js"
},
"devDependencies": {
"webpack": "^5.74.0",
"copy-webpack-plugin": "^11.0.0",
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
"webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
}
}

12
dist/webpack.common.js vendored Normal file
View File

@@ -0,0 +1,12 @@
const path = require('path');
module.exports = {
entry: {
app: './js/app.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
filename: './js/app.js',
},
};

16
dist/webpack.config.dev.js vendored Normal file
View File

@@ -0,0 +1,16 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
liveReload: true,
hot: true,
open: true,
static: ['./'],
},
});

View File

@@ -1,16 +0,0 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode : 'development',
entry : './js/app.js',
devServer: {
liveReload: true,
hot: true,
open: true,
static: ['./'],
},
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
})
]
};

28
dist/webpack.config.prod.js vendored Normal file
View File

@@ -0,0 +1,28 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = merge(common, {
mode: 'production',
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
}),
new CopyPlugin({
patterns: [
{ from: 'img', to: 'img' },
{ from: 'css', to: 'css' },
{ from: 'js/vendor', to: 'js/vendor' },
{ from: 'icon.svg', to: 'icon.svg'},
{ from: 'favicon.ico', to: 'favicon.ico'},
{ from: 'tile-wide.png', to: 'tile-wide.png'},
{ from: 'robots.txt', to: 'robots.txt'},
{ from: 'icon.png', to: 'icon.png'},
{ from: '404.html', to: '404.html'},
{ from: 'site.webmanifest', to: 'site.webmanifest'},
{ from: 'tile.png', to: 'tile.png'}
],
})
],
});

View File

@@ -49,7 +49,9 @@ A basic HTML5 Boilerplate site initially looks something like this:
├── site.webmanifest
├── tile.png
├── tile-wide.png
└── webpack.config.js
└── webpack.common.js
└── webpack.config.dev.js
└── webpack.config.prod.js
```
What follows is a general overview of each major part and how to use them.
@@ -106,3 +108,62 @@ Icon with your own.
If you want to use different Apple Touch Icons for different resolutions please
refer to the [according documentation](extend.md#apple-touch-icons).
### Webpack
The project contains a simple [webpack](https://webpack.js.org/) configuration.
To get started developing a site with a development server, run the following
commands from within the `/dist/` folder in the project's repo or within the
root folder of the dowloaded project files, the folder created by `npm install`
or the project folder created by running [create\-html5\-boilerplate](https://github.com/h5bp/create-html5-boilerplate)
```
npm install
npm run start
```
This will start a Webpack development server with hot reloading of edited files.
To package a site for production run
```
npm run build
```
This command will bundle up the site's JavaScript and copy over static assets to
the newly created `dist` folder.
There are three files:
#### webpack.common.js
Both the production and development scripts inherit from this common script.
#### webpack.config.dev.js
This development configuration defines the behavior of development server.
#### webpack.config.prod.js
This production configuration defines the behavior of the production build.
It copies the following files and folders to the dist folder:
* css
* img
* js/vendor
* 404.html
* favicon.ico
* icon.png
* icon.svg
* index.html
* robots.txt
* site.webmanifest
* tile.png
* tile-wide.png
`js/vendor` is copied over in order to allow you to use unprocessed JS files
(like Modernizr) in addition to the files bundled based on the project's entry
point `app.js.`

View File

@@ -134,6 +134,7 @@ gulp.task('modernizr', (done) => {
gulp.task('lint:js', () =>
gulp.src([
`${dirs.src}/js/*.js`,
`${dirs.src}/*.js`,
`${dirs.test}/*.mjs`
]).pipe(gulpEslint())
.pipe(gulpEslint.failOnError())

2
package-lock.json generated
View File

@@ -20496,4 +20496,4 @@
}
}
}
}
}

View File

@@ -10,12 +10,15 @@
"author": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open --port 8080"
"start": "webpack serve --open --config webpack.config.dev.js",
"build": "webpack --config webpack.config.prod.js"
},
"devDependencies": {
"webpack": "^5.74.0",
"copy-webpack-plugin": "^11.0.0",
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
"webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
}
}

12
src/webpack.common.js Normal file
View File

@@ -0,0 +1,12 @@
const path = require('path');
module.exports = {
entry: {
app: './js/app.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
filename: './js/app.js',
},
};

16
src/webpack.config.dev.js Normal file
View File

@@ -0,0 +1,16 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
liveReload: true,
hot: true,
open: true,
static: ['./'],
},
});

View File

@@ -1,16 +0,0 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode : 'development',
entry : './js/app.js',
devServer: {
liveReload: true,
hot: true,
open: true,
static: ['./'],
},
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
})
]
};

View File

@@ -0,0 +1,28 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = merge(common, {
mode: 'production',
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
}),
new CopyPlugin({
patterns: [
{ from: 'img', to: 'img' },
{ from: 'css', to: 'css' },
{ from: 'js/vendor', to: 'js/vendor' },
{ from: 'icon.svg', to: 'icon.svg'},
{ from: 'favicon.ico', to: 'favicon.ico'},
{ from: 'tile-wide.png', to: 'tile-wide.png'},
{ from: 'robots.txt', to: 'robots.txt'},
{ from: 'icon.png', to: 'icon.png'},
{ from: '404.html', to: '404.html'},
{ from: 'site.webmanifest', to: 'site.webmanifest'},
{ from: 'tile.png', to: 'tile.png'}
],
})
],
});

View File

@@ -18,8 +18,11 @@ const expectedFilesInDistDir = [
'.gitignore',
'404.html',
'package.json',
'webpack.config.js',
'webpack.common.js',
'webpack.config.dev.js',
'webpack.config.prod.js',
'css/', // for directories, a `/` character
// should be included at the end
'css/normalize.css',