linaria icon indicating copy to clipboard operation
linaria copied to clipboard

[Feature Request] supporting linara in vite react-ts template

Open betterRunner opened this issue 3 years ago • 9 comments

I tried adding rollup plugin of linaria to vite template react-ts following https://github.com/callstack/linaria/blob/master/docs/BUNDLERS_INTEGRATION.md#Rollup without success. When calling css of @linaria/core in App.tsx, an error like 'App.tsx_xxx.css is not found' occurred.

image

As https://github.com/vitejs/vite/issues/1967 mentioned, it seems like linaria does not support vite yet (or did I miss sth)?

Thank you in advanced!

betterRunner avatar Feb 11 '21 10:02 betterRunner

I've had success in my personal projects using this plugin I made:

https://github.com/denn1s/vite-plugin-linaria

I'm open to collaborate and use it as a starting point for a more complete plugin down the line.

denn1s avatar Mar 25 '21 19:03 denn1s

Prepeding a forward slash to the filename fixes the problem with Vite: https://github.com/callstack/linaria/blob/20e1ba5f60d06b2399bd54d723be91364b8acea3/packages/rollup/src/index.ts#L52

Namely it should be like below for the plugin to work.

const filename = `/${id.replace(/\.js$/, '')}_${slug}.css`;

mfpopa avatar May 23 '21 20:05 mfpopa

Now that this PR merged, @linaria/rollup looks to works with Vite.

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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh(), pluginLinaria()],
});

tyankatsu0105 avatar Sep 03 '21 13:09 tyankatsu0105

Sorry. Not release yet.

tyankatsu0105 avatar Sep 03 '21 13:09 tyankatsu0105

While @linaria/rollup works for development, in my case it does break when attempting to build with:

vite v2.7.11 building for production...
✓ 0 modules transformed.
[linaria] /Users/olivier/Projects/foo/foo-webapp/index.html: Unexpected token (1:0)

> 1 | <!DOCTYPE html>

Using "^3.0.0-beta.15"

Anyone found a workaround?

mgcrea avatar Jan 13 '22 15:01 mgcrea

@mgcrea same issue for me.

I fixed it by excluding .html files

plugins: [
    linaria({
        exclude: ["**/*.html"],
    }),
]

Platane avatar Apr 21 '22 15:04 Platane

@Platane can you share a working repository with Vite & Linaria? Thank yuou

glekner avatar Jul 29 '22 13:07 glekner

@glekner you can find an example in examples/vite

Anber avatar Jul 29 '22 14:07 Anber

@betterRunner did you fix the error? I'm trying to setup new React TS Vite project and I would like to use linaria but I can't make it working. I was following the https://github.com/callstack/linaria/tree/master/examples/vite but when I start the dev server I get this error:

yarn run v1.22.19
$ vite
failed to load config from /Users/claudiu.cojocaru/Development/thinkOnline_bellway/vite.config.ts
error when starting dev server:
TypeError: linaria is not a function
    at file:///.../vite.config.ts.timestamp-1662464867437.mjs:12:10
    at loadConfigFromFile (file:///.../node_modules/vite/dist/node/chunks/dep-665b0112.js:63308:15)
    at async resolveConfig (file:///.../node_modules/vite/dist/node/chunks/dep-665b0112.js:62904:28)
    at async createServer (file:///.../node_modules/vite/dist/node/chunks/dep-665b0112.js:61978:20)
    at async CAC.<anonymous> (file:///.../node_modules/vite/dist/node/cli.js:700:24)
error Command failed with exit code 1.

cojoclaudiu avatar Sep 06 '22 11:09 cojoclaudiu

@cojoclaudiu I run into this problem too. If your project is of type: module in your package.json it won't import @linaria/rollup correctly. It actually ends up importing an object like this:

{ default: <this is the actual plugin function> }

You might be able to remove type: module from your package.json or invoke linariaPlugin.default() instead.

blazzy avatar Oct 09 '22 20:10 blazzy

Do you have a working sandbox? To be honest I gave up using linaria because I had to move on with the project. But I'm looking forward to use it for the future projects.

cojoclaudiu avatar Oct 09 '22 20:10 cojoclaudiu

@cojoclaudiu Hi there, sorry I haven't followed up on this issue for a while so I can't provide more details. Also waiting for updates.

betterRunner avatar Oct 21 '22 05:10 betterRunner

@betterRunner it should work now. Could you please check?

Anber avatar Oct 21 '22 08:10 Anber