Commit e72d34d
authored
fix(cjs-build): enable evaluating import.meta in cjs build (#1236)
## Purpose
A while ago, a [PR](#897) was merged
to disable `import.meta` evaluation altogether to support new versions
of Webpack. This sometimes causes issues in CJS projects.
In the comments, there were some
[discussions](#897 (comment))
about whether we should have a conditional and keep the previous
behavior for CJS builds. This PR addresses exactly this by translating
`import.meta.url` to a CJS compatible statement which results to the
same output.
I have created a [comparative
build](https://github.com/meienberger/ncc/pull/1/files) between two
projects using the same code and a dependency (@sentry/node) that uses
`import.meta.url`. You can see the difference in the output `index.js`
Before the change:
```javascript
const importMetaUrl = typeof import.meta.url !== 'undefined' ? import.meta.url : undefined;
```
After:
```javascript
const importMetaUrl = typeof require("url").pathToFileURL(__filename).href !== 'undefined' ? require("url").pathToFileURL(__filename).href : undefined;
```
Closes #10191 parent 186af2b commit e72d34d
4 files changed
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
255 | 261 | | |
256 | 262 | | |
257 | 263 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
0 commit comments