getStyles() not working in dist library rewrite

Created on 2 October 2023, about 1 year ago

Problem/Motivation

When processing JS files in Vite::rewriteLibraryForDist(), it will try to include bundled css by calling Manifest::getStyles($path).

However, this lookup fails when the generated file does have a matching entry in manifest.json.

Here is an example:

// vite.config.js
rollupOptions: {
  input: 'src/main.js'
}
// main.js
import 'virtual:uno.css';
import './styles.css';
// manifest.json
{
  "src/main.css": {
    "file": "main-58455558.css",
    "src": "src/main.css"
  },
  "src/main.js": {
    "css": [
      "main-58455558.css"
    ],
    "file": "main-b48015fe.js",
    "isEntry": true,
    "src": "src/main.js"
  }

getStyles() returns getChunkPropertyPaths('css', $chunk, $prependBaseUri).

getChunkPropertyPaths() checks if there's a value for the css property, and then does:

    return array_filter(array_map(
      fn($import) => $this->getChunk($import, $prependBaseUri),
      $this->manifest[$chunk][$property],
    ));

getChunk() does a lookup on main-58455558.css, but it's not a top level chunk in manifest.json, so getStyles() returns nothing.

Steps to reproduce

Here is how to configure vite in a way that works with the current code.

input: 'src/main.js',
output: {
  entryFileNames: `src/[name].js`,
  assetFileNames: `src/[name].[ext]`,
},

With the src/ path added to the entry file and generated asset name, the manifest.json has:

{
  "src/main.css": {
    "file": "src/main.css",
    "src": "src/main.css"
  },
  "src/main.js": {
    "css": [
      "src/main.css"
    ],
    "file": "src/main.js",
    "isEntry": true,
    "src": "src/main.js"
  }
}

Proposed resolution

getStyles should use the file names in the chunk's css property instead of looking them up as another chunk in the manifest.

I think this has been overlooked since having the css in the libraries.yml file will load it when it's not found as a bundle for the js library.

Remaining tasks

Patch it.

API changes

Might disrupt sites, so should be in a 2.x branch.

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

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