2 Star 0 Fork 0

thingjs-code-temp/prefab-template

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vite.config.js 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
import fs from 'fs'
import path from 'path'
import { defineConfig } from 'vite'
import serveStatic from 'serve-static'
export default defineConfig(async ({ mode }) => {
// const {
// default: { vitePrefabPlugin },
// } = await import('@thing.js/cli-p-prefab')
const { vitePrefabPlugin } = await import('@thing.js/cli-p-prefab')
return {
plugins: [
startLocalServe(),
transformHtml(),
mode === 'development' &&
vitePrefabPlugin({
input: 'src',
outputDir: 'dist',
watch: true,
reloadAfterBuild: true,
reloadFilter: ({ file, type }) => {
return path.basename(file) === 'prefab.json' && type !== 'unlink'
},
}),
],
}
})
/**
* Start packaging prefab
*/
function startLocalServe() {
return {
name: 'local-serve',
async configureServer(server) {
const app = server.middlewares
app.use(serveStatic('dist'))
app.use('/local-api/export-data', (req, res) => {
const data = []
req.on('data', (chunk) => {
data.push(chunk)
})
req.on('end', () => {
try {
let newObj = JSON.parse(Buffer.concat(data).toString())
let result = {}
const tsfPath = path.resolve(__dirname, 'src/prefab.json')
if (fs.existsSync(tsfPath)) {
try {
const oldObj = JSON.parse(fs.readFileSync(tsfPath, 'utf8'))
const headers = [
'name',
'id',
'version',
'author',
'description',
]
// Place the old header in front
for (const key in oldObj) {
if (
headers.includes(key) &&
typeof oldObj[key] !== 'undefined'
) {
result[key] = oldObj[key]
}
}
} catch (e) {
console.error(e)
}
}
for (const key in newObj) {
result[key] = newObj[key]
}
fs.writeFileSync(tsfPath, JSON.stringify(result, null, 2))
res.end()
} catch (e) {
console.log(e)
}
})
})
},
}
}
/**
* Inject export script to index.html
*/
function transformHtml() {
return {
name: 'transform-html',
transformIndexHtml() {
return [
{
tag: 'script',
attrs: {
type: 'module',
},
children: `
import exportData from "./dist/index.js";
fetch('/local-api/export-data', {method: 'POST', body: JSON.stringify(exportData(), null, 2)})
`,
injectTo: 'body',
},
]
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/thingjs-code-temp/prefab-template.git
[email protected]:thingjs-code-temp/prefab-template.git
thingjs-code-temp
prefab-template
prefab-template
master

搜索帮助