Vue项目Build后自动打包

1. 安装依赖

1
npm install filemanager-webpack-plugin

2. vue.config.js 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const FileManagerPlugin = require('filemanager-webpack-plugin')
...
// isProd 一般为环境判断 如 process.env.NODE_ENV === 'production'
isProd &&
plugins.push(
new FileManagerPlugin({
events: {
onEnd: {
// 首先先删除之前的文件
delete: ['./dist.zip'],
// 压缩 源->目的地
archive: [
{
source: path.join(__dirname, './dist'),
destination: path.join(__dirname, './dist.zip')
}
]
}
}
})
)
...
const vueConfig = {
...
configureWebpack: {
...
plugins,
...
}
...
}

3. filemanager-webpack-plugin 简易教程

Github

1. Options

1
2
3
4
5
6
7
8
new FileManagerPlugin({
events: {
onStart: {}, //打包前
onEnd: {}, //打包结束后
},
runTasksInSeries: false,
runOnceInWatchMode: false,
});

2. Actions

  • Copy
    • source[string] - 源(文件/目录/glob)
    • destination[string] - 文件/目录
    • options [‘object`] - copy options
      • flat: [Boolean]
      • preserveTimestamps: [Boolean]
      • overwite: [Boolean]
    • globOptions [object] - options to forward to glob options
  • Delete
  • Move
    • source[string] - a file or a directory or a glob
    • destination[string] - a file or a directory.
  • Mkdir
  • Archive
    • source[string] - a file or a directory or a glob
    • destination[string] - a file.
    • format[string] - Optional. Defaults to extension in destination filename.
    • options[object] - Refer https://www.archiverjs.com/archiver

3. 官方示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// webpack.config.js:

const FileManagerPlugin = require("filemanager-webpack-plugin");

export default {
// ...rest of the config
plugins: [
new FileManagerPlugin({
events: {
onEnd: {
copy: [
{ source: "/path/fromfile.txt", destination: "/path/tofile.txt" },
{ source: "/path/**/*.js", destination: "/path" },
],
move: [
{ source: "/path/from", destination: "/path/to" },
{ source: "/path/fromfile.txt", destination: "/path/tofile.txt" },
],
delete: ["/path/to/file.txt", "/path/to/directory/"],
mkdir: ["/path/to/directory/", "/another/directory/"],
archive: [
{ source: "/path/from", destination: "/path/to.zip" },
{ source: "/path/**/*.js", destination: "/path/to.zip" },
{ source: "/path/fromfile.txt", destination: "/path/to.zip" },
{
source: "/path/fromfile.txt",
destination: "/path/to.zip",
format: "tar",
},
{
source: "/path/fromfile.txt",
destination: "/path/to.tar.gz",
format: "tar",
options: {
gzip: true,
gzipOptions: {
level: 1,
},
globOptions: {
// https://github.com/Yqnn/node-readdir-glob#options
dot: true,
},
},
},
],
},
},
}),
],
};
作者

Bryan

发布于

2021-12-14

更新于

2023-10-06

许可协议