JavaScript/vite/组件构建:修订间差异
来自康健生活
< JavaScript | vite
| 第3行: | 第3行: | ||
== 多组件构建 == | == 多组件构建 == | ||
== 多文件单编译 == | == 多文件单编译 == | ||
设置输入文件的目录 | |||
{{hc|head=vite.config.ts|output=<syntaxhighlight lang="bash"> | {{hc|head=vite.config.ts|output=<syntaxhighlight lang="bash"> | ||
| 第19行: | 第22行: | ||
const absolutePath = file.slice(0, file.length - filenameWidthExt.length) | const absolutePath = file.slice(0, file.length - filenameWidthExt.length) | ||
const relativePath = absolutePath.replace(new RegExp(path.join(__dirname, 'src/'), 'g'), '') | const relativePath = absolutePath.replace(new RegExp(path.join(__dirname, 'src/'), 'g'), '') | ||
entries.set(path.join(relativePath, filename), fileURLToPath(new URL(file, import.meta.url))) | entries.set(path.join(relativePath, filename), fileURLToPath(new URL(file, import.meta.url))) | ||
| 第46行: | 第44行: | ||
emptyOutDir: true, | emptyOutDir: true, | ||
},</syntaxhighlight>}} | },</syntaxhighlight>}} | ||
执行 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
Insert comment here | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
npm warn config ignoring workspace config at /home/kwy/postlab-core/packages/postlab-platform/.npmrc | |||
> @postlab/platform@0.0.3 bundle | |||
> vite build --config=vite.config.bundle.ts | |||
vite v6.3.4 building for production... | |||
✓ 29 modules transformed. | |||
bundle/vite-envs.es.js 0.00 kB │ gzip: 0.02 kB | |||
// ... | |||
bundle/io/tauri/io.es.js 10.04 kB │ gzip: 3.31 kB | |||
bundle/vite-envs.cjs.js 0.01 kB │ gzip: 0.03 kB | |||
// ... | |||
bundle/io/tauri/io.cjs.js 3.90 kB │ gzip: 1.64 kB | |||
✓ built in 340ms | |||
</syntaxhighlight>|$|/home/kwy}} | |||
== tree shaking == | == tree shaking == | ||
2025年7月23日 (三) 12:15的版本
标题文本
单组件构建
多组件构建
多文件单编译
设置输入文件的目录
vite.config.ts
import path from 'path'
import * as glob from 'glob'
import { fileURLToPath } from "url"
const entries = new Map<string, string>()
const GLOB_PATH = path.resolve(__dirname, 'src/**/*.ts')
for (const file of glob.sync(GLOB_PATH)) {
const filenameWidthExt = file.split('/').at(-1)
const filename = filenameWidthExt?.split('.')[0]
if (!filename) continue
const absolutePath = file.slice(0, file.length - filenameWidthExt.length)
const relativePath = absolutePath.replace(new RegExp(path.join(__dirname, 'src/'), 'g'), '')
entries.set(path.join(relativePath, filename), fileURLToPath(new URL(file, import.meta.url)))
}
// Add any additional entries
// entries.set('index', './src/index.ts')
// in the defineConfig
build: {
lib: {
entry: Object.fromEntries(entries),
fileName: (format, entryName) => `${entryName}.${format}.js`,
formats: ['es', 'cjs'],
// fileName: '[name]'
},
rollupOptions: {
external: [],
},
target: 'esnext',
sourcemap: true,
emptyOutDir: true,
},
执行
[jian@kang /home/kwy]$
Insert comment here
npm warn config ignoring workspace config at /home/kwy/postlab-core/packages/postlab-platform/.npmrc
> @postlab/platform@0.0.3 bundle
> vite build --config=vite.config.bundle.ts
vite v6.3.4 building for production...
✓ 29 modules transformed.
bundle/vite-envs.es.js 0.00 kB │ gzip: 0.02 kB
// ...
bundle/io/tauri/io.es.js 10.04 kB │ gzip: 3.31 kB
bundle/vite-envs.cjs.js 0.01 kB │ gzip: 0.03 kB
// ...
bundle/io/tauri/io.cjs.js 3.90 kB │ gzip: 1.64 kB
✓ built in 340ms
tree shaking
在多组件或多文件的库中,保持此库支持 tree shaking 需要在 package.json 中设置 sideEffects。
package.json
{
"sideEffects": false,
}
此外可以设置 vite 配置文件 preserveModules 以便更好地支持 tree shaking。
vite.conf.ts
export default defineConfig({
build: {
output: {
preserveModules: true,
// ...
})
No categories