<

1.Vite #

1.1 安装依赖 #

npm install vue  --save
npm install  @vitejs/plugin-vue  vite --save-dev

1.2 配置文件 #

vite.config.js

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
});

1.3 package.json #

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"
  }
}

1.4 index.html #

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>

1.5 src\main.js #

src\main.js

import { createApp } from "vue";
import App from "./App.vue";
createApp(App).mount("#app");

1.6 src\App.vue #

src\App.vue

<template>
  <h1>App</h1>
</template>

2.esbuild 介绍 #

2.1 安装 #

npm install esbuild

2.2 使用 #

2.2.1 main.js #

console.log("title");

2.2.2 esbuild.js #

require("esbuild").buildSync({
  entryPoints: ["main.js"],
  outfile: "out.js",
});

2.3 内容类型 #

2.3.1 main.js #

let title = <h1>hello</h1>;
console.log("title");

2.3.2 esbuild.js #

require("esbuild").buildSync({
  entryPoints: ["main.js"],
  bundle: true,
  loader: { ".js": "jsx" },
  outfile: "out.js",
});

2.4 plugin #

2.4.1 命名空间 #

2.4.2 过滤器 #

2.4.3 Resolve 回调 #

2.4.4 Resolve 参数 #

2.4.5 onLoad 回调 #

2.4.6 onLoad 选项 #

2.4.7 load 结果 #

2.4.1 entry.js #

import { Path } from "env";
console.log(`Path is ${Path}`);

2.4.2 envPlugin #

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));