More progress in webpack guides

This commit is contained in:
2021-04-08 09:05:11 -05:00
parent cf8885ed14
commit dcf99e5409
6 changed files with 3816 additions and 14 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,14 +5,21 @@
"private": true, "private": true,
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch",
"start": "webpack serve --open",
"server": "node server.js",
"build": "webpack" "build": "webpack"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"express": "^4.17.1",
"html-webpack-plugin": "^5.3.1",
"webpack": "^5.31.0", "webpack": "^5.31.0",
"webpack-cli": "^4.6.0" "webpack-cli": "^4.6.0",
"webpack-dev-middleware": "^4.1.0",
"webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "dependencies": {
"lodash": "^4.17.21" "lodash": "^4.17.21"

20
frontend/server.js Normal file
View File

@@ -0,0 +1,20 @@
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const app = express();
const config = require('./webpack.config.js');
const compiler = webpack(config);
// Tell express to use the webpack-dev-middleware and use the webpack.config.js
// configuration file as a base.
app.use(
webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
})
);
// Serve the files on port 3000.
app.listen(3000, function () {
console.log('Example app listening on port 3000!\n');
});

View File

@@ -1,18 +1,12 @@
import _ from "lodash"; import _ from "lodash";
import "./style.css"; import Print from "./print";
import Icon from './ship.png';
function component() { function component() {
const element = document.createElement("div"); const element = document.createElement("div");
// Lodash, currently included via a script, is required for this line to work // Lodash, now imported by this script
element.innerHTML = _.join(["Hello", "webpack"], " "); element.innerHTML = _.join(["Hello", "webpack"], " ");
element.classList.add("hello"); element.onclick = Print.bind(null, "Hello webpack!");
const myIcon = new Image();
myIcon.src = Icon;
element.appendChild(myIcon);
return element; return element;
} }

3
frontend/src/print.js Normal file
View File

@@ -0,0 +1,3 @@
export default function print(text) {
console.log(text);
}

View File

@@ -1,9 +1,35 @@
const path = require("path"); const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = { module.exports = {
entry: "./src/index.js", mode: 'development',
entry: './src/index.js',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
},
plugins: [
new HtmlWebpackPlugin({
title: 'Caching',
})
],
output: { output: {
filename: "bundle.js", filename: '[name].[contenthash].js',
path: path.resolve(__dirname, "dist"), path: path.resolve(__dirname, 'dist'),
clean: true,
publicPath: '/',
},
optimization: {
moduleIds: 'deterministic',
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
}, },
}; };