-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[DOCS] webpack documentation concept and config section from CommonJS to ESM #7731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ By default its value is `./src/index.js`, but you can specify a different (or mu | |
| **webpack.config.js** | ||
|
|
||
| ```js | ||
| module.exports = { | ||
| export default { | ||
| entry: './path/to/my/entry/file.js', | ||
| }; | ||
| ``` | ||
|
|
@@ -70,12 +70,12 @@ You can configure this part of the process by specifying an `output` field in yo | |
| **webpack.config.js** | ||
|
|
||
| ```javascript | ||
| const path = require('path'); | ||
| import path from 'path'; | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| entry: './path/to/my/entry/file.js', | ||
| output: { | ||
| path: path.resolve(__dirname, 'dist'), | ||
| path: path.resolve(process.cwd(), 'dist'), | ||
|
||
| filename: 'my-first-webpack.bundle.js', | ||
| }, | ||
| }; | ||
|
|
@@ -99,9 +99,9 @@ At a high level, **loaders** have two properties in your webpack configuration: | |
| **webpack.config.js** | ||
|
|
||
| ```javascript | ||
| const path = require('path'); | ||
| import path from 'path'; | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| output: { | ||
| filename: 'my-first-webpack.bundle.js', | ||
| }, | ||
|
|
@@ -132,10 +132,10 @@ In order to use a plugin, you need to `require()` it and add it to the `plugins` | |
| **webpack.config.js** | ||
|
|
||
| ```javascript | ||
| const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
| const webpack = require('webpack'); //to access built-in plugins | ||
| import HtmlWebpackPlugin from 'html-webpack-plugin'; | ||
| import webpack from 'webpack'; //to access built-in plugins | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| module: { | ||
| rules: [{ test: /\.txt$/, use: 'raw-loader' }], | ||
| }, | ||
|
|
@@ -154,7 +154,7 @@ Using plugins in your webpack configuration is straightforward. However, there a | |
| By setting the `mode` parameter to either `development`, `production` or `none`, you can enable webpack's built-in optimizations that correspond to each environment. The default value is `production`. | ||
|
|
||
| ```javascript | ||
| module.exports = { | ||
| export default { | ||
| mode: 'production', | ||
| }; | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,15 +26,14 @@ A webpack **plugin** is a JavaScript object that has an [`apply`](https://develo | |
| ```javascript | ||
| const pluginName = 'ConsoleLogOnBuildWebpackPlugin'; | ||
|
|
||
| class ConsoleLogOnBuildWebpackPlugin { | ||
| export deafult class ConsoleLogOnBuildWebpackPlugin { | ||
| apply(compiler) { | ||
| compiler.hooks.run.tap(pluginName, (compilation) => { | ||
| console.log('The webpack build process is starting!'); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = ConsoleLogOnBuildWebpackPlugin; | ||
| ``` | ||
|
|
||
| It is recommended that the first parameter of the tap method of the compiler hook should be a camelized version of the plugin name. It is advisable to use a constant for this so it can be reused in all hooks. | ||
|
|
@@ -50,11 +49,11 @@ Depending on how you are using webpack, there are multiple ways to use plugins. | |
| **webpack.config.js** | ||
|
|
||
| ```javascript | ||
| const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
| const webpack = require('webpack'); //to access built-in plugins | ||
| const path = require('path'); | ||
| import HtmlWebpackPlugin from 'html-webpack-plugin'); | ||
| import webpack from 'webpack'; //to access built-in plugins | ||
| import path from 'path'; | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| entry: './path/to/my/entry/file.js', | ||
| output: { | ||
| filename: 'my-first-webpack.bundle.js', | ||
|
|
@@ -84,14 +83,14 @@ When using the Node API, you can also pass plugins via the `plugins` property in | |
| **some-node-script.js** | ||
|
|
||
| ```javascript | ||
| const webpack = require('webpack'); //to access webpack runtime | ||
| const configuration = require('./webpack.config.js'); | ||
| import webpack from 'webpack'; //to access webpack runtime | ||
| import configuration from './webpack.config.js'; | ||
|
|
||
| let compiler = webpack(configuration); | ||
| const compiler = webpack(configuration); | ||
|
|
||
| new webpack.ProgressPlugin().apply(compiler); | ||
|
|
||
| compiler.run(function (err, stats) { | ||
| compiler.run((err, stats) { | ||
|
||
| // ... | ||
| }); | ||
| ``` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont use process.cwd, use this
https://stackoverflow.com/a/50052194