Skip to content

Conversation

@dschmidt
Copy link
Contributor

Description

This fixes broken asset imports in extensions/apps using the extension-sdk.
We have worked around broken imports by using import.meta.url, dirname, join etc. which is very brittle when paths change (see related issue).
This makes ?url and ?worker work no matter where the extension/app is placed on the server.

Related Issue

Related: opencloud-eu/web-extensions#235
The ticket above can be fixed without this change, because the icons are small and beneath the assetsInlineLimit - but it can break again whenever the limit is/the files are changed.

How Has This Been Tested?

  • Unit tests in the vite PR
  • I have changed unzip and maps apps to use asset imports

Types of changes

  • Bugfix
  • Enhancement (a change that doesn't break existing code or deployments)
  • Breaking change (a modification that affects current functionality)
  • Technical debt (addressing code that needs refactoring or improvements)
  • Tests (adding or improving tests)
  • Documentation (updates or additions to documentation)
  • Maintenance (like dependency updates or tooling adjustments)

Copilot AI review requested due to automatic review settings October 22, 2025 10:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes broken asset imports in extensions/apps using the extension-sdk by implementing a workaround for a Vite bug where AMD modules lack proper require variable injection. The fix includes adding a custom Vite plugin and configuring relative asset paths.

Key Changes:

  • Added completeAmdWrapPlugin to inject require into AMD module wrappers
  • Set base: './' to make asset paths relative for proper resolution regardless of extension location
  • Restructured configuration ordering (moved server config after plugins)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

// TL;DR of the issue: vite uses the require variable for certain features but doesn't ensure it's injected into AMD modules
// thus the global require variable is used which does not have the correct base path set, so relative imports
// with ?url or ?worker break
const completeAmdWrapPlugin = () => {
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

[nitpick] The regular expression is complex and lacks explanation. Consider adding a comment explaining what patterns it matches and why, especially the optional capture groups and spacing patterns.

Suggested change
const completeAmdWrapPlugin = () => {
const completeAmdWrapPlugin = () => {
// This regular expression matches AMD 'define' calls of the form:
// define([deps], function(params) { ... })
// or
// define(function(params) { ... })
//
// Breakdown:
// - \bdefine\( : matches the word 'define' followed by an opening parenthesis.
// - (?:\s*\[([^\]]*)\],)? : optionally matches a dependency array (e.g., [deps]), capturing the contents in group 1.
// - \s* : allows for optional whitespace before the array.
// - \[([^\]]*)\] : matches the array and captures its contents.
// - , : matches the comma after the array.
// - \s* : allows for optional whitespace after the dependency array or directly after '('.
// - (?:\(\s*)? : optionally matches an extra opening parenthesis and whitespace (for cases like define(([...]), ...)).
// - function\s*\(([^)]*)\)\s*\{ : matches 'function', optional whitespace, an opening parenthesis, captures the function parameters in group 2, closing parenthesis, optional whitespace, and opening curly brace.

Copilot uses AI. Check for mistakes.

return {
code: code.replace(AmdWrapRE, (_, deps, params) => {
if (deps?.includes('"require"')) {
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

The check for \"require\" in the deps string is fragile. If deps contains 'require' (with single quotes) instead of \"require\" (with double quotes), this check will fail. Consider using a more robust pattern like /['"]require['"]/ to handle both quote styles.

Suggested change
if (deps?.includes('"require"')) {
if (deps && /['"]require['"]/.test(deps)) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@JammingBen JammingBen left a comment

Choose a reason for hiding this comment

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

Thank you 👍

@JammingBen JammingBen merged commit 19b568b into opencloud-eu:main Oct 22, 2025
29 checks passed
@openclouders openclouders mentioned this pull request Oct 22, 2025
1 task
openclouders pushed a commit that referenced this pull request Oct 22, 2025
fix(extension-sdk): ensure asset imports work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants