<bdo id='Xkobx'></bdo><ul id='Xkobx'></ul>
    1. <i id='Xkobx'><tr id='Xkobx'><dt id='Xkobx'><q id='Xkobx'><span id='Xkobx'><b id='Xkobx'><form id='Xkobx'><ins id='Xkobx'></ins><ul id='Xkobx'></ul><sub id='Xkobx'></sub></form><legend id='Xkobx'></legend><bdo id='Xkobx'><pre id='Xkobx'><center id='Xkobx'></center></pre></bdo></b><th id='Xkobx'></th></span></q></dt></tr></i><div id='Xkobx'><tfoot id='Xkobx'></tfoot><dl id='Xkobx'><fieldset id='Xkobx'></fieldset></dl></div>

        <tfoot id='Xkobx'></tfoot>
        <legend id='Xkobx'><style id='Xkobx'><dir id='Xkobx'><q id='Xkobx'></q></dir></style></legend>

        <small id='Xkobx'></small><noframes id='Xkobx'>

        使用 Webpack 基于环境的条件构建

        Conditional build based on environment using Webpack(使用 Webpack 基于环境的条件构建)

          <i id='Zz15e'><tr id='Zz15e'><dt id='Zz15e'><q id='Zz15e'><span id='Zz15e'><b id='Zz15e'><form id='Zz15e'><ins id='Zz15e'></ins><ul id='Zz15e'></ul><sub id='Zz15e'></sub></form><legend id='Zz15e'></legend><bdo id='Zz15e'><pre id='Zz15e'><center id='Zz15e'></center></pre></bdo></b><th id='Zz15e'></th></span></q></dt></tr></i><div id='Zz15e'><tfoot id='Zz15e'></tfoot><dl id='Zz15e'><fieldset id='Zz15e'></fieldset></dl></div>
            <tfoot id='Zz15e'></tfoot>

              <small id='Zz15e'></small><noframes id='Zz15e'>

                <tbody id='Zz15e'></tbody>
            1. <legend id='Zz15e'><style id='Zz15e'><dir id='Zz15e'><q id='Zz15e'></q></dir></style></legend>

                • <bdo id='Zz15e'></bdo><ul id='Zz15e'></ul>
                • 本文介绍了使用 Webpack 基于环境的条件构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have some things for development - e.g mocks which I would like to not bloat my distributed build file with.

                  In RequireJS you can pass a config in a plugin file and conditonally require things in based on that.

                  For webpack there doesn't seem to be a way of doing this. Firstly to create a runtime config for an environment I have used resolve.alias to repoint a require depending on the environment, e.g:

                  // All settings.
                  var all = {
                      fish: 'salmon'
                  };
                  
                  // `envsettings` is an alias resolved at build time.
                  module.exports = Object.assign(all, require('envsettings'));
                  

                  Then when creating the webpack config I can dynamically assign which file envsettings points to (i.e. webpackConfig.resolve.alias.envsettings = './' + env).

                  However I would like to do something like:

                  if (settings.mock) {
                      // Short-circuit ajax calls.
                      // Require in all the mock modules.
                  }
                  

                  But obviously I don't want to build in those mock files if the environment isn't mock.

                  I could possibly manually repoint all those requires to a stub file using resolve.alias again - but is there a way that feels less hacky?

                  Any ideas how I can do that? Thanks.

                  解决方案

                  You can use the define plugin.

                  I use it by doing something as simple as this in your webpack build file where env is the path to a file that exports an object of settings:

                  // Webpack build config
                  plugins: [
                      new webpack.DefinePlugin({
                          ENV: require(path.join(__dirname, './path-to-env-files/', env))
                      })
                  ]
                  
                  // Settings file located at `path-to-env-files/dev.js`
                  module.exports = { debug: true };
                  

                  and then this in your code

                  if (ENV.debug) {
                      console.log('Yo!');
                  }
                  

                  It will strip this code out of your build file if the condition is false. You can see a working Webpack build example here.

                  这篇关于使用 Webpack 基于环境的条件构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                      <tbody id='Vdsyn'></tbody>

                    <i id='Vdsyn'><tr id='Vdsyn'><dt id='Vdsyn'><q id='Vdsyn'><span id='Vdsyn'><b id='Vdsyn'><form id='Vdsyn'><ins id='Vdsyn'></ins><ul id='Vdsyn'></ul><sub id='Vdsyn'></sub></form><legend id='Vdsyn'></legend><bdo id='Vdsyn'><pre id='Vdsyn'><center id='Vdsyn'></center></pre></bdo></b><th id='Vdsyn'></th></span></q></dt></tr></i><div id='Vdsyn'><tfoot id='Vdsyn'></tfoot><dl id='Vdsyn'><fieldset id='Vdsyn'></fieldset></dl></div>

                      <tfoot id='Vdsyn'></tfoot>

                        <small id='Vdsyn'></small><noframes id='Vdsyn'>

                        <legend id='Vdsyn'><style id='Vdsyn'><dir id='Vdsyn'><q id='Vdsyn'></q></dir></style></legend>
                          • <bdo id='Vdsyn'></bdo><ul id='Vdsyn'></ul>