Skip to content

Conversation

@abbasyed
Copy link
Contributor

@abbasyed abbasyed commented Oct 28, 2025

Description

Implements support for template literals containing import.meta.env.* expressions in Web Worker URL definitions.

Fixes #20900

Problem

Currently, users must use string concatenation for dynamic paths with environment variables in worker URLs:

new URL('../path/' + import.meta.env.VAR + '/worker.ts', import.meta.url)

This is inconsistent with template literal usage elsewhere in Vite and less ergonomic.

Solution

Template literals with import.meta.env.* expressions are now supported and automatically transformed:

new URL(`../path/${import.meta.env.VAR}/worker.ts`, import.meta.url)

Changes

  • Added AST-based detection of safe template literals (only import.meta.env.* expressions allowed)
  • Transforms safe template literals to string concatenation before Rollup processes them
  • Enhanced error messages for unsafe dynamic expressions
  • Added comprehensive test coverage in playground/worker

Safety

  • ✅ Only allows import.meta.env.* expressions in template literals
  • ✅ Other dynamic variables are still blocked with helpful error messages
  • ✅ No performance impact for existing code paths
  • ✅ No new dependencies added
  • ✅ Preserves source maps

Testing

  • ✅ Added test cases in playground/worker covering:
    • Single environment variable
    • Multiple environment variables
    • Mixed string literals and env vars
    • Error cases for unsafe dynamic expressions
  • ✅ Tests pass in both serve and build modes
  • ✅ All existing tests continue to pass

Breaking Changes

None. This is purely additive functionality.

Allows template literals containing only import.meta.env.* expressions
in Web Worker URL definitions, transforming them to string concatenation.

Before (blocked with error):
```javascript
const worker = new Worker(
  new URL(\`./path/\${import.meta.env.MY_VAR}/worker.ts\`, import.meta.url),
  { type: 'module' }
)
```

After (now supported):
The template literal is automatically transformed to:
```javascript
new URL('./path/' + import.meta.env.MY_VAR + '/worker.ts', import.meta.url)
```

Implementation:
- Added transformSafeTemplateLiteral() to detect and transform safe templates
- Only allows import.meta.env.* expressions (no other dynamic variables)
- Transforms safe templates to string concatenation before Rollup processing
- Provides helpful error messages for truly dynamic expressions
- Added test files demonstrating the feature

Technical details:
- Two-pass transform: first transforms safe templates, then processes workers
- Uses AST parsing to validate only safe expressions are present
- Preserves source maps through MagicString transformations
- No new dependencies required
@abbasyed abbasyed force-pushed the feat/worker-url-template-literals branch from b2c8929 to 54a15b8 Compare October 28, 2025 01:51
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.

Allow import.meta.env in worker import

1 participant