Passing environment-dependent variables in webpack

后端 未结 15 2165
醉酒成梦
醉酒成梦 2020-11-22 12:46

I\'m trying to convert an angular app from gulp to webpack. in gulp I use gulp-preprocess to replace some variables in the html page (e.g. database name) depending on the NO

15条回答
  •  旧时难觅i
    2020-11-22 13:21

    now 2020, i am face to same question, but for this old question, there are so many new answer, just list some of it:

    • this is webpack.config.js
    plugins: [
            new HtmlWebpackPlugin({
                // 1. title is the parameter, you can use in ejs template
                templateParameters:{
                    title: JSON.stringify(someting: 'something'),
                },
            }), 
    
    
            //2. BUILT_AT is a parameter too. can use it.
            new webpack.DefinePlugin({
                BUILT_AT: webpack.DefinePlugin.runtimeValue(Date.now,"some"),
    
            }),
    
            //3. for webpack5, you can use global variable: __webpack_hash__
            //new webpack.ExtendedAPIPlugin()
        ],
        //4. this is not variable, this is module, so use 'import tt' to use it.
        externals: { 
            'ex_title': JSON.stringify({
                tt: 'eitentitle',
            })
        },
    
    

    the 4 ways only basic, there are even more ways that i believe. but i think maybe this 4ways is the most simple.

提交回复
热议问题