macaron-css / macaron

Compiler-augmented typesafe CSS-in-JS with zero runtime, colocation, maximum safety and productivity
https://macaron.js.org/
MIT License
735 stars 16 forks source link

vite lib mode/vite-dts build errors #14

Closed zwgnr closed 1 year ago

zwgnr commented 1 year ago

I am currently in the processes of porting over a design system from Vanilla Extract to Macaron.

I was compiling with Vite in Lib mode using vite-dts in Vanilla Extract and everything was working fine. When using Macaron, the builds are failing.

viteissue

Here's the vite.config

import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { peerDependencies } from './package.json';
import { macaronVitePlugin } from '@macaron-css/vite';

const externals = [...Object.keys(peerDependencies)];

export default defineConfig({
  build: {
    lib: {
      entry: 'src/index.ts',
      fileName: 'index',
      formats: ['cjs', 'es'],
    },
    rollupOptions: {
      external: externals,
    },
  },
  plugins: [
    macaronVitePlugin(),
    dts({
      beforeWriteFile: (filePath, content) => ({
        content,
        filePath: filePath.replace('src', ''),
      }),
      compilerOptions: {
        baseUrl: './src/',
        emitDeclarationOnly: true,
        noEmit: false,
      },
      exclude: ['src/**/*.stories.tsx'],
      outputDir: 'dist/types',
    }),
  ],
});

FWIW, I was able to get builds working by removing vite-dts from the vite.config, building, and then running a separate build step with tsc and the following flags "tsc --emitDeclarationOnly --declaration --declarationDir dist/types".

Mokshit06 commented 1 year ago

I believe this is something that's occurring because extracted_*.css files don't actually exist in the file system. They are virtual and are created and resolved by macaron's bundler plugins, but typescript is unable to resolve them. This isn't something that can be fixed since we can't make ts resolve the virtual files, so removing vite-dts and running tsc as a different process is probably the only way to make it work