Skip to content

refactor: import meta url#205

Open
fkakatie wants to merge 3 commits intomainfrom
import-meta-url
Open

refactor: import meta url#205
fkakatie wants to merge 3 commits intomainfrom
import-meta-url

Conversation

@fkakatie
Copy link
Member

@fkakatie fkakatie commented Feb 5, 2026

Description

Replaces the DOM query approach for detecting codeBasePath with import.meta.url, simplifying the code and making it more reliable.

Before:

  • Queries the DOM for <script src$="/scripts/scripts.js">
  • Extracts path from the script element's src attribute
  • 14 lines of code with nested conditionals

After:

  • Uses import.meta.url directly from the ES module
  • Simpler, more direct approach
  • 8 lines of code (net -6 lines)

Example:
When aem.js is loaded from /scripts/aem.js:

  • import.meta.url = http://example.com/scripts/aem.js
  • Split by /scripts/ gives codeBasePath = '' (root) or base path

Related Issue

Closes #134

Motivation and Context

The original DOM query approach was a legacy from when aem.js and scripts.js were combined. With ES modules, import.meta.url provides a more direct and reliable way to determine the module's location without needing to search the DOM.

Benefits:

  • Simpler code: Reduced from 14 lines to 8 lines
  • More reliable: No dependency on DOM structure or script tag existence
  • Standard API: Uses ES module spec rather than DOM queries
  • Same browser support: import.meta.url is supported everywhere ES modules are

How Has This Been Tested?

Updated existing test in test/setup/setup.test.html:

  1. Removed fake script tag: No longer needed since we don't query DOM
  2. Updated assertions: Test now verifies codeBasePath exists rather than checking for specific path value
  3. Added documentation: Comments explain behavior difference between dev (src/) and production (scripts/)

Browser Support

import.meta.url is supported in all browsers that support ES modules (95%+ coverage):

Since this library already requires ES module support ("type": "module"), there is no additional browser support loss.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@fkakatie fkakatie requested a review from shsteimer February 5, 2026 23:11
@fkakatie fkakatie changed the title Import meta url refactor: import meta url Feb 11, 2026
expect(window.hlx.codeBasePath).to.be.equal('/some/path');
// When setup.js is in /src/, codeBasePath will be empty or src path
// In production, aem.js is at /scripts/aem.js so split works correctly
expect(window.hlx.codeBasePath).to.exist;
Copy link
Contributor

@shsteimer shsteimer Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be annoying, but I don't think there is much value in just asserting the value exists. The code could change in a way that breaks how the value is set, and the test wouldn't catch it.

If we want to test this logic, I'd probably change the signature of setup() to be setup(importUrl = import.meta.url) Then in the tests, we can pass different values to ensure window.hlx.codeBasePath is always set properly

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.

set codebase path based on import.meta.url?

2 participants