Skip to content

Commit

Permalink
[v3.0] Port doc changes from #4572 and #4583 to 3.0 (#4592)
Browse files Browse the repository at this point in the history
  • Loading branch information
berniegp authored and lukastaegert committed Oct 11, 2022
1 parent 39d3fa7 commit 0360a1d
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions docs/01-command-line-reference.md
Expand Up @@ -305,41 +305,31 @@ It can be useful to import your package file to e.g. mark your dependencies as "
import pkg from './package.json' assert { type: 'json' };

export default {
input: 'src/main.js',
external: Object.keys(pkg.dependencies),
output: {
format: 'es',
dir: 'dist'
}
// Mark package dependencies as "external". Rest of configuration omitted.
external: Object.keys(pkg.dependencies)
};
```
- For older Node versions, you can use "createRequire"
- For older Node versions, you can use `createRequire`
```js
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const pkg = require('./package.json');

export default {
input: 'src/main.js',
external: Object.keys(pkg.dependencies),
output: {
format: 'es',
dir: 'dist'
}
};
// ...
```
- Or just directly read and parse the file from disk
```js
// rollup.config.mjs
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';

const pkgFileName = fileURLToPath(new URL('./package.json', import.meta.url));
const pkg = JSON.parse(readFileSync(pkgFileName));
// Use import.meta.url to make the path relative to the current source file instead of process.cwd()
// For more info: https://nodejs.org/docs/latest-v16.x/api/esm.html#importmetaurl
const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)));

// ...
```
Expand Down

0 comments on commit 0360a1d

Please sign in to comment.