vite icon indicating copy to clipboard operation
vite copied to clipboard

fix: call `resolveId` hooks of custom plugins for css imports

Open aleclarson opened this issue 3 years ago • 18 comments

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).

aleclarson avatar Oct 20 '22 16:10 aleclarson

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.

patak-dev avatar Oct 21 '22 12:10 patak-dev

/ecosystem-ci run

patak-dev avatar Oct 21 '22 12:10 patak-dev

📝 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

vite-ecosystem-ci[bot] avatar Oct 21 '22 12:10 vite-ecosystem-ci[bot]

For reference, the fails in ecosystem-ci are the same as with main.

patak-dev avatar Oct 21 '22 12:10 patak-dev

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).

aleclarson avatar Oct 22 '22 01:10 aleclarson

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.

patak-dev avatar Oct 22 '22 05:10 patak-dev

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 avatar Oct 22 '22 16:10 aleclarson

@aleclarson Any idea when this would be merged and shipped ? I m also blocked by this

Viijay-Kr avatar Oct 25 '22 11:10 Viijay-Kr

@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.

patak-dev avatar Nov 03 '22 19:11 patak-dev

@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)

patak-dev avatar Nov 30 '22 06:11 patak-dev

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?

itsdouges avatar Dec 30 '22 00:12 itsdouges

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.

bluwy avatar Dec 30 '22 15:12 bluwy

Hey @patak-dev would you have some time soon(TM) to review this?

Thanks!

hakubo avatar Jan 31 '23 19:01 hakubo

@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 avatar Jan 31 '23 20:01 aleclarson

@aleclarson right! Sorry missed that. I see it is added to milestone 5 for now.

hakubo avatar Feb 01 '23 12:02 hakubo

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.

bluwy avatar May 03 '23 04:05 bluwy

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. 😄

aleclarson avatar Jul 14 '23 16:07 aleclarson

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

emosheeep avatar Dec 08 '23 12:12 emosheeep