skip to content

In a web build, why does `new URL('./decoder.wasm', import.meta.url)` keep working after bundling, while `new URL(fileName, import.meta.url)` breaks?

level: middleimportance: should knowfreq 9%

answer

  1. standard code, no build needed
  2. the module's own URL as base
  3. code moves into a chunk
  4. literal path is statically analysable
  5. variable: left as is

basics

~20 s

With a literal path, bundlers recognise the pattern, emit the file and rewrite the URL to the emitted copy. With a variable, the build cannot tell which file is meant, so the code ships unchanged and resolves against the chunk's new location.

solid answer

~40 s

`import.meta.url` is the URL of the current module, and the `URL` constructor resolves `'./decoder.wasm'` against it, so the expression is plain standard JavaScript that works unbundled: the module sits next to the file. Bundling moves the module's code into a chunk somewhere else, so the relative path would no longer point at anything. Bundlers therefore recognise the exact shape `new URL('<literal>', import.meta.url)`, treat the literal as a dependency, emit the file and rewrite the argument to the emitted URL. A variable such as `fileName` cannot be analysed at build time, so the build leaves the code alone: nothing is emitted for it, and at runtime the path resolves against the chunk's URL and 404s. Use literals, or a lookup object of literal `new URL()` calls, when the choice is made at runtime.

code

javascript · 13 lines
javascript
// recognised: a literal path next to import.meta.url
const decoderUrl = new URL('./decoder.wasm', import.meta.url)
// after the build, roughly: new URL('<emitted decoder URL>', import.meta.url)

// still recognised: every candidate is a literal
const decoders = {
  jp2: new URL('./decoders/jp2.wasm', import.meta.url),
  tiff: new URL('./decoders/tiff.wasm', import.meta.url),
}
const chosen = decoders[specimen.format]

// not recognised: shipped as written, resolves next to the chunk, 404s
const broken = new URL(specimen.decoderFile, import.meta.url)

go deeper

for a junior

Recall that import.meta.url is the current module's URL and that new URL() resolves a relative path against it. Keep the path a string literal.

for a middle

Explain why bundling moves code into chunks, how a bundler recognises the literal form, emits the file and rewrites the URL, and why a variable defeats that analysis.

for a senior

Design runtime choices so every candidate stays visible to the build, know where the pattern fails, such as server rendering, and recognise a 404 from an unanalysed path.

for a principal

Prefer standard, portable asset references in shared code so libraries work bundled or unbundled, and set rules for when runtime-chosen files belong in the public folder.

## The pattern is standard code Two standard features make up the pattern, and neither belongs to a build tool: - **`import.meta.url`** is the absolute URL of the module that contains the expression. It exists only in ES modules; in a classic script the same line is a syntax error. - The **`URL` constructor** resolves a relative reference against a base URL, exactly as a browser resolves a relative link against the page. So `new URL('./decoder.wasm', import.meta.url)` means "the file `decoder.wasm` next to this module". In an unbundled setting, such as a dev server that serves source modules one by one, it works with no help at all: the module is served from its source location, and the file sits beside it. ## What bundling changes A production build combines modules into **chunks** and writes them to an output folder, often under new names. Two things break the naive expression: 1. The module's code now runs from the **chunk's URL**, so `./decoder.wasm` resolves next to the chunk, not next to the original source file. 2. The file itself is **not in the output** unless the build knew to emit it. ## What a bundler does with a literal Several bundlers recognise the exact shape `new URL('<string literal>', import.meta.url)` during the build and treat it as an asset reference: 1. they resolve the literal relative to the source module, just as they would an import; 2. they emit the file (or inline it if it is small enough), usually under a hashed name; 3. they rewrite the first argument so the expression evaluates to the emitted file's URL. The resulting code still calls `new URL(...)` with `import.meta.url` or an equivalent base, so it remains standard JavaScript, and the same source works both bundled and unbundled. That portability is the main reason to prefer this pattern over a tool-specific way of asking for a file's URL. ## What a bundler cannot do with a variable | Source | Can the build analyse it? | Production result | |---|---|---| | `new URL('./decoder.wasm', import.meta.url)` | Yes, the path is a literal | File emitted, URL rewritten | | A lookup object of literal `new URL()` calls | Yes, each entry is a literal | Every listed file emitted | | A template literal with a fixed folder, e.g. `` `./decoders/${name}.wasm` `` | Sometimes: some bundlers match the files in that folder | Matching files emitted, if supported | | `new URL(fileName, import.meta.url)` | No | Code left unchanged; file never emitted | With a plain variable, the build would have to run your program to know the value. It does not, so it leaves the expression as written. At runtime the path resolves against the chunk's URL, where the file was never written, and the request **404s**. Depending on the browsers the build targets, an untransformed `import.meta` can also be a problem on its own in older engines. ## Fixing a runtime choice When the museum viewer chooses a decoder at runtime, keep every candidate visible to the build: - write a **lookup object** whose values are literal `new URL()` calls, and pick from it at runtime; - if a bundler supports it, use a **template literal with a fixed directory prefix**, and keep that directory small, since every matching file is emitted; - for files chosen from data the build can never see, put them in the **public pass-through folder** and build root-absolute URLs instead. ## Checking the build output A quick way to confirm the build understood a reference: 1. Search the emitted chunk for the expression: a recognised call now carries the emitted file's URL, while an unrecognised one still shows your variable. 2. List the output folder: the decoder should appear there, usually with a hash in its name. 3. In the browser's network panel, compare the requested URL with the files that actually exist in the output. ## Where the pattern stops - **Server-side rendering.** On a server, `import.meta.url` is usually a file path on disk, and the server cannot know the client's host, so the pattern is unreliable in server code. - **Workers.** Passing such a URL straight into a worker constructor is a separate, worker-specific pattern with its own rules. - **WebAssembly.** The pattern only gives you the decoder's URL. Fetching and instantiating it is the WebAssembly interop subject; some bundlers can also import a `.wasm` file directly as a module, following the WebAssembly ES-module integration proposal.

  • Why prefer this pattern over a tool-specific way of importing a file's URL?
    It is standard JavaScript: `import.meta.url` and the `URL` constructor work in any ES module, so the same code runs unbundled in a browser and bundled by any tool that recognises the shape. A tool-specific import form ties the code to one build setup and fails wherever that tool is not in the chain.
  • Why is the pattern unreliable in code that also runs during server-side rendering?
    On the server, `import.meta.url` is usually a file path on disk rather than the URL the client loads, and the server build cannot know the client's host ahead of time. The resolved URL is therefore meaningless to the browser, so asset URLs for server-rendered markup need another route.

  • `new URL(path, import.meta.url)` is a bundler API that only works after a build.
  • Bundlers evaluate any expression passed to `new URL()` at build time.
  • `import.meta.url` is the URL of the HTML page that loaded the script.
  • After bundling, `import.meta.url` still points at the original source module.
  • The pattern resolves to the same client URL during server-side rendering.