Skip to content

Path aliases for certain import statements are not resolved when packaging with @sveltejs/package 2.5.1+ #14455

@maxherrmann

Description

@maxherrmann

Describe the bug

When packaging with @sveltejs/package (versions 2.5.1 or higher), path aliases are not resolved for certain import statements, which breaks the package on the consumer side.

The issue was introduced with #14413.

Reproduction

Setup

Assume we have the following alias configuration in svelte.config.js:

alias: {
  $foo: "./src/lib/foo",
}

Assume we have a .ts or .svelte file in the root of ./src/lib with either one of the following import statements (all valid, as per MDN Docs):

import foo, { bar } from '$foo'
import foo, { bar, baz } from '$foo'
import foo, { type bar, type baz } from '$foo'
import foo, * as bar from '$foo'
import _foo from '$foo'
import * as _foo from '$foo'
import '$foo'

Current result after packaging

None of the path aliases have been resolved.

import foo, { bar } from '$foo'
import foo, { bar, baz } from '$foo'
import foo, { type bar, type baz } from '$foo'
import foo, * as bar from '$foo'
import _foo from '$foo'
import * as _foo from '$foo'
import '$foo'

Expected result after packaging

All path aliases have been resolved.

import foo, { bar } from './foo'
import foo, { bar, baz } from './foo'
import foo, { type bar, type baz } from './foo'
import foo, * as bar from './foo'
import _foo from './foo'
import * as _foo from './foo'
import './foo'

Root cause

The current regular expression that identifies path aliases does not seem to capture the following:

  • Combinations of default and named¹/namespaced imports
  • Default/namespaced imports with identifiers that start with _
  • Side-effect-only imports

¹) See #14455 (comment)

Workaround

As a workaround, you can use @sveltejs/package@2.5.0 or rewrite any affected import statements (if possible) so that they are captured by the current regular expression.

Logs

System Info

System:
    OS: macOS 15.6.1
    CPU: (12) arm64 Apple M4 Pro
    Memory: 192.52 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.16.0 - ~/.nvm/versions/node/v22.16.0/bin/node
    npm: 11.4.1 - ~/.nvm/versions/node/v22.16.0/bin/npm
  Browsers:
    Chrome: 140.0.7339.133
    Edge: 140.0.3485.66
    Safari: 18.6

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions