forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-esm-plugin.mjs
34 lines (30 loc) · 1.02 KB
/
make-esm-plugin.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import fs from 'node:fs';
/**
* Outputs a package.json file with {type: module} in the root of the output directory so that Node
* treats .js files as ESM.
*/
export function makePackageNodeEsm() {
return {
name: 'make-package-node-esm',
async generateBundle() {
// We need to keep the `sideEffects` value from the original package.json,
// as e.g. webpack seems to depend on this
// without this, tree shaking does not work as expected
const packageJSONPath = (await this.resolve('package.json')).id;
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));
const sideEffects = packageJSON.sideEffects;
// For module federation we need to keep the version of the package
const version = packageJSON.version;
const newPackageJSON = {
type: 'module',
version,
sideEffects,
};
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: JSON.stringify(newPackageJSON),
});
},
};
}