Skip to content

Instantly share code, notes, and snippets.

@isaltyfish
Created February 14, 2025 03:00
Show Gist options
  • Save isaltyfish/b9cae38e67dd9a5e6c36e36c091e4d36 to your computer and use it in GitHub Desktop.
Save isaltyfish/b9cae38e67dd9a5e6c36e36c091e4d36 to your computer and use it in GitHub Desktop.
vite lib template script
#! /bin/bash
function green() {
echo -e "\e[32m$1\e[0m"
}
function red() {
echo -e "\e[31m$1\e[0m"
}
read -p "输入项目名称: " project_name
echo "当前目录是: $PWD"
read -p "是否在当前目录下创建项目?(y/n): " confirm
if [ $confirm == "y" ]; then
mkdir $project_name
cd $project_name
else
exit 1
fi
packagejson='{
"name": "$str-project-name",
"version": "1.0.0",
"description": "",
"main": "dist/$str-project-name.umd.js",
"module": "dist/$str-project-name.es.js",
"exports": {
".": {
"import": "./dist/$str-project-name.js",
"require": "./dist/$str-project-name.umd.js"
}
},
"files": [
"dist"
],
"scripts": {
"test": "vitest",
"dev": "vite",
"build": "vite build"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@babel/register": "^7.25.9",
"vite": "^5.4.11",
"vitest": "^2.1.8"
}
}'
packagejson=$(echo "$packagejson" | sed "s/\$str-project-name/$project_name/g")
echo "$packagejson" > package.json
mkdir lib && touch lib/$project_name.js
viteconf='
import { defineConfig } from "vite";
export default defineConfig({
build: {
lib: {
entry: "lib/$str-project-name.js",
name: "$str-project-name",
fileName: (format) => `$str-project-name.${format}.js`,
},
outDir: "dist",
},
});'
viteconf=$(echo "$viteconf" | sed "s/\$str-project-name/$project_name/g")
echo "$viteconf" > vite.config.js
babelrc='{
"presets": ["@babel/preset-env"]
}'
echo "$babelrc" > .babelrc
green "项目创建成功!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment