parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.27k stars 2.26k forks source link

import.meta.url causing build errors with --log-level verbose #9784

Open Joolyan opened 3 weeks ago

Joolyan commented 3 weeks ago

🐛 bug report

import.meta.url causes verbose logging to error with "@parcel/transformer-js: Conditional or non-top-level require() call."

🎛 Configuration

package.json { "name": "my-project", "source": "src/test.html", "browserslist": "> 0.5%, last 2 versions, not dead", "scripts": { "test": "parcel watch --no-hmr --log-level verbose" }, "devDependencies": { "parcel": "latest" } }

🤔 Expected Behavior

Using the recommended import.meta.url method to reference files in JavaScript shouldn't produce errors.

😯 Current Behavior

@parcel/transformer-js: Conditional or non-top-level require() call. This causes the resolved module and all dependencies to be wrapped.

/Users/Julian/Sites/studio/src/js/test.js:1:1

1 | const lottieUrl = new URL("../images/colt.png", import.meta.url); | ^ 2 | console.log(lottieUrl.href); 3 |

📝 Learn more: https://parceljs.org/features/scope-hoisting/#avoid-conditional-require()

✨ Built in 985ms

🔦 Context

The issue was discovered whilst using the Parcel Logger to debug a Parcel Packager script.

💻 Code Sample

<!doctype html>

My First Parcel App

Hello, World!

// test.js const lottieUrl = new URL("../images/colt.png", import.meta.url); console.log(lottieUrl.href);

/ test.css / h1 { color: hotpink; font-family: cursive; }

🌍 Your Environment

Software Version
Parcel 2.12.0
Node 20.14.0
npm 10.7.0
macOS 13.6.7 Intel
devongovett commented 2 weeks ago

It's not an error, just a warning.

Joolyan commented 1 week ago

Okay. Thanks for answering.