Vue -- webpack 項目自動打包壓縮成zip文件

  這段時間用 Vue2.0 開發項目,每次打包都會用到 npm run build 命令,可是每次部署時給後端發包都要手動zip壓縮,這樣一兩次還行,但遇到項目板塊測試和臨時加急功能測試的時候,一天可能就要打包好屢次,這就很煩了。因此索性在執行 npm run build 命令時就直接打包成zip文件,方便省事!html

一、插件裝備

  webpack插件:filemanager-webpack-plugin,該插件可執行打包,複製,移動,刪除文件以及新文件夾在build以前及以後建立。webpack

  安裝:web

npm install filemanager-webpack-plugin --save-dev
或
cnpm install filemanager-webpack-plugin --save-dev

二、webpack配置

  ① 在項目 根目錄 build/webpack.base.config.js 中 擡頭變量聲明區域添加npm

const FileManagerPlugin = require('filemanager-webpack-plugin')

  ② 在根目錄 build/webpack.base.config.js 內找到 module.exports。 而後在plugins內添加後端

new FileManagerPlugin({
    onEnd: {
        delete: [
            './dist/control-operate.zip',
        ],
        archive: [
            {source: './dist', destination: './dist/control-operate.zip'},
        ]
    }
})

  注:若 plugins不存在,則新建plugins,plugins爲數組格式。數組

三、執行效果

  配置完成後,從新執行 npm run build 命令。執行完成後,在dist文件夾內(上面配置的目的地目錄爲 dist文件夾),就能夠看到壓縮好的zip文件包了。測試

四、其餘功能

module.exports = {
    ......
    plugins: [
        new FileManagerPlugin({
            onEnd: {
                copy: [
                    {source: '/path/from', destination: '/path/to'},
                    {source: '/path/**/*.js', destination: '/path'},
                    {source: '/path/fromfile.txt', destination: '/path/tofile.txt'},
                    {source: '/path/**/*.{html,js}', destination: '/path/to'},
                    {source: '/path/{file1,file2}.js', destination: '/path/to'},
                    {source: '/path/file-[hash].js', destination: '/path/to'}
                ],
                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
                            }
                        }
                    }

                ]
            }
        })
    ],
    ......
}
相關文章
相關標籤/搜索