fix: call `resolveId` hooks of custom plugins for css imports
Note: All plugins whose name matches vite:* are excluded from resolution of CSS imports. Any instances of @rollup/plugin-alias are also excluded (use resolve.alias instead).
Could you explain why we need to exclude internal plugins? If we are going to start having users plugins, should we directly the full pipeline and modify internal plugins where needed? If ours plugins don't work here, this may flag that users could also have issues with this change.
/ecosystem-ci run
📝 Ran ecosystem CI: Open
| suite | result |
|---|---|
| astro | :white_check_mark: success |
| histoire | :x: failure |
| iles | :white_check_mark: success |
| ladle | :white_check_mark: success |
| laravel | :white_check_mark: success |
| marko | :x: failure |
| nuxt-framework | :white_check_mark: success |
| rakkas | :white_check_mark: success |
| svelte | :x: failure |
| vite-plugin-ssr | :x: failure |
| vite-setup-catalogue | :white_check_mark: success |
| vitepress | :white_check_mark: success |
| vitest | :white_check_mark: success |
| windicss | :white_check_mark: success |
For reference, the fails in ecosystem-ci are the same as with main.
Could you explain why we need to exclude internal plugins? If we are going to start having users plugins, should we directly the full pipeline and modify internal plugins where needed? If ours plugins don't work here, this may flag that users could also have issues with this change.
It's an optimization. None of Vite's internal plugins expect to receive CSS imports, so there's no benefit to using them (apart from vite:resolve which is always applied by createResolver).
Ah, gotcha. I think it would be good to measure if having this optimization gets us much. But let's leave it as is until we discuss the PR in a team meeting. Added it to the board now.
I think it would be good to measure if having this optimization gets us much.
That's a fair point. Is there a recommended way to benchmark the test suite?
Benchmarking isn't mentioned in the CONTRIBUTING.md guide (https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md).
@aleclarson Any idea when this would be merged and shipped ? I m also blocked by this
@aleclarson we discussed the PR in today's team meeting, and this sounds good. Before merging, we need to check that enforce and order are being respected for user plugins.
@aleclarson just a heads up in case you would like to push this feature for Vite 4, we're planning to release a beta soon and try to release the major around Dec 10. This is a feature we could include in a minor version later if not (just create the 4.1 milestone, we can move it there in that case)
This is a feature we could include in a minor version later if not (just create the 4.1 milestone, we can move it there in that case)
Hey folks is there a way we could get this in as a non-breaking change? For context I'm interested in this for our work on the Deno resolver plugin https://github.com/itsdouges/vite_plugin_deno_resolve
This looks to be a great first party solution to getting the optimizer using our resolver. What work is missing to get this merged?
We need patak's review to be resolved and tests for this. One way we could make this non-breaking is an experimental option to enable this feature, which could be a good workaround as the Vite plugin can turn it on internally.
Hey @patak-dev would you have some time soon(TM) to review this?
Thanks!
@hakubo He has reviewed it, and he raised some issues with the current state of the PR. This PR needs someone with time to push it forward (eg: write tests and resolve Patak's concerns).
@aleclarson right! Sorry missed that. I see it is added to milestone 5 for now.
FWIW I was able to workaround it by implementing this trick on the plugin side. Given most plugins don't need to tap into createResolver for CSS etc often, I'm slightly leaning towards the trick being a good enough solution so there's no potential perf loss.
I question whether config.createResolver(…) should even be a public API. Why isn't it createResolver(config, …) instead? It should at least be documented if monkey-patching it is seen as an acceptable maneuver. 😄
FWIW I was able to workaround it by implementing this trick on the plugin side.
I managed to do this originally using the following lines:
export function createTsAliasPlugin(): PluginOption {
const tsResolvePlugin = tsconfigPaths({
root: monoRoot,
ignoreConfigErrors: true,
loose: true, // to support resolve alias in scss files(any other non-script files)
});
return [
tsResolvePlugin,
{
name: 'alias:style',
config() {
return {
resolve: {
alias: [
{
find: /(.*\.scss)$/,
replacement: '$1',
async customResolver(source, importer) {
if (importer && /\.scss/.test(importer)) {
// HACK: resolve alias in style files.
// @ts-ignore
return tsResolvePlugin.resolveId.apply(this, arguments);
}
},
},
],
},
};
},
},
];
}
But after I saw your implementation, I try to use it as follows:
export function createTsAliasPlugin(): PluginOption {
return [
tsResolvePlugin,
{
name: 'alias:style',
configResolved(config) {
patchCreateResolver(config, tsResolvePlugin);
},
},
];
}
It should work fine as I see, but actually not. Do you guys know what happened deep within it? @aleclarson @bluwy