Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port doc changes from #4572 and #4583 to 3.0 #4592

Merged
merged 1 commit into from Jul 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 8 additions & 18 deletions docs/01-command-line-reference.md
Expand Up @@ -303,41 +303,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