Skip to content

Conversation

@jimmywarting
Copy link

use explicit path in imports (See #4587)

### Title: Encourage Explicit Import Paths for Better Compatibility and Predictability

**Description:**
When using Alpine.js via a CDN (e.g., [jsDelivr](https://cdn.jsdelivr.net)), importing modules without explicit file extensions may lead to issues. For example, attempting to import the CSP build results in a 404 error:

```javascript
import Alpine from 'https://cdn.jsdelivr.net/npm/@alpinejs/csp@3.14.9/builds/cdn.js';
```

This error occurs because Alpine.js includes module imports without explicitly specifying file extensions. Some runtime environments and CDNs, including Deno, enforce stricter module resolution and do not guess the correct extension.

### Example Error:

```
GET https://cdn.jsdelivr.net/npm/@alpinejs/csp@3.14.9/src/index net::ERR_ABORTED 404 (Not Found)
```

### Suggested Solution:

- Explicitly specify `.js` in all import paths, e.g.:
  ```javascript
  import Alpine from './index.js';
  ```
- This approach is aligned with best practices and ensures compatibility across multiple environments.

Ryan dhal did regreted the hole module resolver thinking index was cute, but oh so wrong he was. it adds complexity to a simple solution that don't even work in the browser/esm. It gets harder to make the switch by refactoring code to esm. paths need to be guessed/looked up: did you meant to import `foo.js` or `foo/index.js`? Remote path resolver don't have this luxury where it dose not have direct access to the harddrive and can't lookup the file that is needed.
@calebporzio
Copy link
Collaborator

PR Review: #4588 — use absolute path.

Type: Refactor (cosmetic)
Verdict: Close

What's happening (plain English)

This PR adds .js to one import path in packages/alpinejs/builds/cdn.js:

// Before
import Alpine from './../src/index'

// After
import Alpine from './../src/index.js'

The idea is that explicit file extensions in import specifiers are "more correct." The linked issue #4587 presumably argues for this.

Other approaches considered

  1. Do nothing — esbuild (Alpine's bundler) resolves extensionless imports without issue. The build output is byte-for-byte identical with or without .js. This is the right approach.
  2. Fix all inconsistent files — If we did want consistency, this PR misses 3 other files with the same extensionless pattern: packages/alpinejs/builds/module.js, packages/csp/builds/cdn.js, and packages/csp/builds/module.js. A consistency fix that only fixes 1 of 4 files isn't consistent.
  3. Adopt a lint rule — If explicit extensions mattered, enforce it project-wide. But they don't matter here because these files are build entry points consumed exclusively by esbuild.

Changes Made

No changes made.

Test Results

No tests to run — this is a cosmetic change to a build entry point. CI build check passed.

Code Review

  • The change itself is technically fine — adding .js to an import doesn't break anything.
  • But it also doesn't fix anything. esbuild resolves ./src/index to ./src/index.js automatically.
  • The PR only touches 1 of 4 files with this same pattern, so it doesn't even achieve consistency.
  • Zero community engagement (no reactions, comments, or reviews).

Security

No security concerns identified.

Verdict

Close. This is a zero-impact cosmetic change that doesn't fully achieve its own stated goal (consistency). Alpine's build system uses esbuild, which handles extensionless imports without issue — the build output is identical either way. The PR touches 1 of 4 files with this pattern, so merging it would actually make the codebase less consistent (1 file with .js, 3 without) until the others are fixed too. Not worth the merge noise. Thanks for the contribution though — the instinct toward explicit imports is reasonable in general, it just doesn't apply to bundled entry points.


Reviewed by Claude

@calebporzio calebporzio closed this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants