Skip to content

Commit 2e16249

Browse files
authored
docs(guide): convert asset management webpack.config.js to esm (#7781)
1 parent 7ef1d45 commit 2e16249

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

src/content/guides/asset-management.mdx

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ contributors:
1212
- wizardofhogwarts
1313
- astonizer
1414
- snitin315
15+
- Brennvo
1516
---
1617

1718
If you've been following the guides from the start, you will now have a small project that shows "Hello webpack". Now let's try to incorporate some other assets, like images, to see how they can be handled.
@@ -44,9 +45,13 @@ Let's make a minor change to our project before we get started:
4445
**webpack.config.js**
4546

4647
```diff
47-
const path = require('path');
48+
import path from 'node:path';
49+
import { fileURLToPath } from 'node:url';
4850

49-
module.exports = {
51+
const __filename = fileURLToPath(import.meta.url);
52+
const __dirname = path.dirname(__filename);
53+
54+
export default {
5055
entry: './src/index.js',
5156
output: {
5257
- filename: 'main.js',
@@ -67,9 +72,13 @@ npm install --save-dev style-loader css-loader
6772
**webpack.config.js**
6873

6974
```diff
70-
const path = require('path');
75+
import path from 'node:path';
76+
import { fileURLToPath } from 'node:url';
77+
78+
const __filename = fileURLToPath(import.meta.url);
79+
const __dirname = path.dirname(__filename);
7180

72-
module.exports = {
81+
export default {
7382
entry: './src/index.js',
7483
output: {
7584
filename: 'bundle.js',
@@ -171,9 +180,13 @@ So now we're pulling in our CSS, but what about our images like backgrounds and
171180
**webpack.config.js**
172181

173182
```diff
174-
const path = require('path');
183+
import path from 'node:path';
184+
import { fileURLToPath } from 'node:url';
175185

176-
module.exports = {
186+
const __filename = fileURLToPath(import.meta.url);
187+
const __dirname = path.dirname(__filename);
188+
189+
export default {
177190
entry: './src/index.js',
178191
output: {
179192
filename: 'bundle.js',
@@ -284,9 +297,13 @@ So what about other assets like fonts? The Asset Modules will take any file you
284297
**webpack.config.js**
285298

286299
```diff
287-
const path = require('path');
300+
import path from 'node:path';
301+
import { fileURLToPath } from 'node:url';
302+
303+
const __filename = fileURLToPath(import.meta.url);
304+
const __dirname = path.dirname(__filename);
288305

289-
module.exports = {
306+
export default {
290307
entry: './src/index.js',
291308
output: {
292309
filename: 'bundle.js',
@@ -395,9 +412,13 @@ npm install --save-dev csv-loader xml-loader
395412
**webpack.config.js**
396413

397414
```diff
398-
const path = require('path');
415+
import path from 'node:path';
416+
import { fileURLToPath } from 'node:url';
399417

400-
module.exports = {
418+
const __filename = fileURLToPath(import.meta.url);
419+
const __dirname = path.dirname(__filename);
420+
421+
export default {
401422
entry: './src/index.js',
402423
output: {
403424
filename: 'bundle.js',
@@ -581,12 +602,16 @@ And configure them in your webpack configuration:
581602
**webpack.config.js**
582603

583604
```diff
584-
const path = require('path');
585-
+const toml = require('toml');
586-
+const yaml = require('yamljs');
587-
+const json5 = require('json5');
605+
import path from 'node:path';
606+
import { fileURLToPath } from 'node:url';
607+
+import toml from 'toml';
608+
+import yaml from 'yamljs';
609+
+import json5 from 'json5';
610+
611+
const __filename = fileURLToPath(import.meta.url);
612+
const __dirname = path.dirname(__filename);
588613

589-
module.exports = {
614+
export default {
590615
entry: './src/index.js',
591616
output: {
592617
filename: 'bundle.js',
@@ -734,12 +759,16 @@ For the next guides we won't be using all the different assets we've used in thi
734759
**webpack.config.js**
735760

736761
```diff
737-
const path = require('path');
738-
-const toml = require('toml');
739-
-const yaml = require('yamljs');
740-
-const json5 = require('json5');
762+
import path from 'node:path';
763+
import { fileURLToPath } from 'node:url';
764+
-import toml from 'toml';
765+
-import yaml from 'yamljs';
766+
-import json5 from 'json5';
767+
768+
const __filename = fileURLToPath(import.meta.url);
769+
const __dirname = path.dirname(__filename);
741770

742-
module.exports = {
771+
export default {
743772
entry: './src/index.js',
744773
output: {
745774
filename: 'bundle.js',

0 commit comments

Comments
 (0)