Skip to content

Commit

Permalink
fix: moduleDecls minify #1644 (#1650)
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 authored Jul 21, 2024
1 parent 4f700eb commit 5c603a6
Show file tree
Hide file tree
Showing 19 changed files with 1,054 additions and 147 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-vans-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

fix moduleDecls minify bug #1644
8 changes: 8 additions & 0 deletions crates/plugin_minify/src/imports_minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ impl<'a> VisitMut for IdentReplacer {
}
}

// fix #1644. Do not replace ident of member expression
fn visit_mut_member_prop(&mut self, n: &mut MemberProp) {
// ignore ident and private name of member expression
if let MemberProp::Computed(computed) = n {
computed.expr.visit_mut_with(self);
}
}

fn visit_mut_object_pat(&mut self, pat: &mut farmfe_core::swc_ecma_ast::ObjectPat) {
for n in &mut pat.props {
match n {
Expand Down
26 changes: 26 additions & 0 deletions examples/pandacss/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.sln
*.sw?

## Panda
styled-system
styled-system-studio
20 changes: 20 additions & 0 deletions examples/pandacss/farm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from '@farmfe/core';
import postcss from '@farmfe/js-plugin-postcss';
import * as path from "node:path";

export default defineConfig({
plugins: ['@farmfe/plugin-react', postcss()],
compilation: {
output: {
publicPath: "./"
},
sourcemap: false,
persistentCache: false,
resolve: {
alias: {
"@/": path.join(process.cwd(), "src"),
"@styled-system/": path.join(process.cwd(), "styled-system"),
},
},
}
});
14 changes: 14 additions & 0 deletions examples/pandacss/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>
29 changes: 29 additions & 0 deletions examples/pandacss/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@farmfe-examples/pandacss",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "farm start",
"start": "farm start",
"build": "farm build",
"preview": "farm preview",
"clean": "farm clean",
"prepare": "pandacss codegen"
},
"dependencies": {
"react": "18",
"react-dom": "18"
},
"devDependencies": {
"@farmfe/cli": "workspace:*",
"@farmfe/core": "workspace:*",
"@farmfe/js-plugin-postcss": "workspace:*",
"@farmfe/plugin-react": "workspace:*",
"@pandacss/dev": "^0.42.0",
"@types/react": "18",
"@types/react-dom": "18",
"core-js": "^3.37.1",
"postcss": "^8.4.39",
"react-refresh": "^0.14.2"
}
}
20 changes: 20 additions & 0 deletions examples/pandacss/panda.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from "@pandacss/dev";

export default defineConfig({
// Whether to use css reset
preflight: true,

// Where to look for your css declarations
include: ["./src/**/*.{js,jsx,ts,tsx}", "./pages/**/*.{js,jsx,ts,tsx}"],

// Files to exclude
exclude: [],

// Useful for theme customization
theme: {
extend: {},
},

// The output directory for your css system
outdir: "styled-system",
});
5 changes: 5 additions & 0 deletions examples/pandacss/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
plugins: {
'@pandacss/dev/postcss': {},
},
}
Binary file added examples/pandacss/public/favicon.ico
Binary file not shown.
Binary file added examples/pandacss/src/assets/logo.png
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/pandacss/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.
1 change: 1 addition & 0 deletions examples/pandacss/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@layer reset, base, tokens, recipes, utilities;
10 changes: 10 additions & 0 deletions examples/pandacss/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Main } from './main';
import './index.css'


const container = document.querySelector('#root');
const root = createRoot(container);

root.render(<Main />);
26 changes: 26 additions & 0 deletions examples/pandacss/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { useState } from "react";
import {css} from "@styled-system/css";
export function Main() {
const [count, setCount] = useState(0);

return (
<>
<h1>Farm + React</h1>
<div className={css({
bg: "red.500",
color: "white",
w: "md"
})}>
<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/pandacss/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';
32 changes: 32 additions & 0 deletions examples/pandacss/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,

"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@styled-system/*": ["./styled-system/*"]
},
"allowSyntheticDefaultImports": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/pandacss/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["farm.config.ts"]
}
56 changes: 35 additions & 21 deletions packages/core/src/server/middlewares/static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import path, { relative } from 'path';
import { Context, Middleware, Next } from 'koa';
import serve from 'koa-static';
import { Server } from '../index.js';
Expand All @@ -14,33 +14,47 @@ export function staticMiddleware(devServerContext: Server): Middleware {

// Fallback
const fallbackMiddleware: Middleware = async (ctx: Context, next: Next) => {
ctx.type = 'html';
ctx.body = fs.createReadStream(path.join(config.distDir, 'index.html'));
await next();

// If staticMiddleware doesn't find the file, try to serve index.html
if (ctx.status === 404 && !ctx.body) {
ctx.type = 'html';
ctx.body = fs.createReadStream(path.join(config.distDir, 'index.html'));
}
};

return async (ctx: Context, next: Next) => {
if (ctx.status !== 404 || ctx.body) {
await next();
return;
}

const requestPath = ctx.request?.path;
let modifiedPath = requestPath;

if (requestPath && requestPath.startsWith(config.output.publicPath)) {
const modifiedPath = requestPath.substring(
config.output.publicPath.length
);

ctx.request.path = `/${modifiedPath}`;

try {
// Serve middleware for static files
await staticMiddleware(ctx, async () => {
// If staticMiddleware doesn't find the file or refresh current page router, execute fallbackMiddleware
await fallbackMiddleware(ctx, next);
});
} catch (error) {
this.logger.error('Static file handling error:', error);
ctx.status = 500;
if (requestPath) {
if (config.output.publicPath.startsWith('/')) {
modifiedPath = requestPath.substring(config.output.publicPath.length);
} else {
const publicPath = relative(
path.join(config.distDir, config.output.publicPath),
config.distDir
);
modifiedPath = requestPath.substring(publicPath.length + 1);
}
} else {
await next();
}

ctx.request.path = `/${modifiedPath}`;

try {
// Serve middleware for static files
await staticMiddleware(ctx, async () => {
// If staticMiddleware doesn't find the file or refresh current page router, execute fallbackMiddleware
await fallbackMiddleware(ctx, next);
});
} catch (error) {
devServerContext.logger.error('Static file handling error:', error);
ctx.status = 500;
}
};
}
Loading

0 comments on commit 5c603a6

Please sign in to comment.