介绍
Astro
是一个基于JavaScript
的静态站点生成器, 和Hexo
有点类似, 支持Markdown
构建内容, 但Astro
性能会更好, 它具有独特的岛屿架构,按需加载JS
,能显著减少初始加载时间, 非常适合需要快速首屏加载的场景比如如博客、营销网站、企业官网等等
Astro
官网: 点击进入
环境搭建
这里以MacOs
平台为例:
安装
npm
1
brew install npm
安装
nodejs
这里指定
nodejs
版本为v181
npm install -g n
然后
1
n v18
开始
创建工程模板
1
npm create astro@latest 工程名称
根据提示一步步往下走即可
完成后会在当前目录生成工程文件夹, 目录如下:
运行工程
1
npm run dev
开启调试, 此时在浏览器地址栏输入
http://localhost:4321/
即可实时访问打包静态文件
工程编写完毕后, 即可输出网站静态文件
1
npm run build
执行完毕后, 在工程目录下会自动生成
dist
文件夹将
dist
目录下的所有文件上传到你的静态服务器上就可访问了
Astro与React结合
Astro
不仅可以和vue
结合使用 还可以和React
结合, 这里做一个简单Tailwind
风格的网站演示
创建项目
1
npm create astro@latest my-toolbox -- --template framework-react
安装依赖
1
2
3cd my-toolbox
npm install @astrojs/tailwind tailwindcss react-dnd react-dnd-html5-backend初始化
Tailwind
1
npx tailwindcss init -p
配置
Tailwind.config.js
1
2
3
4
5
6
7export default {
content: ['./src/*/.{astro,js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [],
}配置
astro.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
import tailwind from '@astrojs/tailwind';
export default defineConfig({
integrations: [
react(),
tailwind({
config: {
applyBaseStyles: false,
},
}),
],
server: {
port: 3000,
},
build: {
format: 'file',
},
});编写主页
工作流页面
工具注册中心
工程目录结构介绍
以下是示例
1 | src/ |
本文为作者原创 转载时请注明出处 谢谢