From f019faedd994a8c342119fda9dd85d7cfbe33267 Mon Sep 17 00:00:00 2001 From: Bertrand Guay-Paquet Date: Fri, 15 Jul 2022 09:38:45 +0200 Subject: [PATCH] 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 Co-authored-by: Lukas Taegert-Atkinson --- .lintstagedrc.js | 5 ++--- docs/01-command-line-reference.md | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.lintstagedrc.js b/.lintstagedrc.js index c9c66585f1a..c2d42142695 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,5 +1,4 @@ module.exports = { - '.ts': ['eslint --fix --cache'], - '!(test/**/*).js': ['eslint --fix --cache'], - '{test/*,test/*/*,test/*/samples/**/_config}.js': ['eslint --fix --cache'] + '*.{ts,js}': ['eslint --fix --cache'], + '*.md': ['prettier --write'], }; diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 60dd8db5182..601389c26d7 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -288,7 +288,27 @@ On the other hand if you are using at least Node 13 and have `"type": "module"` There are some potential gotchas when using `.mjs` on Node 13+: - You will only get a default export from CommonJS plugins -- You may not be able to import JSON files such as your `package.json file`. There are two ways to go around this: +- You may not be able to import JSON files such as your `package.json file`. There are four ways to go around this: + + - read and parse the JSON file yourself via + + ``` + // rollup.config.mjs + import { readFileSync } from 'fs'; + + const packageJson = JSON.parse(readFileSync('./package.json')); + ... + ``` + + - use `createRequire` via + + ``` + // rollup.config.mjs + import { createRequire } from 'module'; + const require = createRequire(import.meta.url); + const packageJson = require('./package.json'); + ... + ``` - run Rollup CLI via