Skip to content

.css.ts files not scanned for imports #5124

@mattcompiles

Description

@mattcompiles

Describe the bug

I'm trying to get Vite working with vanilla-extract. VE uses a custom file extension of .css.ts to identify files that use styling code. Unfortunately, this is causing some issues when esbuild scans the code for deps. Inside the esbuildScanPlugin it marks all .css requests as external without doing a proper Vite resolve on it. This can be seen here:

filter: /\.(css|less|sass|scss|styl|stylus|pcss|postcss|json)$/

This means that all deps within a .css.ts file get ignored by the scan. This could be fixed by running a full resolve on the file but I can see that that was opted out of for perf reasons. There are a few ways I can think of to resolve the issue (unless someone has a better idea):

  • Option 1: Do a full Vite resolve on file requests ending in .css. I assume this would have a small perf cost in projects with lots of CSS files, however not doing a proper resolve is a dangerous optimisation as shown here because we don't really know the file is a CSS file, just that the import request ended in .css.
  • Option 2: Add some config to generate a better matching regex in the esbuildScanPlugin. E.g. Adding resolve.customExtensions: ['.css.ts'] would allow Vite to know that it's not safe to assume .css requests don't need to be resolved. We could use the existing resolve.extensions but not sure if that would clash with other behaviour. While this config may be confusing for most users the @vanilla-extract/vite-plugin could be in charge of setting it consumers implicitly.

There's other config that could be exposed but the above was the nicest option I thought of. Happy to discuss other ideas though.

Lastly, I'm happy to do the work for this if we can agree on the approach. This is a blocker for my work on vanilla-extract and some other open source projects I'm currently working on.

Reproduction

I didn't add a reproduction due to the context supplied. I can add if it helps understanding on the issue though.

System Info

System:
    OS: macOS 11.5.2
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
    Memory: 244.00 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
Binaries:
    Node: 12.18.2 - ~/.volta/tools/image/node/12.18.2/bin/node
    Yarn: 1.22.10 - ~/.volta/tools/image/yarn/1.22.10/bin/yarn
    npm: 6.14.5 - ~/.volta/tools/image/node/12.18.2/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
Browsers:
    Chrome: 94.0.4606.61
    Firefox: 83.0
    Safari: 14.1.2
Packages:
    vite: 2.5.1

Used Package Manager

yarn

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: cssp3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions