From 01f5a3e47a85d68bbe5cb76ca617aff0b43df532 Mon Sep 17 00:00:00 2001 From: avivkeller Date: Thu, 18 Jun 2026 17:37:44 -0600 Subject: [PATCH 1/2] chore(copying): copy files from webpack.js.org: guides --- .../guides/core-workflows/asset-management.md | 828 ++++++++++++++++++ pages/guides/core-workflows/asset-modules.md | 566 ++++++++++++ .../development/hot-module-replacement.md | 376 ++++++++ .../core-workflows/development/index.md | 422 +++++++++ .../core-workflows/development/vagrant.md | 107 +++ .../core-workflows/environment-variables.md | 46 + .../core-workflows/output-management.md | 225 +++++ pages/guides/core-workflows/production.md | 201 +++++ .../getting-started/concepts/configuration.md | 84 ++ .../concepts/dependency-graph.md | 17 + .../getting-started/concepts/entry-points.md | 223 +++++ .../concepts/hot-module-replacement.md | 64 ++ .../guides/getting-started/concepts/index.md | 162 ++++ .../getting-started/concepts/loaders.md | 122 +++ .../getting-started/concepts/manifest.md | 37 + .../concepts/module-federation.md | 373 ++++++++ .../concepts/module-resolution.md | 74 ++ .../getting-started/concepts/modules.md | 49 ++ .../guides/getting-started/concepts/output.md | 68 ++ .../getting-started/concepts/plugins.md | 90 ++ .../getting-started/concepts/targets.md | 79 ++ .../concepts/under-the-hood.md | 122 +++ .../getting-started/concepts/why-webpack.md | 47 + pages/guides/getting-started/index.md | 393 +++++++++ .../getting-started/installing-webpack.md | 69 ++ pages/guides/index.md | 3 + pages/guides/migration/index.md | 7 + pages/guides/migration/to-v3.md | 589 +++++++++++++ pages/guides/migration/to-v4.md | 147 ++++ pages/guides/migration/to-v5.md | 339 +++++++ pages/guides/modern-web/index.md | 189 ++++ pages/guides/modern-web/native-css.md | 250 ++++++ .../modern-web/progressive-web-application.md | 139 +++ pages/guides/modern-web/web-workers.md | 119 +++ .../dependency-management.md | 143 +++ .../ecma-script-modules.md | 210 +++++ .../entry-advanced.md | 71 ++ .../package-exports.md | 603 +++++++++++++ .../modules-and-dependencies/shimming.md | 461 ++++++++++ .../guides/optimization/build-performance.md | 249 ++++++ pages/guides/optimization/caching.md | 328 +++++++ .../optimization/code-splitting/index.md | 461 ++++++++++ .../code-splitting/lazy-loading.md | 237 +++++ pages/guides/optimization/tree-shaking.md | 576 ++++++++++++ pages/guides/site.json | 209 +++++ .../authoring-libraries.md | 318 +++++++ .../content-security-policy.md | 40 + .../tooling-and-integration/integrations.md | 116 +++ .../tooling-and-integration/typescript.md | 360 ++++++++ pages/site.mjs | 2 + 50 files changed, 11010 insertions(+) create mode 100644 pages/guides/core-workflows/asset-management.md create mode 100644 pages/guides/core-workflows/asset-modules.md create mode 100644 pages/guides/core-workflows/development/hot-module-replacement.md create mode 100644 pages/guides/core-workflows/development/index.md create mode 100644 pages/guides/core-workflows/development/vagrant.md create mode 100644 pages/guides/core-workflows/environment-variables.md create mode 100644 pages/guides/core-workflows/output-management.md create mode 100644 pages/guides/core-workflows/production.md create mode 100644 pages/guides/getting-started/concepts/configuration.md create mode 100644 pages/guides/getting-started/concepts/dependency-graph.md create mode 100644 pages/guides/getting-started/concepts/entry-points.md create mode 100644 pages/guides/getting-started/concepts/hot-module-replacement.md create mode 100644 pages/guides/getting-started/concepts/index.md create mode 100644 pages/guides/getting-started/concepts/loaders.md create mode 100644 pages/guides/getting-started/concepts/manifest.md create mode 100644 pages/guides/getting-started/concepts/module-federation.md create mode 100644 pages/guides/getting-started/concepts/module-resolution.md create mode 100644 pages/guides/getting-started/concepts/modules.md create mode 100644 pages/guides/getting-started/concepts/output.md create mode 100644 pages/guides/getting-started/concepts/plugins.md create mode 100644 pages/guides/getting-started/concepts/targets.md create mode 100644 pages/guides/getting-started/concepts/under-the-hood.md create mode 100644 pages/guides/getting-started/concepts/why-webpack.md create mode 100644 pages/guides/getting-started/index.md create mode 100644 pages/guides/getting-started/installing-webpack.md create mode 100644 pages/guides/index.md create mode 100644 pages/guides/migration/index.md create mode 100644 pages/guides/migration/to-v3.md create mode 100644 pages/guides/migration/to-v4.md create mode 100644 pages/guides/migration/to-v5.md create mode 100644 pages/guides/modern-web/index.md create mode 100644 pages/guides/modern-web/native-css.md create mode 100644 pages/guides/modern-web/progressive-web-application.md create mode 100644 pages/guides/modern-web/web-workers.md create mode 100644 pages/guides/modules-and-dependencies/dependency-management.md create mode 100644 pages/guides/modules-and-dependencies/ecma-script-modules.md create mode 100644 pages/guides/modules-and-dependencies/entry-advanced.md create mode 100644 pages/guides/modules-and-dependencies/package-exports.md create mode 100644 pages/guides/modules-and-dependencies/shimming.md create mode 100644 pages/guides/optimization/build-performance.md create mode 100644 pages/guides/optimization/caching.md create mode 100644 pages/guides/optimization/code-splitting/index.md create mode 100644 pages/guides/optimization/code-splitting/lazy-loading.md create mode 100644 pages/guides/optimization/tree-shaking.md create mode 100644 pages/guides/site.json create mode 100644 pages/guides/tooling-and-integration/authoring-libraries.md create mode 100644 pages/guides/tooling-and-integration/content-security-policy.md create mode 100644 pages/guides/tooling-and-integration/integrations.md create mode 100644 pages/guides/tooling-and-integration/typescript.md diff --git a/pages/guides/core-workflows/asset-management.md b/pages/guides/core-workflows/asset-management.md new file mode 100644 index 00000000..14f52c12 --- /dev/null +++ b/pages/guides/core-workflows/asset-management.md @@ -0,0 +1,828 @@ +--- +authors: skipjack,michael-ciniawsky,TheDutchCoder,sudarsangp,chenxsan,EugeneHlushko,AnayaDesign,wizardofhogwarts,astonizer,snitin315,Brennvo,mr-baraiya,avivkeller +--- + +# Asset Management + +If you've followed the guides from the beginning, you now have a small project that prints "Hello webpack". The next step is to bring in other kinds of assets, such as images, and see how webpack handles them. + +Before webpack, front-end developers relied on tools like [grunt](https://gruntjs.com/) and [gulp](https://gulpjs.com/) to process assets and copy them from a `/src` folder into a `/dist` or `/build` directory. JavaScript modules followed the same approach, but webpack takes it further: it **dynamically bundles** every dependency, building what's known as a [dependency graph](#TODO[/concepts/dependency-graph]). This is powerful because each module now _explicitly declares its dependencies_, which lets webpack skip anything that isn't actually used. + +One of webpack's nicest features is that you can _import almost any type of file_, not just JavaScript, as long as there's a loader or built-in [Asset Modules](#TODO[/guides/asset-modules/]) support for it. That means the benefits you get with JavaScript, like explicit dependencies, apply to everything you use to build a site or web app. We'll begin with CSS, since that setup is probably already familiar to you. + +## Setup + +First, make a small change to the project before we begin. + +```diff displayName="dist/index.html" + + + + +- Getting Started ++ Asset Management + + +- ++ + + +``` + +```diff displayName="webpack.config.js" + import path from 'node:path'; + import { fileURLToPath } from 'node:url'; + + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + + export default { + entry: './src/index.js', + output: { +- filename: 'main.js', ++ filename: 'bundle.js', + path: path.resolve(__dirname, 'dist'), + }, + }; +``` + +## Loading CSS + +To `import` a CSS file from within a JavaScript module, install the [style-loader](#TODO[/loaders/style-loader]) and [css-loader](#TODO[/loaders/css-loader]), then add them to your [`module` configuration](#TODO[/configuration/module]): + +```bash +npm install --save-dev style-loader css-loader +``` + +```diff displayName="webpack.config.js" + import path from 'node:path'; + import { fileURLToPath } from 'node:url'; + + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + + export default { + entry: './src/index.js', + output: { + filename: 'bundle.js', + path: path.resolve(__dirname, 'dist'), + }, ++ module: { ++ rules: [ ++ { ++ test: /\.css$/i, ++ use: ['style-loader', 'css-loader'], ++ }, ++ ], ++ }, + }; +``` + +Loaders can be chained together, with each loader in the chain transforming the resource it receives. The chain runs in reverse order, from right to left. + +For example, consider the following rule: + +```js +export default { + module: { + rules: [ + { + test: /\.scss$/i, + use: ['postcss-loader', 'sass-loader'], + }, + ], + }, +}; +``` + +Although `postcss-loader` is listed before `sass-loader` in the `use` array, webpack executes `sass-loader` first to compile Sass into CSS, then runs `postcss-loader` on the result. If this order isn't respected, webpack may throw errors. + +```diff displayName="project" + webpack-demo + ├── package.json + ├── package-lock.json + ├── webpack.config.js + ├── /dist + │ ├── bundle.js + │ └── index.html + ├── /src ++ │ ├── style.css + │ └── index.js + └── /node_modules +``` + +```css displayName="src/style.css" +.hello { + color: red; +} +``` + +```diff displayName="src/index.js" + import _ from 'lodash'; ++import './style.css'; + + function component() { + const element = document.createElement('div'); + + // Lodash, now imported by this script + element.innerHTML = _.join(['Hello', 'webpack'], ' '); ++ element.classList.add('hello'); + + return element; + } + + document.body.appendChild(component()); +``` + +Now run the build command: + +```bash +$ npm run build + +... +[webpack-cli] Compilation finished +asset bundle.js 72.6 KiB [emitted] [minimized] (name: main) 1 related asset +runtime modules 1000 bytes 5 modules +orphan modules 326 bytes [orphan] 1 module +cacheable modules 539 KiB + modules by path ./node_modules/ 538 KiB + ./node_modules/lodash/lodash.js 530 KiB [built] [code generated] + ./node_modules/style-loader/dist/runtime/injectStylesIntoStyleTag.js 6.67 KiB [built] [code generated] + ./node_modules/css-loader/dist/runtime/api.js 1.57 KiB [built] [code generated] + modules by path ./src/ 965 bytes + ./src/index.js + 1 modules 639 bytes [built] [code generated] + ./node_modules/css-loader/dist/cjs.js!./src/style.css 326 bytes [built] [code generated] +webpack 5.x.x compiled successfully in 2231 ms +``` + +Open `dist/index.html` in your browser again and you should see `Hello webpack` styled in red. To check what webpack did, inspect the page rather than viewing the page source; the source won't reflect the result because the `