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

Vitest problem with LESS files #2762

Closed
6 tasks done
oferitz opened this issue Jan 27, 2023 · 2 comments
Closed
6 tasks done

Vitest problem with LESS files #2762

oferitz opened this issue Jan 27, 2023 · 2 comments

Comments

@oferitz
Copy link

oferitz commented Jan 27, 2023

Describe the bug

I'm trying to migrate a project from CRA+Jest to Vite+Vitest, so far everything went well but now i'm stuck at a point where vitest is failing the majority of the tests with the same error:

SyntaxError: Invalid or unexpected token
 - /node_modules/antd/lib/date-picker/style/index.js:3:1

Digging into this file reveals that this file is doing require to other file who contains a LESS variable

"use strict";

require("./index.less");
require("../../button/style");
require("../../tag/style");

index.less

@root-entry-name: default;

@import './index-pure.less';

So the underlying error is actually:

Invalid or unexpected token
/Users/oferitz/Projects/my-project/node_modules/antd/lib/date-picker/style/index.less:1
@root-entry-name: default;
^

Now in the app itself everything is working fine, the app transpiles, all less files and variables resolved correctly with this vite.config.ts

/// <reference types="vitest" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import checker from 'vite-plugin-checker'
import tsconfigPaths from 'vite-tsconfig-paths'
import eslintPlugin from 'vite-plugin-eslint'
import svgr from 'vite-plugin-svgr'
import vitePluginImp from 'vite-plugin-imp'
import { configDefaults } from 'vitest/config'


export default defineConfig(({ mode }) => {
  return {
    build: {
      sourcemap: true
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: {
            // LESS variables
          }
        }
      }
    },

    resolve: {
      alias: [
        // Alias for Ant Design less files
        { find: /^~/, replacement: '' }
      ]
    },
    plugins: [
      react(),
      vitePluginImp({
        libList: [
          {
            libName: 'antd',
            libDirectory: 'es',
            style: name => `antd/es/${name}/style`
          }
        ]
      }),
      svgr(),
      checker({ typescript: true }),
      tsconfigPaths(),
      eslintPlugin()
    ],
    define: {
      ...(mode === 'development' && { global: 'window', 'process.env': {} })
    },
    test: {
      globals: true,
      environment: 'jsdom',
      setupFiles: './src/setupTests.ts'
    }
  }
})

To my understanding Vitest is using the above config so what am i missing here?
I tried to set some configuration options like deps : {fallbackCJS : true} but it didn't help...
Any help will be appreciated.

Reproduction

System Info

System:
    OS: macOS 13.1
    CPU: (8) arm64 Apple M1
    Memory: 94.95 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.13.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.19.3 - /usr/local/bin/npm
    Watchman: 2022.11.14.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 109.0.5414.119
    Firefox Developer Edition: 110.0
    Safari: 16.2
  npmPackages:
    @vitejs/plugin-react: ^3.0.1 => 3.0.1 
    @vitest/ui: ^0.28.2 => 0.28.2 
    vite: ^4.0.4 => 4.0.4 
    vitest: ^0.28.2 => 0.28.2

Used Package Manager

npm

Validations

@oferitz
Copy link
Author

oferitz commented Jan 27, 2023

Closing cause i found the cause:
there was an import from lib somewhere and not from es

import 'antd/lib/date-picker/style/index' -> import 'antd/es/date-picker/style/index'

@oferitz oferitz closed this as completed Jan 27, 2023
@rluvaton
Copy link
Contributor

Possible fix to future visitors:

I had a problem with antd styles as well but the solution above me did not work maybe because antd was used by a dependency, so I created a PR to fix it in: #3465 and it's fixed in v0.31.2

@github-actions github-actions bot locked and limited conversation to collaborators Jun 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants