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

Linaria doesn't generate css in Vite #1240

Closed
houlang opened this issue Apr 24, 2023 · 15 comments · Fixed by #1280
Closed

Linaria doesn't generate css in Vite #1240

houlang opened this issue Apr 24, 2023 · 15 comments · Fixed by #1280
Assignees
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided platform: ssr 🛠️ Issue related to SSR

Comments

@houlang
Copy link

houlang commented Apr 24, 2023

Environment

my project create by:

yarn create vite project_name --template react

package.json:

{
  "name": "ithome_test_linaria",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },
  "dependencies": {
    "@linaria/babel-preset": "^4.4.3",
    "@linaria/core": "^4.2.8",
    "@linaria/react": "^4.3.6",
    "less": "^4.1.3",
    "less-loader": "^11.1.0",
    "mobx": "^6.9.0",
    "mobx-react-lite": "^3.4.3",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@linaria/vite": "^4.2.9",
    "@types/react": "^18.0.28",
    "@types/react-dom": "^18.0.11",
    "@vitejs/plugin-react": "^4.0.0-beta.0",
    "eslint": "^8.38.0",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.3.4",
    "vite": "^4.3.1"
  }
}

vite.config.js:

import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import linaria from '@linaria/vite';
// https://vitejs.dev/config/
export default defineConfig({
    plugins: [linaria(), react()],
    server: {
        open: true
    }
}) 

Test Components:

import { styled } from '@linaria/react';
const Title = styled.div`
  width: 100px;
  height: 100px;
  color: red;
`;
const Test = (props) => {
    return (
        <div>
            <Title>TitleTitle</Title>
        </div>
    )
}

export default Test; 

App.jsx:

import { useState } from 'react'

// import './App.css'
import Test from "./pages/Test/index.jsx";
function App() {
  const [count, setCount] = useState(0)

  return (
    <>
        <Test />
    </>
  )
}

export default App 

Description

run:yarn dev

In browser console show error

Failed to load resource: the server responded with a status of 404 (Not Found)

In Shell console show error info

Failed to load url /src/pages/Test/index_ursw01.css (resolved id: D:/.../src/pages/Test/index_ursw01.css) in D:/.../src/pages/Test/index.jsx. Does the file exist?

But running the build is ok

yarn build

So the css can be generated. Why?

My Question

Is it not supported for vite@^4.3.1 generation css?

I need help

@houlang houlang added bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided needs: triage 🏷 Issue needs to be checked and prioritized labels Apr 24, 2023
@github-actions github-actions bot added platform: ssr 🛠️ Issue related to SSR and removed needs: triage 🏷 Issue needs to be checked and prioritized labels Apr 24, 2023
@leedalei
Copy link

same issue,any updates?

@houlang
Copy link
Author

houlang commented Apr 27, 2023

same issue,any updates?

The version I am using now is ~4.2.0

run:

yarn remove vite

yarn add vite@~4.2.0 # not this vite@^4.2.0 

@dx314
Copy link

dx314 commented May 3, 2023

I'm having the same issue. File does not exist.

@houlang
Copy link
Author

houlang commented May 3, 2023

I'm having the same issue. File does not exist.

Try to run:

yarn remove vite

yarn add vite@~4.2.0 # not this vite@^4.2.0 

@nstepien
Copy link
Contributor

nstepien commented May 5, 2023

Replacing @linaria/vite with @linaria/rollup solved issues I had with Vite 4.3, maybe you could try that.

@houlang
Copy link
Author

houlang commented May 5, 2023

Replacing @linaria/vite with @linaria/rollup solved issues I had with Vite 4.3, maybe you could try that.

I have tried and I think that is not the perfect solution I just want @linaria/vite

@nstepien
Copy link
Contributor

nstepien commented May 5, 2023

@houlang you can use the rollup plugin in your vite config

@houlang
Copy link
Author

houlang commented May 5, 2023

@houlang you can use the rollup plugin in your vite config

I have already used @linaria/rollup

run yarn dev is ok

but yarn build failed,I am going back to vite@~4.2.0 version,continue to use @linaria/vite

error info:

vite v4.3.5 building for production...
✓ 849 modules transformed.
✓ built in 33.42s
[commonjs--resolver] D:\...test_linaria_rollup\node_modules\rc-menu\es\hooks\useAccessibility.js: Cannot read properties of null (re
ading 'body')
file: D:/.../test_linaria_rollup/node_modules/rc-menu/es/hooks/useAccessibility.js
error during build:
TypeError: D:\...\test_linaria_rollup\node_modules\rc-menu\es\hooks\useAccessibility.js: Cannot read properties of null (reading 'bod
y')

My config

import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import linaria from '@linaria/rollup';

import path from 'path' // install:npm i -D @types/node


// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react(), linaria({
        // sourceMap: process.env.NODE_ENV !== 'production',
    })],
    resolve: {
        alias: {
            // @ => src
            '@': path.resolve(__dirname, 'src'),
            // ~@=>src
            '~@': path.resolve(__dirname, 'src')
        }
    }
})

package.json

{
  "name": "test_linaria_rollup",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },
  "dependencies": {
    "@linaria/babel-preset": "^4.4.5",
    "@linaria/core": "^4.2.10",
    "@linaria/react": "^4.3.8",
    "@rollup/plugin-babel": "^6.0.3",
    "antd": "^5.4.6",
    "axios": "^1.4.0",
    "history": "^5.3.0",
    "less": "^4.1.3",
    "less-loader": "^11.1.0",
    "mobx": "^6.9.0",
    "mobx-react-lite": "^3.4.3",
    "nprogress": "^0.2.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.11.1",
    "rollup-plugin-css-only": "^4.3.0",
    "rollup-plugin-visualizer": "^5.9.0"
  },
  "devDependencies": {
    "@linaria/rollup": "^4.3.8",
    "@types/node": "^20.0.0",
    "@types/react": "^18.0.28",
    "@types/react-dom": "^18.0.11",
    "@vitejs/plugin-react": "^4.0.0",
    "eslint": "^8.38.0",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.3.4",
    "vite": "^4.3.2"
  }
}

@hmerritt
Copy link
Contributor

hmerritt commented Jul 2, 2023

I think I've solved this. I was having the same problem and was messing around with it for ages.

In linaria/packages/vite/src/index.ts change cssFilename -> cssId:

result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;

->

result.code += `\nimport ${JSON.stringify(cssId)};\n`;

@houlang
Copy link
Author

houlang commented Jul 3, 2023

I think I've solved this. I was having the same problem and was messing around with it for ages.

In linaria/packages/vite/src/index.ts change cssFilename -> cssId:

result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;

->

result.code += `\nimport ${JSON.stringify(cssId)};\n`;

Unfortunately, the author's updates are a bit slow.

@ktdd
Copy link

ktdd commented Jul 13, 2023

I think I've solved this. I was having the same problem and was messing around with it for ages.

In linaria/packages/vite/src/index.ts change cssFilename -> cssId:

result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;

->

result.code += `\nimport ${JSON.stringify(cssId)};\n`;

Damn, this stopped working in latest Linaria. Really need a fix for this.

e: Removing changes made by #1263 and everything works again.

@Anber Anber self-assigned this Jul 13, 2023
@Anber
Copy link
Collaborator

Anber commented Jul 13, 2023

Hi guys!
It looks like the issue appears only on Windows and is related to backslashes in paths. The problem here is that I don't have Windows, and I'm not allowed to use it by the company's security policies. It's solvable by using some cloud Windows, but it's an extremely uncomfortable and slow solution. So, if someone creates a PR, I would be extremely appreciated! :)

@nstepien
Copy link
Contributor

@Anber would it help to run the Linaria CI on Windows as well?

@Anber
Copy link
Collaborator

Anber commented Jul 13, 2023

@nstepien yes, it will definitely help to prevent regressions.

@ktdd
Copy link

ktdd commented Jul 13, 2023

Hi guys! It looks like the issue appears only on Windows and is related to backslashes in paths. The problem here is that I don't have Windows, and I'm not allowed to use it by the company's security policies. It's solvable by using some cloud Windows, but it's an extremely uncomfortable and slow solution. So, if someone creates a PR, I would be extremely appreciated! :)

Oh yeah, that was an easy solve now that we know it's related to backslashes.

In https://github.com/callstack/linaria/blob/master/packages/vite/src/index.ts, import path from 'node:path/posix' instead. That changes A:\Dir\Project\styles\linaria_{slug}.css to A:/Dir/Project/styles/linaria_{slug}.css and Windows finally finds the file.

hmerritt added a commit to hmerritt/linaria that referenced this issue Jul 13, 2023
Anber added a commit that referenced this issue Jul 14, 2023
…1280)

* fix(vite): css `url()` windows-specific resolve error (#1240)

* chore: changeset

* chore: linter

---------

Co-authored-by: Anton Evzhakov <anton@evz.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided platform: ssr 🛠️ Issue related to SSR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants