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

Fail to require (in cjs) CSS modules from an npm package #13298

Closed
7 tasks done
Jinjiang opened this issue May 22, 2023 · 8 comments · Fixed by #13519
Closed
7 tasks done

Fail to require (in cjs) CSS modules from an npm package #13298

Jinjiang opened this issue May 22, 2023 · 8 comments · Fixed by #13519
Labels
feat: css feat: deps optimizer Esbuild Dependencies Optimization p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release

Comments

@Jinjiang
Copy link
Contributor

Describe the bug

The background is I have some existing packages whose generated code is in CommonJS + CSS modules. E.g.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.circularFont = void 0;
const circular_font_module_scss_1 = __importDefault(require("./circular-font.module.scss"));
exports.circularFont = circular_font_module_scss_1.default.circularFont;
//# sourceMappingURL=index.js.map

This file uses require() to load CSS Modules.

In Vite, it fails to be loaded, however, an import works.

Reproduction

https://github.com/Jinjiang/reproductions/tree/vite-cssmodules-20230523

Steps to reproduce

pnpm install
pnpm dev

Then open browser, the console panel will show up:

import a css module {foo: '_foo_iyjzy_1', bar: '_bar_iyjzy_5'} // works
require a css module {__esModule: true} // not
{foo: '_foo_iyjzy_1', bar: '_bar_iyjzy_5'} // works
undefined // not
{foo: '_foo_1clpo_1', bar: '_bar_1clpo_5'} // works
{foo: '_foo_iyjzy_1', bar: '_bar_iyjzy_5'} // works

System Info

$ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers

  System:
    OS: macOS 13.4
    CPU: (8) arm64 Apple M1
    Memory: 69.14 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.asdf/installs/nodejs/16.15.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/shims/yarn
    npm: 8.5.5 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Safari: 16.5
  npmPackages:
    vite: ^4.3.8 => 4.3.8

Used Package Manager

pnpm

Logs

No response

Validations

@bluwy bluwy added feat: css feat: deps optimizer Esbuild Dependencies Optimization labels May 23, 2023
@hai-x
Copy link
Contributor

hai-x commented Jun 1, 2023

In the development stage, you can use vite plugin vite-plugin-cjs2esm to solve this problem. It provide esbuild plugin which can solve the commonjs import problem during esbuild pre-build stage.

import { defineConfig } from 'vite'
import * as cjs2esm from 'vite-plugin-cjs2esm'

export default defineConfig({
  plugins: [cjs2esm.vitePlugin()],
  optimizeDeps: {
    esbuildOptions: {
      plugins: [cjs2esm.esbuildPlugin()]
    }
  }
})

image

@Jinjiang
Copy link
Contributor Author

Jinjiang commented Jun 6, 2023

@haijie-x thanks for the information. I tried to install this package and added the vite config file you provided. First, it works in the browser console, which is great! At the same time, I got another error in the terminal:

Error:   Failed to scan for dependencies from entries:
  C:/Users/zhaoj/code/reproductions/index.html

  X [ERROR] The JSX syntax extension is not currently enabled

    html:C:/Users/zhaoj/code/reproductions/index.html:1:0:
      1 │ <script type="module" src="index.js"></script>
        ╵ ^

  The esbuild loader for this file is currently set to "js" but it must be set to "jsx" to be able to parse JSX syntax. You can use "loader: { '.js': 'jsx' }" to do that.


    at failureErrorWithLog (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1636:15)
    at C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1048:25
    at runOnEndCallbacks (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1471:45)
    at buildResponseToResult (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1046:7)
    at C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1058:9
    at new Promise (<anonymous>)
    at requestCallbacks.on-end (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:1057:54)
    at handleRequest (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:723:19)
    at handleIncomingPacket (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:745:7)
    at Socket.readFromStdout (C:\Users\zhaoj\code\reproductions\node_modules\.pnpm\esbuild@0.17.19\node_modules\esbuild\lib\main.js:673:7)

Is there anything I missed? Or do you have any idea how to make it works?

Thanks.

@hai-x
Copy link
Contributor

hai-x commented Jun 6, 2023

@Jinjiang Thanks for your feedback, please try the latest package version v1.0.6, a minor issue has just been fixed.

@hai-x
Copy link
Contributor

hai-x commented Jun 6, 2023

@Jinjiang Thanks for your feedback, please try the latest package version v1.0.6, a minor issue has just been fixed.

I just tried it and it works.

@Jinjiang
Copy link
Contributor Author

Jinjiang commented Jun 6, 2023

Cool. It works on 1.0.6 as you mentioned. Thanks so much.

@Jinjiang
Copy link
Contributor Author

Hi, I'd like to reopen this issue since:

  1. the plugin @haijie-x mentioned is still in debugging
  2. even this plugin can help, I still feel the root cause of this issue is about the native process of CSS/SCSS files in Vite, not a missing plugin to transform CJS code to ESM since other CJS syntax could be well-transformed natively.

Thanks.

@Jinjiang Jinjiang reopened this Jun 14, 2023
@sapphi-red
Copy link
Member

This seems to be a regression from 4.2.0 caused by #12343.
We should still use export * + export default for CSS modules files.

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release and removed pending triage labels Jun 14, 2023
@Jinjiang
Copy link
Contributor Author

@sapphi-red thanks for pointing this out. I further found it works on v4.1.x 😅

@github-actions github-actions bot locked and limited conversation to collaborators Jun 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: css feat: deps optimizer Esbuild Dependencies Optimization p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants