From 334da0a57e74911e452545d30cfc409f9f4b1cde Mon Sep 17 00:00:00 2001 From: chenxsan Date: Sun, 8 Nov 2020 10:36:36 +0800 Subject: [PATCH] docs(guides): update getting-started --- src/content/guides/getting-started.md | 188 +++++++++++++------------- 1 file changed, 95 insertions(+), 93 deletions(-) diff --git a/src/content/guides/getting-started.md b/src/content/guides/getting-started.md index 0164095ed46a..d3f518db2d2d 100644 --- a/src/content/guides/getting-started.md +++ b/src/content/guides/getting-started.md @@ -25,9 +25,9 @@ contributors: - anshumanv --- -webpack is used to compile JavaScript modules. Once [installed](/guides/installation), you can interface with webpack either from its [CLI](/api/cli) or [API](/api/node). If you're still new to webpack, please read through the [core concepts](/concepts) and [this comparison](/comparison) to learn why you might use it over the other tools that are out in the community. +webpack is used to compile JavaScript modules. Once [installed](/guides/installation), you can interact with webpack either from its [CLI](/api/cli) or [API](/api/node). If you're still new to webpack, please read through the [core concepts](/concepts) and [this comparison](/comparison) to learn why you might use it over the other tools that are out in the community. -W> Since webpack v5.0.0-beta.1 the minimum Node.js version to run webpack is 10.13.0 (LTS) +W> The minimum Node.js version to run webpack 5 is 10.13.0 (LTS) ## Basic Setup @@ -40,7 +40,13 @@ npm init -y npm install webpack webpack-cli --save-dev ``` -T> Throughout the Guides we will use `diff` blocks to show you what changes we're making to directories, files, and code. +Throughout the Guides we will use __`diff`__ blocks to show you what changes we're making to directories, files, and code. For instance: + +```diff ++ this is a new line you shall copy into your code +- and this is a line to be removed from your code + and this is a line not to touch. +``` Now we'll create the following directory structure, files and their contents: @@ -72,12 +78,12 @@ document.body.appendChild(component()); __index.html__ ``` html - + - + Getting Started - + @@ -92,24 +98,23 @@ T> If you want to learn more about the inner workings of `package.json`, then we __package.json__ ``` diff - { - "name": "webpack-demo", - "version": "1.0.0", - "description": "", -+ "private": true, -- "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "devDependencies": { - "webpack": "^4.20.2", - "webpack-cli": "^3.1.2" - }, - "dependencies": {} - } + { + "name": "webpack-demo", + "version": "1.0.0", + "description": "", +- "main": "index.js", ++ "private": true, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "webpack": "^5.4.0", + "webpack-cli": "^4.2.0" + } + } ``` In this example, there are implicit dependencies between the ` +- - + - + ``` In this setup, `index.js` explicitly requires `lodash` to be present, and binds it as `_` (no global scope pollution). By stating what dependencies a module needs, webpack can use this information to build a dependency graph. It then uses the graph to generate an optimized bundle where scripts will be executed in the correct order. @@ -190,22 +195,19 @@ In this setup, `index.js` explicitly requires `lodash` to be present, and binds With that said, let's run `npx webpack`, which will take our script at `src/index.js` as the [entry point](/concepts/entry-points), and will generate `dist/main.js` as the [output](/concepts/output). The `npx` command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (`./node_modules/.bin/webpack`) of the webpack package we installed in the beginning: ``` bash -npx webpack - -... -Built at: 13/06/2018 11:52:07 - Asset Size Chunks Chunk Names -main.js 70.4 KiB 0 [emitted] main -... - -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ +$ npx webpack +[webpack-cli] Compilation finished +asset main.js 69.3 KiB [emitted] [minimized] (name: main) 1 related asset +runtime modules 1000 bytes 5 modules +cacheable modules 530 KiB + ./src/index.js 257 bytes [built] [code generated] + ./node_modules/lodash/lodash.js 530 KiB [built] [code generated] +webpack 5.4.0 compiled successfully in 3619 ms ``` T> Your output may vary a bit, but if the build is successful then you are good to go. Also, don't worry about the warning, we'll tackle that later. -Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: 'Hello webpack'. +Open `index.html` from the `dist` directory in your browser and, if everything went right, you should see the following text: `'Hello webpack'`. W> If you are getting a syntax error in the middle of minified JavaScript when opening `index.html` in the browser, set [`development mode`](/configuration/mode/#mode-development) and run `npx webpack` again. This is related to running `npx webpack` on latest Node.js (v12.5+) instead of [LTS version](https://nodejs.org/en/). @@ -214,7 +216,7 @@ W> If you are getting a syntax error in the middle of minified JavaScript when o The [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) statements have been standardized in [ES2015](https://babeljs.io/docs/en/learn/). They are supported in most of the browsers at this moment, however there are some browsers that don't recognize the new syntax. But don't worry, webpack does support them out of the box. -Behind the scenes, webpack actually "transpiles" the code so that older browsers can also run it. If you inspect `dist/main.js`, you might be able to see how webpack does this, it's quite ingenious! Besides `import` and `export`, webpack supports various other module syntaxes as well, see [Module API](/api/module-methods) for more information. +Behind the scenes, webpack actually "__transpiles__" the code so that older browsers can also run it. If you inspect `dist/main.js`, you might be able to see how webpack does this, it's quite ingenious! Besides `import` and `export`, webpack supports various other module syntaxes as well, see [Module API](/api/module-methods) for more information. Note that webpack will not alter any code other than `import` and `export` statements. If you are using other [ES2015 features](http://es6-features.org/), make sure to [use a transpiler](/loaders/#transpiling) such as [Babel](https://babeljs.io/) or [Bublé](https://buble.surge.sh/guide/) via webpack's [loader system](/concepts/loaders/). @@ -252,16 +254,14 @@ module.exports = { Now, let's run the build again but instead using our new configuration file: ``` bash -npx webpack --config webpack.config.js - -... - Asset Size Chunks Chunk Names -main.js 70.4 KiB 0 [emitted] main -... - -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ +$ npx webpack --config webpack.config.js +[webpack-cli] Compilation finished +asset main.js 69.3 KiB [compared for emit] [minimized] (name: main) 1 related asset +runtime modules 1000 bytes 5 modules +cacheable modules 530 KiB + ./src/index.js 257 bytes [built] [code generated] + ./node_modules/lodash/lodash.js 530 KiB [built] [code generated] +webpack 5.4.0 compiled successfully in 3516 ms ``` T> If a `webpack.config.js` is present, the `webpack` command picks it up by default. We use the `--config` option here only to show that you can pass a configuration of any name. This will be useful for more complex configurations that need to be split into multiple files. @@ -276,26 +276,27 @@ Given it's not particularly fun to run a local copy of webpack from the CLI, we __package.json__ ``` diff - { - "name": "webpack-demo", - "version": "1.0.0", - "description": "", - "scripts": { -- "test": "echo \"Error: no test specified\" && exit 1" -+ "test": "echo \"Error: no test specified\" && exit 1", -+ "build": "webpack" - }, - "keywords": [], - "author": "", - "license": "ISC", - "devDependencies": { - "webpack": "^4.20.2", - "webpack-cli": "^3.1.2" - }, - "dependencies": { - "lodash": "^4.17.5" - } - } +{ + "name": "webpack-demo", + "version": "1.0.0", + "description": "", + "private": true, + "scripts": { +- "test": "echo \"Error: no test specified\" && exit 1" ++ "test": "echo \"Error: no test specified\" && exit 1", ++ "build": "webpack" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "webpack": "^5.4.0", + "webpack-cli": "^4.2.0" + }, + "dependencies": { + "lodash": "^4.17.20" + } + } ``` Now the `npm run build` command can be used in place of the `npx` command we used earlier. Note that within `scripts` we can reference locally installed npm packages by name the same way we did with `npx`. This convention is the standard in most npm-based projects because it allows all contributors to use the same set of common scripts (each with flags like `--config` if necessary). @@ -303,19 +304,20 @@ Now the `npm run build` command can be used in place of the `npx` command we use Now run the following command and see if your script alias works: ``` bash -npm run build +$ npm run build -... - Asset Size Chunks Chunk Names -main.js 70.4 KiB 0 [emitted] main ... -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/. +[webpack-cli] Compilation finished +asset main.js 69.3 KiB [compared for emit] [minimized] (name: main) 1 related asset +runtime modules 1000 bytes 5 modules +cacheable modules 530 KiB + ./src/index.js 257 bytes [built] [code generated] + ./node_modules/lodash/lodash.js 530 KiB [built] [code generated] +webpack 5.4.0 compiled successfully in 3764 ms ``` -T> Custom parameters can be passed to webpack by adding two dashes between the `npm run build` command and your parameters, e.g. `npm run build -- --colors`. +T> Custom parameters can be passed to webpack by adding two dashes between the `npm run build` command and your parameters, e.g. `npm run build -- --color`. ## Conclusion @@ -336,7 +338,7 @@ webpack-demo |- /node_modules ``` -T> If you're using npm 5, you'll probably also see a `package-lock.json` file in your directory. +T> If you're using npm 5+, you'll probably also see a `package-lock.json` file in your directory. W> Do not compile untrusted code with webpack. It could lead to execution of malicious code on your computer, remote servers, or in the Web browsers of the end users of your application.