Skip to content

Commit

Permalink
fix: invalid css #1748 and #1557 (#1823)
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 authored Oct 12, 2024
1 parent 728707b commit a2a32f5
Show file tree
Hide file tree
Showing 20 changed files with 8,873 additions and 5,446 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-falcons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

Fix invalid css syntax #1748 #1557
13 changes: 4 additions & 9 deletions crates/plugin_css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -424,11 +421,9 @@ impl Plugin for FarmPluginCss {
.remove(&param.module_id.to_string())
.unwrap_or_else(|| panic!("ast not found {:?}", param.module_id.to_string()))
} else {
let ParseCssModuleResult { ast, comments } = parse_css_stylesheet(
&param.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(&param.module_id.to_string(), param.content.clone())?;

(ast, CommentsMetaData::from(comments))
};
Expand Down
3 changes: 3 additions & 0 deletions crates/plugin_css/tests/fixtures/analyze_deps/basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ div {

p {
background: url('@/img/logo.png');
top: -8px/2 + 1;
}

.home {filter: progid:DXImageTransform.Microsoft.Alpha(opacity=20)}
17 changes: 15 additions & 2 deletions crates/toolkit/src/css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -25,11 +26,23 @@ pub struct ParseCssModuleResult {
/// parse the input css file content to [Stylesheet]
pub fn parse_css_stylesheet(
id: &str,
content: Arc<String>,
orig_content: Arc<String>,
) -> farmfe_core::error::Result<ParseCssModuleResult> {
// 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 {
Expand Down
5 changes: 5 additions & 0 deletions examples/invalid-css-issue-1557/farm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@farmfe/core';

export default defineConfig({
plugins: ['@farmfe/plugin-react'],
});
14 changes: 14 additions & 0 deletions examples/invalid-css-issue-1557/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<title>Farm + React + TS</title>
</head>
<body>
<div id="root"></div>
<script src="./src/index.tsx"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions examples/invalid-css-issue-1557/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/invalid-css-issue-1557/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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 (
<Highlighter
highlightClassName="YourHighlightClass"
searchWords={["and", "or", "the"]}
autoEscape={true}
textToHighlight="The dog is chasing the cat. Or perhaps they're just playing?"
/>
);
}

export default HighlightKeyword;
69 changes: 69 additions & 0 deletions examples/invalid-css-issue-1557/src/index.css
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions examples/invalid-css-issue-1557/src/index.tsx
Original file line number Diff line number Diff line change
@@ -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(<Main />,container);
46 changes: 46 additions & 0 deletions examples/invalid-css-issue-1557/src/main.css
Original file line number Diff line number Diff line change
@@ -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;
}
37 changes: 37 additions & 0 deletions examples/invalid-css-issue-1557/src/main.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<HighlightKeyword></HighlightKeyword>
<div>
<a href="https://farmfe.org/" target="_blank">
<img src={FarmLogo} className="logo" alt="Farm logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Farm + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/main.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Farm and React logos to learn more
</p>
</>
);
}
3 changes: 3 additions & 0 deletions examples/invalid-css-issue-1557/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '*.svg';
declare module '*.png';
declare module '*.css';
21 changes: 21 additions & 0 deletions examples/invalid-css-issue-1557/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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" }]
}
9 changes: 9 additions & 0 deletions examples/invalid-css-issue-1557/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["farm.config.ts"]
}
1 change: 1 addition & 0 deletions examples/vite-adapter-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions examples/vite-adapter-vue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<div>
<img src="/logo.png" alt="">
<test />

<TinyButton type="primary" @click="btnClick">
Tiny Vue Modal 最大化显示
</TinyButton>

<test1 />
<aboute />
<home/>
Expand All @@ -10,6 +15,12 @@
</template>

<script lang="ts" setup>
import { Button as TinyButton, Modal } from '@opentiny/vue'
function btnClick() {
Modal.alert({ message: '最大化显示', fullscreen: true })
}
import test1 from './components/test1.vue';
import test from './components/test.vue';
import home from './pages/index.vue';
Expand Down
Loading

0 comments on commit a2a32f5

Please sign in to comment.