How can I provide parameters for webpack html-loader interpolation?

前端 未结 7 1024
暖寄归人
暖寄归人 2020-12-05 02:36

In the html-loader documentation there is this example

require(\"html?interpolate=require!./file.ftl\");

<#list list as list>
    

        
7条回答
  •  爱一瞬间的悲伤
    2020-12-05 03:19

    Using html-loader with interpolate, you can import variables from your webpack.config.js by using DefinePlugin.

    // webpack.config.js:
    
    module.exports = {
      module: {
        rules: [
          {
            test: /\.html$/,
            loader: 'html-loader',
            options: {
              interpolate: true
            }
          }
        ],
      },
      plugins: [
        new DefinePlugin({
          VARNAME: JSON.stringify("here's a value!")
        })
      ]
    };
    

    // index.html
    
    ${ VARNAME }
    

    html-loader's interpolations accept any JavaScript expression, but the scope that those expressions are evaluated in aren't populated with any of your configuration options by default. DefinePlugin adds values to that global scope. EnvironmentPlugin could also be used to populate values in process.env.

提交回复
热议问题