Visitar URL original
`new URL('./source.js', import.meta.url)` produces a URL ending in `[object%20Module]` with a `javascript/auto` rule · Issue #23271 · webpack/webpack · GitHub
Skip to content

new URL('./source.js', import.meta.url) produces a URL ending in [object%20Module] with a javascript/auto rule #23271

Open

Description

@intellild

Have you used AI?

Yes

Which part of webpack is affected?

not sure, config (options, defaults, validation)

Bug Description

new URL('./source.js', import.meta.url) to get a link to the original JavaScript file. When a rule sets type: 'javascript/auto' for all .js files, webpack compiles successfully, skips emitting the source file, and generates a URL ending in [object%20Module].

Link to Minimal Reproduction and steps to reproduce

The reproduction repository includes the emitted files. Clone it and run pnpm install --frozen-lockfile, then pnpm run webpack and pnpm run webpack:baseline.

Expected Behavior

The URL should point to the emitted copy of source.js, as it does in the baseline.

Actual Behavior

With the rule, webpack emits only dist/webpack/with-rule/bundle.cjs. Running it prints file:///…/dist/webpack/with-rule/[object%20Module].

Without the rule, webpack also emits dist/webpack/baseline/source.js. The bundle prints file:///…/dist/webpack/baseline/source.js, and cmp confirms that the emitted file matches the original byte for byte.

Environment

System:
  OS: macOS 27.2
  CPU: (18) arm64 Apple M5 Pro
  Memory: 48.00 GB
Binaries:
  Node: 24.15.0
  Yarn: 1.22.22
  npm: 11.12.1
  pnpm: 10.34.4
Packages:
  webpack: 5.112.2
  webpack-cli: 7.3.1

Is this a regression?

None

Last Working Version

No response

Additional Context

No response

Activity

  1. added theissue type on Nov 12, 2026
  2. added
    area: parserJavaScript parsing and dependency analysis (lib/javascript, lib/dependencies)
    area: runtimeEmitted runtime and chunk loading across targets (lib/runtime, lib/web, lib/node, lib/esm)
    on Nov 12, 2026
  3. intellild commented on Nov 12, 2026

    @intellild
    ContributorAuthor

    Possible Solution

    Allow new URL(..., import.meta.url) to return the URL of an emitted JavaScript or CSS bundle. The CSS proposal would resolve stylesheet dependencies and emit a standalone CSS file. webpack/webpack#21209

    Making bundle URLs the default for JavaScript and CSS could be a breaking change. Existing callers can use these URLs to fetch the original source bytes, while a bundle may contain transformed code and inlined dependencies. An explicit opt-in could preserve the current asset behavior for existing callers.

  4. intellild commented on Nov 12, 2026

    @intellild
    ContributorAuthor

    Another option is to add module.finalRules. webpack would apply these rules after module.rules, so the default URL handling could take precedence over a broad rule such as { test: /.js$/, type: 'javascript/auto' }.

    The built-in finalRules would be:

    [
      {
        dependency: 'url',
        oneOf: [
          {
            scheme: /^data$/,
            type: 'asset/inline',
          },
          {
            type: 'asset/resource',
          },
        ],
      },
    ]
    

    If a user specifies module.finalRules, their array replaces the built-in rules. The special "..." entry explicitly inserts the built-in rules at that position. For example, finalRules: ["..."] keeps the defaults, and finalRules: [] disables them.

  5. alexander-akait commented on Nov 12, 2026

    @alexander-akait
    Member

    Yeah, known limitation, the new for html and css (as you can see), need to think about it

  6. thisisvaishnav commented on Nov 13, 2026

    @thisisvaishnav

    Problem & Solution: The user's module.rules override the default dependency: "url" asset type so URLDependencyTemplate receives a JS module instead of an asset string and stringifies its exports object to [object%20Module]; for now this can be solved in config with dependency: { not: ['url'] }.

    I'd like to contribute and help fix this in Webpack—should I open a PR? If so, what direction would the maintainers prefer (e.g. emitting a warning/error when a URL dependency resolves to a non-asset module, or a config-level solution)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: parserJavaScript parsing and dependency analysis (lib/javascript, lib/dependencies)area: runtimeEmitted runtime and chunk loading across targets (lib/runtime, lib/web, lib/node, lib/esm)

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions