var fs = require('fs');
const writeFileRecursive = function(path, buffer, callback){
let lastPath = path.substring(0, path.lastIndexOf("/"));
fs.mkdir(lastPath, {recursive: true}, (err) => {
if (err) return callback(err);
fs.writeFile(path, buffer, function(err){
if (err) return callback(err);
return callback(null);
});
});
}
var files = {
"./vite.config.js":`
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue2'
export default defineConfig({
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
host: '0.0.0.0',
port:'10086',
proxy: {
'/api': {
target: 'http://example.com', // 代理目标的基础路径
changeOrigin: true, // 更改请求头中的 Origin 字段
rewrite: (path) => path.replace(/^\/api/, '') // 重写请求路径
}
}
}
})
`,
"./package.json":`
{
"name": "vue2-project",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^2.7.16"
},
"devDependencies": {
"@vitejs/plugin-vue2": "^2.3.1",
"vite": "^5.3.1"
}
}
`,
"./index.html":`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
`,
"src/main.js":`
import Vue from 'vue';
import App from './App.vue';
Vue.config.productionTip = false;
new Vue({ render: h => h(App) }).$mount('#app');
`,
"src/App.vue":`
<template>
<div>vue2</div>
</template>
`,
}
for (const key in files) {
writeFileRecursive(key, files[key] ,(err)=>{
if(err) console.error(err);
console.info("write success");
});
}