Skip to content

Commit f019fae

Browse files
Document more ways to read package.json in ESM (#4572)
* Document more ways to read package.json in ESM * Fix linting Co-authored-by: Lukas Taegert-Atkinson <[email protected]> Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent b7e451c commit f019fae

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

.lintstagedrc.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = {
2-
'.ts': ['eslint --fix --cache'],
3-
'!(test/**/*).js': ['eslint --fix --cache'],
4-
'{test/*,test/*/*,test/*/samples/**/_config}.js': ['eslint --fix --cache']
2+
'*.{ts,js}': ['eslint --fix --cache'],
3+
'*.md': ['prettier --write'],
54
};

docs/01-command-line-reference.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,27 @@ On the other hand if you are using at least Node 13 and have `"type": "module"`
288288
There are some potential gotchas when using `.mjs` on Node 13+:
289289

290290
- You will only get a default export from CommonJS plugins
291-
- You may not be able to import JSON files such as your `package.json file`. There are two ways to go around this:
291+
- You may not be able to import JSON files such as your `package.json file`. There are four ways to go around this:
292+
293+
- read and parse the JSON file yourself via
294+
295+
```
296+
// rollup.config.mjs
297+
import { readFileSync } from 'fs';
298+
299+
const packageJson = JSON.parse(readFileSync('./package.json'));
300+
...
301+
```
302+
303+
- use `createRequire` via
304+
305+
```
306+
// rollup.config.mjs
307+
import { createRequire } from 'module';
308+
const require = createRequire(import.meta.url);
309+
const packageJson = require('./package.json');
310+
...
311+
```
292312
293313
- run Rollup CLI via
294314

0 commit comments

Comments
 (0)