webpack/examples/lazy-compilation
Tobias Koppers 1a06a4517b update lazy-compilation example for webpack-dev-server@4 2021-08-19 11:00:31 +02:00
..
public update lazy-compilation example for webpack-dev-server@4 2021-08-19 11:00:31 +02:00
README.md update lazy-compilation example for webpack-dev-server@4 2021-08-19 11:00:31 +02:00
all.js add experimental lazy compilation 2021-01-21 17:39:56 +01:00
build.js add experimental lazy compilation 2021-01-21 17:39:56 +01:00
example.js update lazy-compilation example for webpack-dev-server@4 2021-08-19 11:00:31 +02:00
more.js add experimental lazy compilation 2021-01-21 17:39:56 +01:00
template.md add experimental lazy compilation 2021-01-21 17:39:56 +01:00
webpack.config.js update lazy-compilation example for webpack-dev-server@4 2021-08-19 11:00:31 +02:00

README.md

To run this example you need to install webpack-dev-server and run webpack serve.

example.js

const libraries = {
	react: () => import("react"),
	acorn: () => import("acorn"),
	"core-js": () => import("core-js"),
	lodash: () => import("lodash"),
	xxhashjs: () => import("xxhashjs"),
	"all of them": () => import("./all")
};

document.body.style = "font-size: 16pt;";
const pre = document.createElement("pre");
pre.style = "height: 200px; overflow-y: auto";
pre.innerText =
	"Click on a button to load the library with import(). The first click triggers a lazy compilation of the module.";
for (const key of Object.keys(libraries)) {
	const button = document.createElement("button");
	const loadFn = libraries[key];
	button.innerText = key;
	button.onclick = async () => {
		pre.innerText = "Loading " + key + "...";
		const result = await loadFn();
		pre.innerText = `${key} = {\n  ${Object.keys(result).join(",\n  ")}\n}`;
	};
	document.body.appendChild(button);
}
const button = document.createElement("button");
button.innerText = "Load more...";
button.onclick = async () => {
	pre.innerText = "Loading more...";
	await import("./more");
	pre.innerText = "More libraries available.";
};
document.body.appendChild(button);
document.body.appendChild(pre);

webpack.config.js

const { HotModuleReplacementPlugin } = require("../../");

module.exports = {
	mode: "development",
	entry: {
		main: "./example.js"
	},
	cache: {
		type: "filesystem",
		idleTimeout: 5000
	},
	experiments: {
		lazyCompilation: true
	},
	devServer: {
		hot: true,
		devMiddleware: {
			publicPath: "/dist/"
		}
	},
	plugins: [new HotModuleReplacementPlugin()]
};