diff --git a/docs/src/pages/guides/styling.md b/docs/src/pages/guides/styling.md index 87f95a767029..98df62b8aa0d 100644 --- a/docs/src/pages/guides/styling.md +++ b/docs/src/pages/guides/styling.md @@ -124,10 +124,10 @@ It’s recommended to only use this in scenarios where a `` tag won’t wo ## Autoprefixer -[Autoprefixer][autoprefixer] takes care of cross-browser CSS compatibility for you. Use it in astro by installing it (`npm install --save-dev autoprefixer`) and adding a `postcss.config.js` file to the root of your project: +[Autoprefixer][autoprefixer] takes care of cross-browser CSS compatibility for you. Use it in astro by installing it (`npm install --save-dev autoprefixer`) and adding a `postcss.config.cjs` file to the root of your project: ```js -// postcss.config.js +// postcss.config.cjs module.exports = { autoprefixer: { /* (optional) autoprefixer settings */ @@ -139,7 +139,7 @@ _Note: Astro v0.21 and later requires this manual setup for autoprefixer. Previo ## PostCSS -You can use any PostCSS plugin by adding a `postcss.config.js` file to the root of your project. Follow the documentation for the plugin you’re trying to install for configuration and setup. +You can use any PostCSS plugin by adding a `postcss.config.cjs` file to the root of your project. Follow the documentation for the plugin you’re trying to install for configuration and setup. --- @@ -208,10 +208,10 @@ Astro can be configured to use [Tailwind][tailwind] easily! Install the dependen npm install --save-dev tailwindcss ``` -And create 2 files in your project root: `tailwind.config.js` and `postcss.config.js`: +And create 2 files in your project root: `tailwind.config.cjs` and `postcss.config.cjs`: ```js -// tailwind.config.js +// tailwind.config.cjs module.exports = { mode: 'jit', purge: ['./public/**/*.html', './src/**/*.{astro,js,jsx,svelte,ts,tsx,vue}'], @@ -220,7 +220,7 @@ module.exports = { ``` ```js -// postcss.config.js +// postcss.config.cjs module.exports = { tailwind: {}, }; @@ -250,7 +250,7 @@ As of [version 0.20.0](https://github.com/snowpackjs/astro/releases/tag/astro%40 ### 🎭 PostCSS -Using PostCSS is as simple as placing a [`postcss.config.js`](https://github.com/postcss/postcss#usage) file in the root of your project. +Using PostCSS is as simple as placing a [`postcss.config.cjs`](https://github.com/postcss/postcss#usage) file in the root of your project. Be aware that this plugin will run on all CSS in your project, including any files that compiled to CSS (like `.scss` Sass files, for example). diff --git a/packages/astro/package.json b/packages/astro/package.json index 2514a05b4d88..c081959d2bad 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -73,7 +73,6 @@ "estree-util-value-to-estree": "^1.2.0", "fast-xml-parser": "^3.19.0", "html-entities": "^2.3.2", - "htmlparser2": "^7.1.2", "kleur": "^4.1.4", "mime": "^2.5.2", "morphdom": "^2.6.1", diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 2ba951de0e5e..3fa7f6365a33 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -8,7 +8,7 @@ import { fileURLToPath } from 'url'; import { transform } from '@astrojs/compiler'; import { decode } from 'sourcemap-codec'; import { AstroDevServer } from '../core/dev/index.js'; -import { preprocessStyle } from './styles.js'; +import { getViteTransform, TransformHook, transformWithVite } from './styles.js'; interface AstroPluginOptions { config: AstroConfig; @@ -17,12 +17,12 @@ interface AstroPluginOptions { /** Transform .astro files for Vite */ export default function astro({ config, devServer }: AstroPluginOptions): vite.Plugin { - let viteConfig: vite.ResolvedConfig; + let viteTransform: TransformHook; return { name: '@astrojs/vite-plugin-astro', enforce: 'pre', // run transforms before other plugins can configResolved(resolvedConfig) { - viteConfig = resolvedConfig; // gain access to vite:css + viteTransform = getViteTransform(resolvedConfig); }, // note: don’t claim .astro files with resolveId() — it prevents Vite from transpiling the final JS (import.meta.globEager, etc.) async load(id) { @@ -33,10 +33,6 @@ export default function astro({ config, devServer }: AstroPluginOptions): vite.P // everything else is treated as a fragment const isPage = id.startsWith(fileURLToPath(config.pages)) || id.startsWith(fileURLToPath(config.layouts)); let source = await fs.promises.readFile(id, 'utf8'); - - // preprocess styles before compiler runs - source = await preprocessStyle({ source, filePath: id, config, viteConfig }); - let tsResult: TransformResult | undefined; try { @@ -49,6 +45,11 @@ export default function astro({ config, devServer }: AstroPluginOptions): vite.P sourcefile: id, sourcemap: 'both', internalURL: 'astro/internal', + preprocessStyle: async (value: string, attrs: Record) => { + if (!attrs || !attrs.lang) return null; + const result = await transformWithVite(value, attrs, id, viteTransform); + return result; + }, }); // Compile `.ts` to `.js` const { code, map } = await esbuild.transform(tsResult.code, { loader: 'ts', sourcemap: 'external', sourcefile: id }); @@ -73,7 +74,8 @@ export default function astro({ config, devServer }: AstroPluginOptions): vite.P return devServer.handleHotUpdate(context); } }, - transformIndexHtml(html) { + transformIndexHtml() { + // note: this runs only in dev return [ { injectTo: 'head-prepend', diff --git a/packages/astro/src/vite-plugin-astro/styles.ts b/packages/astro/src/vite-plugin-astro/styles.ts index d4e4248f76ef..2794a3728b8c 100644 --- a/packages/astro/src/vite-plugin-astro/styles.ts +++ b/packages/astro/src/vite-plugin-astro/styles.ts @@ -1,130 +1,22 @@ import type vite from '../core/vite'; -import type { AstroConfig } from '../@types/astro-core'; -import htmlparser2 from 'htmlparser2'; - -interface StyleProcessOptions { - source: string; - filePath: string; - config: AstroConfig; - viteConfig: vite.ResolvedConfig; -} +export type TransformHook = (code: string, id: string, ssr?: boolean) => Promise; // https://vitejs.dev/guide/features.html#css-pre-processors const SUPPORTED_PREPROCESSORS = new Set(['scss', 'sass', 'styl', 'stylus', 'less']); -/** Given HTML, preprocess (Sass, etc.) */ -export async function preprocessStyle({ source, filePath, viteConfig }: StyleProcessOptions): Promise { - // crawl HTML for script tags - const styles = getStyleTags(source); - - // if no ` + html.substring(end + 1); - } - - return html; + return viteCSSPlugin.transform.bind(null as any) as any; } -/** Convert attr object to string */ -function stringAttrs(attrs: Record = {}) { - let output = ''; - for (const [k, v] of Object.entries(attrs)) { - if (!v) continue; - if (typeof v === 'string') { - output += ` ${k}="${v}"`; - } else { - output += ` ${k}`; - } - } - return output; -} - -interface StyleTag { - attrs: Record; - contents: string; - start: number; - end: number; -} - -/** Parse HTML with htmlparser2 to return is encountered, take everything stored and save it - onclosetag(tagname) { - if (tagname === 'style') { - // skip empty