Add type=module attribute to imports

Created on 29 December 2023, 11 months ago
Updated 4 April 2024, 7 months ago

Problem/Motivation

When importing the same file in multiple in multiple js files, it gets added as import keys in manifest.json.
The module then adds them to the library in vite\rewriteLibraryForDist.

foreach ($imports as $importPath) {
  $library['js'][$importPath] = ['attributes' => ['rel' => 'modulepreload']];
}

However, if the imported file contains an exports you get an error in the console.
SyntaxError: Unexpected token 'export'

Steps to reproduce

Create a script foo.js

export const foo = "bar";

Import it in multiple other scripts in your theme/module.

import { foo } from './utils.js'
console.log(foo);

Check dev tools console for errors.

Proposed resolution

Either always add type=module to the imported scripts (might have drawbacks I'm not aware of) or have it as an option.

$library['js'][$importPath] = ['attributes' => ['rel' => 'modulepreload', 'type' => 'module']];
🐛 Bug report
Status

Fixed

Version

1.1

Component

Code

Created by

🇧🇪Belgium kubrick

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024