Skip to content

Commit 12cffdb

Browse files
committed
Improve CHANGELOG, enhance missing PostCSS config error message with examples for ESM and CJS formats
1 parent c13478c commit 12cffdb

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

CHANGELOG.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,51 @@ can adopt modern async APIs from the ecosystem without hacks**.
3939
Note: `Encore.getWebpackConfig()` is now **async** and returns a `Promise`. Use `await` at the
4040
top level of your webpack config (webpack supports async config files natively).
4141

42+
* If you prefer not to add `"type": "module"` to your project, you can rename your webpack config
43+
file to `webpack.config.mjs` instead. Webpack detects the `.mjs` extension and treats it as ESM
44+
automatically — no other changes needed.
45+
46+
* Replace `__dirname` and `__filename` with their ESM equivalents in your webpack config:
47+
```js
48+
// Before (CJS)
49+
path.resolve(__dirname, 'src/utilities/')
50+
config: [__filename]
51+
52+
// After (ESM)
53+
path.resolve(import.meta.dirname, 'src/utilities/')
54+
config: [import.meta.filename]
55+
```
56+
57+
* Replace `require()` calls in your webpack config with `import` statements:
58+
```js
59+
// Before (CJS)
60+
const path = require('path');
61+
62+
// After (ESM)
63+
import path from 'path';
64+
```
65+
If you need to require a CJS-only package, use `createRequire`:
66+
```js
67+
import { createRequire } from 'module';
68+
const require = createRequire(import.meta.url);
69+
const somePackage = require('cjs-only-package');
70+
```
71+
72+
* Config files that use CJS syntax (`module.exports`, `require()`) must be renamed to `.cjs` if
73+
your project has `"type": "module"` in its `package.json`. This affects files such as:
74+
- `postcss.config.js``postcss.config.cjs`
75+
- `babel.config.js``babel.config.cjs`
76+
77+
Alternatively, rewrite them as ESM:
78+
```js
79+
// postcss.config.js (ESM)
80+
export default {
81+
plugins: {
82+
autoprefixer: {},
83+
},
84+
};
85+
```
86+
4287
* The package `"exports"` field is now restrictive: only `"@symfony/webpack-encore"` and
4388
`"@symfony/webpack-encore/lib/plugins/plugin-priorities.js"` are exposed as public entry points.
4489
If you were importing other internal modules directly, those imports will no longer work.

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ class Encore {
377377
*
378378
* ```
379379
* Encore.addAliases({
380-
* Utilities: path.resolve(__dirname, 'src/utilities/'),
381-
* Templates: path.resolve(__dirname, 'src/templates/')
380+
* Utilities: path.resolve(import.meta.dirname, 'src/utilities/'),
381+
* Templates: path.resolve(import.meta.dirname, 'src/templates/')
382382
* })
383383
* ```
384384
*
@@ -1052,12 +1052,12 @@ class Encore {
10521052
* Encore.enableBuildCache({
10531053
* // object of "buildDependencies"
10541054
* // https://webpack.js.org/configuration/other-options/#cachebuilddependencies
1055-
* // __filename means that changes to webpack.config.js should invalidate the cache
1056-
* config: [__filename],
1055+
* // import.meta.filename means that changes to webpack.config.js should invalidate the cache
1056+
* config: [import.meta.filename],
10571057
* });
10581058
*
10591059
* // also configure other options the Webpack "cache" key
1060-
* Encore.enableBuildCache({ config: [__filename] }, (cache) => {
1060+
* Encore.enableBuildCache({ config: [import.meta.filename] }, (cache) => {
10611061
* cache.version: `${process.env.GIT_REV}`;
10621062
*
10631063
* cache.name: `${env.target}`

lib/friendly-errors/formatters/missing-postcss-config.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,27 @@ function formatErrors(errors) {
2222
pc.red('Module build failed: Error: No PostCSS Config found')
2323
);
2424
messages.push('');
25-
messages.push(`${pc.bgGreen(pc.black('FIX'))} Create a ${pc.yellow('postcss.config.js')} file at the root of your project.`);
25+
messages.push(`${pc.bgGreen(pc.black('FIX'))} Create a PostCSS config file at the root of your project.`);
2626
messages.push('');
27-
messages.push('Here is an example to get you started!');
27+
messages.push('Here are two examples to get you started:');
28+
messages.push('');
29+
messages.push(pc.bold('Option 1: ESM') + ` (${pc.yellow('postcss.config.js')})`);
30+
messages.push(pc.yellow(`
31+
export default {
32+
plugins: {
33+
'autoprefixer': {},
34+
}
35+
}
36+
`));
37+
messages.push(pc.bold('Option 2: CJS') + ` (${pc.yellow('postcss.config.cjs')})`);
2838
messages.push(pc.yellow(`
29-
// postcss.config.js
3039
module.exports = {
3140
plugins: {
3241
'autoprefixer': {},
3342
}
3443
}
3544
`));
3645

37-
messages.push('');
38-
messages.push('');
39-
4046
return messages;
4147
}
4248

0 commit comments

Comments
 (0)