Code Monkey home page Code Monkey logo

version-check-webpack-plugin's Introduction

version-check-webpack-plugin

A webpack plugin to generate dynamic-version json file and inject version-check function into frontend dist assets. 一个用于给前端代码动态生成版本json文件和注入检测版本脚本的Webpack插件。

Usage

npm install version-check-webpack-plugin --save-dev / yarn add version-check-webpack-plugin -D

Options

interface Options { versionFilename?: string, entryNeedInjectedUpdater?: string | [] }
const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const VersionCheckPlugin = require('version-check-webpack-plugin');
/**
 * @type {import('webpack').Configuration}
 */
const webpackConfig = {
    mode: 'production',
    context: path.resolve(__dirname, './'),
    entry: {
        main: {
            import: ['./src/index.js']
        },
        vendor: './src/vendor.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist/'),
        filename: '[name].[chunkhash:8].js',
        publicPath: '/public'
    },
    plugins: [
        new VersionCheckPlugin({
            versionFilename: 'version/current-version.json',
            // entryNeedInjectedUpdater: ['main'], // or next line
            entryNeedInjectedUpdater: 'main'
        }),
        new HTMLWebpackPlugin({
            filename: 'index.html',
            template: path.resolve(__dirname, 'index.html'),
            inject: 'body'
        })
    ]
};
module.exports = webpackConfig;

HTMLWebpackPlugin is recommended with this plugin.

Dist Assets like:

dist/version/current-verion.json

{"version":"e3131e0662c4c2dc9c5b37fab2dbf6e1329fb50134ad6cdad81722e54c68"}

version-check code like(just origin template, not transformed yet):

function initial(self) {
    const cvkey = `__${(0xbc729ba02bd1).toString(36)}_version-check-updater__`;
    const previousVersion = self.localStorage.get(cvkey);
    if (previousVersion === null) {
        fetchVersion().then(versionJson => {
            self.localStorage.setItem(cvkey, versionJson.version);
        }).catch(e => {
            console.warn(`Do initial check version failed cause: ${e.message || e.toString()}`);
        })
    }

    /**
     * Get latest version.
     * @returns {Promise<{version: string}>}
     */
    function fetchVersion() {
        return new Promise((resolve, reject) => {
            fetch(`{{versionHashPath}}?tag=${Math.random().toString(36).slice(2)}`, {
                credentials: 'include',
                method: 'GET',
            }).then(res => {
                res.json().then(versionJson => resolve(versionJson));
            }).catch(e => reject(e));
        });
    }

    /**
     * Check if new version built.
     * @param {boolean} forceRefresh 
     * @returns {Promise<boolean>}
     */
    function versionCheckController(forceRefresh) {
        return new Promise((resolve, reject) => {
            fetchVersion().catch(e => reject(e)).then(versionJson => {
                if (versionJson.version === previousVersion) resolve(false);
                else if (forceRefresh) self.location.reload();
                else resolve(true);
            })
        });
    }

    self.$versionCheckController = versionCheckController;

    return versionCheckController;
}

export default initial(globalThis || window);

And in entry index.html this script will be injected like:

<script defer="defer" src="/public/updater.34ad6cda.js"></script>

So you can use it like:

//...

function doUdpate() {
    $versionCheckController(/** forceRefresh */false).then(verionChanged => {
        if (versionChanged) {
            //TODO 
            // show version changed message for user fetch new version or reload window directly ^_^
        }
    })
}
function beforeRouteChange() {
    doUdpate();
}

request('/api/list/detail?id=12').then(res => {
    if (res.status === '404') doUdpate();
}).catch(e => {
    doUdpate()
})
//...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.