npm install vue --save
npm install @vitejs/plugin-vue vite --save-dev
vite.config.js
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
});
package.json
{
"name": "vite2-prepare",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.0.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "^1.2.4",
"vite": "^2.4.0"
}
}
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 { createApp } from "vue";
import App from "./App.vue";
createApp(App).mount("#app");
src\App.vue
<template>
<h1>App</h1>
</template>
npm install esbuild
console.log("title");
require("esbuild").buildSync({
entryPoints: ["main.js"],
outfile: "out.js",
});
加载器
,它告诉 esbuild 如何解释文件内容。默认情况下,某些文件扩展名已经为它们配置了加载器,尽管可以覆盖默认值let title = <h1>hello</h1>;
console.log("title");
require("esbuild").buildSync({
entryPoints: ["main.js"],
bundle: true,
loader: { ".js": "jsx" },
outfile: "out.js",
});
setup
函数在每次BUILD API
调用时都会运行一次import { Path } from "env";
console.log(`Path is ${Path}`);
let envPlugin = {
name: "env",
setup(build) {
//拦截名为env的导入路径,以便esbuild不会尝试将它们映射到文件系统位置
//用env-ns名称空间标记它们,以便为该插件保留它们
build.onResolve({ filter: /^env$/ }, (args) => ({
path: args.path,
namespace: "env-ns",
}));
//加载带有env-ns名称空间标记的路径,它们的行为就像指向包含环境变量的JSON文件一样
build.onLoad({ filter: /.*/, namespace: "env-ns" }, () => ({
contents: JSON.stringify(process.env),
loader: "json",
}));
},
};
require("esbuild")
.build({
entryPoints: ["entry.js"],
bundle: true,
outfile: "out.js",
plugins: [envPlugin],
})
.catch(() => process.exit(1));