diff --git a/.changeset/tiny-falcons-lie.md b/.changeset/tiny-falcons-lie.md new file mode 100644 index 000000000..045b1ee13 --- /dev/null +++ b/.changeset/tiny-falcons-lie.md @@ -0,0 +1,5 @@ +--- +"@farmfe/core": patch +--- + +Fix invalid css syntax #1748 #1557 diff --git a/crates/plugin_css/src/lib.rs b/crates/plugin_css/src/lib.rs index 2890943b1..024a8beb4 100644 --- a/crates/plugin_css/src/lib.rs +++ b/crates/plugin_css/src/lib.rs @@ -284,10 +284,7 @@ impl Plugin for FarmPluginCss { comments, } = parse_css_stylesheet( &css_modules_module_id.to_string(), - Arc::new( - // replace --: '' to --farm-empty: '' - param.content.replace("--:", "--farm-empty:"), - ), + Arc::new(param.content.clone()), )?; // js code for css modules @@ -424,11 +421,9 @@ impl Plugin for FarmPluginCss { .remove(¶m.module_id.to_string()) .unwrap_or_else(|| panic!("ast not found {:?}", param.module_id.to_string())) } else { - let ParseCssModuleResult { ast, comments } = parse_css_stylesheet( - ¶m.module_id.to_string(), - // replace --: '' to --farm-empty: '' - Arc::new(param.content.replace("--:", "--farm-empty:")), - )?; + // swc_css_parser does not support + let ParseCssModuleResult { ast, comments } = + parse_css_stylesheet(¶m.module_id.to_string(), param.content.clone())?; (ast, CommentsMetaData::from(comments)) }; diff --git a/crates/plugin_css/tests/fixtures/analyze_deps/basic.css b/crates/plugin_css/tests/fixtures/analyze_deps/basic.css index b3356af68..a5adbe0f2 100644 --- a/crates/plugin_css/tests/fixtures/analyze_deps/basic.css +++ b/crates/plugin_css/tests/fixtures/analyze_deps/basic.css @@ -19,4 +19,7 @@ div { p { background: url('@/img/logo.png'); + top: -8px/2 + 1; } + +.home {filter: progid:DXImageTransform.Microsoft.Alpha(opacity=20)} diff --git a/crates/toolkit/src/css/mod.rs b/crates/toolkit/src/css/mod.rs index 4fd34b9a2..545319a36 100644 --- a/crates/toolkit/src/css/mod.rs +++ b/crates/toolkit/src/css/mod.rs @@ -2,6 +2,7 @@ use std::{path::PathBuf, sync::Arc}; use farmfe_core::{ error::CompilationError, + regex::Regex, swc_common::{comments::SingleThreadedComments, input::SourceFileInput}, swc_css_ast::Stylesheet, }; @@ -25,11 +26,23 @@ pub struct ParseCssModuleResult { /// parse the input css file content to [Stylesheet] pub fn parse_css_stylesheet( id: &str, - content: Arc, + orig_content: Arc, ) -> farmfe_core::error::Result { + // swc_css_parser does not support parsing invalid css, so we need to replace known invalid css here + // 1. replace --: '' to --farm-empty: '' + let mut content = orig_content.replace("--:", "--farm-empty:"); + // 2. replace filter: xxx.Microsoft.xxx to filter: "xxx.Microsoft.xxx" using regex. fix #1557 + let regex = Regex::new(r#"filter:\s*(.*?)\.Microsoft\.(.*?)(;|\})"#).unwrap(); + content = regex + .replace_all(&content, "filter:\"$1.Microsoft.$2\"$3") + .to_string(); + // 3. replace invalid operator, eg: top: -8px/2 + 1 to top: "-8px/2 + 1" using regex. fix #1748 + let regex = Regex::new(r#":\s*([^;{}]*?\d\s(?:\+|-|\*|/)\s\d[^;{}]*?)\s*(;|\})"#).unwrap(); + content = regex.replace_all(&content, ":\"$1\"$2").to_string(); + let (cm, source_file) = create_swc_source_map(Source { path: PathBuf::from(id), - content, + content: Arc::new(content), }); let config = ParserConfig { diff --git a/examples/invalid-css-issue-1557/farm.config.ts b/examples/invalid-css-issue-1557/farm.config.ts new file mode 100644 index 000000000..c349e0675 --- /dev/null +++ b/examples/invalid-css-issue-1557/farm.config.ts @@ -0,0 +1,5 @@ +import { defineConfig } from '@farmfe/core'; + +export default defineConfig({ + plugins: ['@farmfe/plugin-react'], +}); diff --git a/examples/invalid-css-issue-1557/index.html b/examples/invalid-css-issue-1557/index.html new file mode 100644 index 000000000..56d5a1bdb --- /dev/null +++ b/examples/invalid-css-issue-1557/index.html @@ -0,0 +1,14 @@ + + + + + + + + Farm + React + TS + + +
+ + + \ No newline at end of file diff --git a/examples/invalid-css-issue-1557/package.json b/examples/invalid-css-issue-1557/package.json new file mode 100644 index 000000000..133ef446d --- /dev/null +++ b/examples/invalid-css-issue-1557/package.json @@ -0,0 +1,28 @@ +{ + "name": "@farmfe-examples/invalid-css-issue-1557", + "version": "1.0.0", + "private": true, + "scripts": { + "dev": "farm start", + "start": "farm start", + "build": "farm build", + "preview": "farm preview", + "clean": "farm clean" + }, + "dependencies": { + "dhtmlx-gantt": "^8.0.9", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-highlight": "^0.15.0", + "react-highlight-words": "^0.20.0" + }, + "devDependencies": { + "@farmfe/cli": "^1.0.2", + "@farmfe/core": "^1.2.8", + "@farmfe/plugin-react": "^1.1.0", + "@types/react": "18", + "@types/react-dom": "18", + "core-js": "^3.36.1", + "react-refresh": "^0.14.0" + } +} diff --git a/examples/invalid-css-issue-1557/src/assets/logo.png b/examples/invalid-css-issue-1557/src/assets/logo.png new file mode 100644 index 000000000..0caeb4381 Binary files /dev/null and b/examples/invalid-css-issue-1557/src/assets/logo.png differ diff --git a/examples/invalid-css-issue-1557/src/assets/react.svg b/examples/invalid-css-issue-1557/src/assets/react.svg new file mode 100644 index 000000000..6c87de9bb --- /dev/null +++ b/examples/invalid-css-issue-1557/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/invalid-css-issue-1557/src/componnets/HighlightKeyword.tsx b/examples/invalid-css-issue-1557/src/componnets/HighlightKeyword.tsx new file mode 100644 index 000000000..c16436c65 --- /dev/null +++ b/examples/invalid-css-issue-1557/src/componnets/HighlightKeyword.tsx @@ -0,0 +1,18 @@ +import Highlighter from 'react-highlight-words'; + +// import Highlighter from 'react-highlight-words/dist/main.js'; +interface Props { + text?: string; +} +function HighlightKeyword({ text }: Props) { + return ( + + ); +} + +export default HighlightKeyword; diff --git a/examples/invalid-css-issue-1557/src/index.css b/examples/invalid-css-issue-1557/src/index.css new file mode 100644 index 000000000..6cc4daf98 --- /dev/null +++ b/examples/invalid-css-issue-1557/src/index.css @@ -0,0 +1,69 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #9f1a8f; + text-decoration: inherit; +} +a:hover { + color: #9f1a8f; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #9f1a8f; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #9F1A8F; + } + button { + background-color: #f9f9f9; + } +} diff --git a/examples/invalid-css-issue-1557/src/index.tsx b/examples/invalid-css-issue-1557/src/index.tsx new file mode 100644 index 000000000..dde475a7e --- /dev/null +++ b/examples/invalid-css-issue-1557/src/index.tsx @@ -0,0 +1,9 @@ +import React from 'react'; +import { render } from 'react-dom'; +import { Main } from './main'; +import './index.css' + + +const container = document.querySelector('#root'); + +render(
,container); diff --git a/examples/invalid-css-issue-1557/src/main.css b/examples/invalid-css-issue-1557/src/main.css new file mode 100644 index 000000000..7a0494a5f --- /dev/null +++ b/examples/invalid-css-issue-1557/src/main.css @@ -0,0 +1,46 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: filter 300ms; +} +.logo:hover { + filter: drop-shadow(0 0 2em #9F1A8Faa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +@media (prefers-reduced-motion: no-preference) { + a:nth-of-type(2) .logo { + animation: logo-spin infinite 20s linear; + } +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} + +.keyword-highlight { + color: red; +} diff --git a/examples/invalid-css-issue-1557/src/main.tsx b/examples/invalid-css-issue-1557/src/main.tsx new file mode 100644 index 000000000..71e62caf8 --- /dev/null +++ b/examples/invalid-css-issue-1557/src/main.tsx @@ -0,0 +1,37 @@ +import React, { useState } from "react"; +import "./main.css"; +import reactLogo from "./assets/react.svg"; +import FarmLogo from "./assets/logo.png"; +import 'dhtmlx-gantt/codebase/dhtmlxgantt.css'; + +import HighlightKeyword from "./componnets/HighlightKeyword.tsx"; + +export function Main() { + const [count, setCount] = useState(0); + + return ( + <> + + +

Farm + React

+
+ +

+ Edit src/main.tsx and save to test HMR +

+
+

+ Click on the Farm and React logos to learn more +

+ + ); +} diff --git a/examples/invalid-css-issue-1557/src/typings.d.ts b/examples/invalid-css-issue-1557/src/typings.d.ts new file mode 100644 index 000000000..fa0a2da54 --- /dev/null +++ b/examples/invalid-css-issue-1557/src/typings.d.ts @@ -0,0 +1,3 @@ +declare module '*.svg'; +declare module '*.png'; +declare module '*.css'; diff --git a/examples/invalid-css-issue-1557/tsconfig.json b/examples/invalid-css-issue-1557/tsconfig.json new file mode 100644 index 000000000..aeb9f2d60 --- /dev/null +++ b/examples/invalid-css-issue-1557/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "lib": ["DOM", "DOM.Iterable", "ESNext"], + "allowJs": false, + "skipLibCheck": true, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "Node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + }, + "include": ["src"], + "references": [{ "path": "./tsconfig.node.json" }] +} \ No newline at end of file diff --git a/examples/invalid-css-issue-1557/tsconfig.node.json b/examples/invalid-css-issue-1557/tsconfig.node.json new file mode 100644 index 000000000..8aba60b03 --- /dev/null +++ b/examples/invalid-css-issue-1557/tsconfig.node.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "composite": true, + "module": "ESNext", + "moduleResolution": "Node", + "allowSyntheticDefaultImports": true + }, + "include": ["farm.config.ts"] +} diff --git a/examples/vite-adapter-vue/package.json b/examples/vite-adapter-vue/package.json index 599f28d68..0e8290637 100644 --- a/examples/vite-adapter-vue/package.json +++ b/examples/vite-adapter-vue/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "@sinclair/typebox": "^0.32.33", + "@opentiny/vue": "^3.18.0", "ant-design-vue": "3", "axios": "^1.4.0", "bcryptjs": "^2.4.3", diff --git a/examples/vite-adapter-vue/src/App.vue b/examples/vite-adapter-vue/src/App.vue index bda65101a..bef13316e 100644 --- a/examples/vite-adapter-vue/src/App.vue +++ b/examples/vite-adapter-vue/src/App.vue @@ -2,6 +2,11 @@
+ + + Tiny Vue Modal 最大化显示 + + @@ -10,6 +15,12 @@