Files
web/packages/design-system/vite.config.ts
Rasmus Langvad d0546926a9 Merged in fix/3697-prettier-configs (pull request #3396)
fix(SW-3691): Setup one prettier config for whole repo

* Setup prettierrc in root and remove other configs


Approved-by: Joakim Jäderberg
Approved-by: Linus Flood
2026-01-07 12:45:50 +00:00

124 lines
3.7 KiB
TypeScript

/// <reference types="vitest" />
import { dirname, extname, relative, resolve } from "node:path"
import { fileURLToPath } from "node:url"
import fs from "node:fs"
import react from "@vitejs/plugin-react"
import { glob } from "glob"
import { defineConfig } from "vite"
import dts from "vite-plugin-dts"
import { libInjectCss } from "vite-plugin-lib-inject-css"
// import ensureDirectives from './plugins/rollup-plugin-ensure-directives'
import preserveDirectives from "rollup-preserve-directives"
// https://vitejs.dev/config/
export default defineConfig({
optimizeDeps: {
include: ["react/jsx-dev-runtime"],
},
plugins: [
react(),
libInjectCss(),
dts({
include: ["lib"],
exclude: [
"node_modules",
"**/*.css",
"**/*.test.ts",
"**/*.stories.tsx",
".storybook/content/*.mdx",
".storybook/content/tokens/**/*.mdx",
".storybook/content/tokens/**/*.css",
".storybook/content/tokens/**/*.tsx",
],
// rollupTypes: true,
bundledPackages: ["class-variance-authority", "clsx"],
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
preserveDirectives() as any,
],
build: {
cssCodeSplit: true,
copyPublicDir: false,
lib: {
entry: [resolve(__dirname, "lib/index.ts")],
formats: ["es"],
},
rollupOptions: {
plugins: [],
external: [
"react",
"react-dom",
"react/jsx-runtime",
"react-aria-components",
"react-hook-form",
"react-intl",
"next",
],
onwarn(warning, defaultHandler) {
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.message.includes("use client")
) {
return
}
// When using the directive 'use client' in components, rollup throws an error.
// See: https://github.com/vitejs/vite/issues/15012#issuecomment-1815854072
// We ignore sourcemap_error on the first character for component files
// to be caused by the 'use client' directive
if (
warning.code === "SOURCEMAP_ERROR" &&
warning.loc?.file &&
warning.loc.line === 1 &&
warning.loc.column === 0
) {
if (fs.statSync(warning.loc.file)) {
const file = fs.readFileSync(warning.loc.file, {
encoding: "utf-8",
})
const lines = file.split("\n")
if (lines[0] === "'use client'") {
return
}
}
}
defaultHandler(warning)
},
input: Object.fromEntries(
glob
.sync(["lib/**/*.{ts,tsx,css}"], {
ignore: ["**/types.ts", "**/*.stories.{ts,tsx}", "lib/tokens/*"],
})
.map((file) => {
return [
// The name of the entry point
// lib/nested/foo.ts becomes nested/foo
relative(
"lib",
file.slice(0, file.length - extname(file).length)
),
// The absolute path to the entry file
// lib/nested/foo.ts becomes /project/lib/nested/foo.ts
fileURLToPath(new URL(file, import.meta.url)),
]
})
),
output: {
assetFileNames: (assetInfo) => {
if (assetInfo.originalFileNames.length > 0) {
const originalFileName = assetInfo.originalFileNames[0]
if (originalFileName.indexOf("/components/") >= 0) {
return `${dirname(originalFileName).replace("lib/", "")}/[name]-[hash][extname]`
}
}
return `[name][extname]`
},
entryFileNames: "[name].js",
},
},
},
})