Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS is missed in transform hook #6421

Closed
7 tasks done
AlttiRi opened this issue Jan 9, 2022 · 11 comments · Fixed by #8278
Closed
7 tasks done

CSS is missed in transform hook #6421

AlttiRi opened this issue Jan 9, 2022 · 11 comments · Fixed by #8278
Labels
feat: css wontfix This will not be worked on

Comments

@AlttiRi
Copy link

AlttiRi commented Jan 9, 2022

Describe the bug

CSS is missed in transform hook now.

code argument of transform(code, id) hook contains only "export default ''" string. There is no CSS.

It worked before — with Vite 2.5.7.

For example, I was able to generate such style.css

With this plugin: (using), (plugin code)


For the reproduction uncomment these lines of the minimal reproduction repo

Reproduction

https://github.com/AlttiRi/serve-json-gz-length-test

System Info

OS: Windows 10 10.0.19043
Node: 17.3.0
Yarn: 1.22.5
npm: 7.19.1
vite: 2.7.2

Used Package Manager

npm

Logs

No response

Validations

@toSayNothing
Copy link
Contributor

toSayNothing commented Jan 9, 2022

I don't know the older version. but right now, if you put the cssBundlePlugin in plugins, will solve the problem:

// vite.config.js
export default defineConfig({
  plugins: [
    vue(),
    sourceMapsPathChangerPlugin([
      // Use "../../" instead of "../" if resources are in "assets/" directory
      ["../node_modules/", "node-modules:///"],
      ["../vite/", "node-modules:///vite/"],
      ["../src/", "source-maps:///"],
      ["../", "source-maps:///"],
    ]),
    cssBundlePlugin({
      overwriteBundle: "style.css",
      importFromModule: true,
    }),
  ],
  server: {
    open: `./`,
  },
...

more info about plugin API: https://vitejs.dev/guide/api-plugin.html
you can use the config hook to separate the dev or build mode.

@AlttiRi
Copy link
Author

AlttiRi commented Jan 9, 2022

Not solves.

In this case (when I use it as a plugin of Vite (plugins (not as build.rollupOptions.plugins)) the plugin just works as a transparent proxy, and it returns the code as is in transform hook.

In this case I receive the correct CSS in transform hook, instead of expected "export default '.warning \n{\n color:red\n}'" like it should be when I use the plugin as a Rollup plugin:

"[cssBundlePlugin]: Failed to load CSS as a module. Returns as pure code. Use "importFromModule: false"

However even with:

    cssBundlePlugin({
      bundleToOverwrite: "style.css",
      importFromModule: false // [!]
    }),

It does not work since in generateBundle(opts, bundles, isWrite) hook bundles argument has a missed property with CSS content.
[cssBundlePlugin]: "style.css" bundle is not found. Available bundles: ['index.js', 'vendor.js'].


The expected CSS in style.css is:

/* App.vue */
.warning {
  color: red;
}

I have updated the example repo code.

@AlttiRi
Copy link
Author

AlttiRi commented Jan 9, 2022

There are two bugs:

  1. A plugin used in build.rollupOptions.plugins has no CSS code in code argument of transform(code, id) hook, only "export default ''" string.
  2. A plugin used in plugins has no CSS file property in bundles object argument of generateBundle(opts, bundles, isWrite) hook, only {"index.js": {...}, "vendor.js": {...}}, there is no "style.css", "index.html" keys.

The first bug is new.
The second bug is old, it's why I use the plugin as Rollup's one.

@AlttiRi
Copy link
Author

AlttiRi commented Jan 10, 2022

The bug appeared with Vite 2.6.0.

With "~2.5.10" it works OK.

@lauritk
Copy link

lauritk commented Feb 18, 2022

Indeed 2.6.0 seems to mess style processing (causing all sorts of strange style related issues). Had similar issues with Lit + imported SCSS + PostCSS + Tailwind project with vite@2.6.0 and newer when doing production build, but everything worked with dev build. Switching to vite@2.5.10 and @vitejs/plugin-legacy@1.6.4 fixed the issue. Used rollup-plugin-postcss-lit (in vite plugin config) to load processed CSS into Lit elements.

Using rollup-plugin-postcss caused same export default '' error, when used it with/without rollup-plugin-postcss-lit in the rollup plugin config.

I am too interested in what causes this. I have not yet found the breaking change.

@bluwy bluwy added the regression The issue only appears after a new release label Apr 3, 2022
sapphi-red added a commit to sapphi-red/vite that referenced this issue Jun 5, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2022
This was referenced Jul 1, 2022
@sapphi-red
Copy link
Member

Reopening as #8278 and #8471 was reverted. See #8896 and #8874 for details.

@sapphi-red sapphi-red reopened this Jul 3, 2022
@vitejs vitejs unlocked this conversation Jul 3, 2022
@AlttiRi
Copy link
Author

AlttiRi commented Dec 3, 2022

With the latest Vite (3.2.4) while this issue is still actual, I can handle CSS:

  • a plugin with the transform hook should be used as a Vite's plugin
  • build.cssCodeSplit should be false

In this case it works with minor changes:

  • ignore the additional CSS from index.html
  • the result bundle name is index.css, not style.css

I use the plugin to generate such CSS:
https://alttiri.github.io/file-manager-snapshot-explorer/index.css

Because Vite.js still does not support SourceMaps for CSS.
#2830

@snatvb

This comment was marked as spam.

@yunsii
Copy link

yunsii commented Jul 23, 2023

Plugin hooks like resolveId, load and others code transform hooks should also apply to css/less/sass?

@YuchenCC
Copy link

YuchenCC commented Feb 4, 2024

same issue. I had fixed it by delete enforce key. You may set it post?

@sapphi-red
Copy link
Member

Given that

, I'll close this issue as wontfix.
If there's a good way to solve this, feel free to create a PR or a new issue.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2024
@sapphi-red sapphi-red added wontfix This will not be worked on and removed pending triage regression The issue only appears after a new release labels Mar 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: css wontfix This will not be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants