【问题标题】:Vue + Webpack: exclude config.js from being packed works, but it does not loadVue + Webpack:将 config.js 排除在打包工作之外,但它不会加载
【发布时间】:2019-08-27 03:05:09
【问题描述】:

我想要什么:

在我的 Vue 项目中,当执行 vue run build 时,所有内容都被打包(webpack)到 dist 目录中。

我可以将整个包部署到测试服务器,但由于测试服务器需要另一组凭据(用于数据库等),因此每个环境中必须有一个文件,即 config.js

我的策略:

  • 排除 config.js 被打包到 app.12314...js 包中
  • config.js 定义为作为文件原样发出(通过webpack 的file-loader

我做了什么:

在需要配置数据的Vue组件文件中,配置通过:

<script>
  import config from '@/config/config.js';
</script>

我创建了一个vue.config.js 文件来修改默认的 webpack 设置,如下所示:

const path = require("path");

// vue.config.js
module.exports = {
  publicPath: '/',
  configureWebpack: {
    module: {
      rules: [
        {
          test: /config.*config\.js$/,
          use: [
            {
              // exclude config file from being packed. The config file should simply reside as a plain file on the
              // file system, since it must be replaced with the target environoment specific config file on the server,
              // e.g. for setting the correct database connection
              loader: 'file-loader',
              options: {
                name: 'config/config.js'
              },
            }
          ]
        }
      ]
    }
  }
}

我的预期:

  • config.js 将作为普通文件发出,而不是捆绑 => dist 文件夹将包含一个捆绑的 app....js 文件和一个单独的 config.js 文件
  • 我可以将dist 文件夹的内容部署到测试服务器,生成的代码将负责加载 config.js,以便组件可以使用配置数据李>

问题:

当然,config.js 现在作为文件发出,所以我的 dist 文件夹如下所示:

.
├── config
│   └── config.js
├── css
│   ├── app.eb377513.css
│   └── chunk-vendors.2da46af1.css
├── favicon.png
├── index.html
└── js
    ├── app.724607ed.js
    ├── app.724607ed.js.map
    ├── chunk-vendors.426dad42.js
    └── chunk-vendors.426dad42.js.map

如您所见,一个单独的文件config.js!所有其他 JS 代码已捆绑到 app.724607ed.js。到目前为止,一切顺利。

但是,测试服务器上的应用程序不会加载config.js,因此每个尝试使用其中变量的组件都会失败。

我注意到在 Chrome 的开发者工具中,config.js 没有网络流量。显然,webpack 代码并没有尝试从文件系统中实际加载文件。我预计会自动出现这种情况。

  1. 我做错了什么,我错过了什么?
  2. 这是排除配置文件被打包的最佳做法吗?

【问题讨论】:

    标签: vue.js webpack config


    【解决方案1】:

    显然,Webpack 的 file-loader 确实单独发出文件(好),但没有添加代码以在 webapp 中加载它(坏)。我假设file-loader 会负责在index.html 文件中插入正确的&lt;script&gt; 标签,这样:

    • 将加载单独发出的config.js 文件
    • 在开发方面,使用 config.js 的语法与其他(打包)工件的语法相同,如下所示:

    (在Vue组件文件中:)

    <script>
        import backend from '@/utils/backend.js'; // <-- normal way of requiring modules in Vue component
        import config from '@/config/config.js'; // <-- would work the same, but actually will load emitted file
    </script>
    

    不幸的是,我似乎错了。由于 Webpack 不关心在 webapp 中加载文件,所以我切换到手动方式,如下所示:

    第 1 步:作为单独的文件发出

    这个很重要,因为我希望 config.js 文件是独立的(不与 Vue 应用程序的其余部分一起打包和缩小)。我已经在问题中描述了如何做到这一点(为config.js 定义一个单独的规则并使用file-loader)。

    第 2 步:注意在 webapp 中加载配置文件

    由于file-loader 没有添加加载代码,请手动添加。在index.html 中,插入以下行:

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <link rel="icon" href="<%= BASE_URL %>favicon.png">
        <script type="text/javascript" src="config/config.js" ></script> <-------- insert this line!
        <title>replayer-gui4</title>
    </head>
    

    第3步:更改config.js以兼容旧版导入方式(不带ES2015模块)

    现在,commonJS 语法不再可行,因为 config.js 应该由浏览器加载。而且我不确定如何将 ES2015 类型的模块加载(使用本机 import 语法)与 Webpack 代码(它将用 Webpack 加载代码替换所有 import)混合。所以我决定改用旧方法:让config.js 像以前一样简单地注册一个全局(糟糕!)变量:

    var config = (() => {
        return {
            databaseCredentials: ...
        };
    })();
    

    因此,在每个使用配置设置的 Vue 模块中,调整“导入”代码 - 只需删除导入,因为变量 config 无论如何都将在全局范围内可用(在我们在此步骤中更改 config.js 之后):

    <script>
        // import config from '@/config/config.js'; // <-- commented out - or just REMOVE this line
    </script>
    

    第 4 步:触发 Webpack 考虑config.js

    现在唯一需要注意的是,由于 config.js 从未在 import 语句中的 Javascript 文件中引用,Webpack 不会“找到”该文件,也不会考虑它。这意味着config.js 也永远不会被发出。

    所以,强制 Webpack 处理它,App.vue:

    <script>
        import configDummy from '@/config/config.js';
    </script>
    

    请注意,我们从不使用 configDummy 引用(变量),这只是 Webpack 找到它 - 并将其规则应用于文件。这种情况下的规则是:将其作为dist 文件夹中的单独文件发出(即生成dist/config/config.js 文件)

    就是这样!我们使用了 Webpack 的 file-loader,因此 config.js 不会被缩小并捆绑到应用程序包中,而是作为单独的文件保存。此外,我们还负责通过index.html 加载它,这意味着配置设置是全局可用的。

    我不喜欢这个解决方案的一点是,带有import 方法的好方法在这个特殊文件中被破坏了。但是我发现没有简单的解决方案可以让 Webpack 处理这些事情。

    如果有人有建议,我很高兴听到!

    09.04.2019 更新:不,这不是一个理想的解决方案。这适用于打包/部署(npm run build 部分),但不适用于 用于开发服务器(npm run serve 部分)。为了让开发服务器工作,我不得不将config.js复制到路径public/config/config.js中,以便开发服务器可以在index.html告诉它的地方找到它,即config/config.js(已解决相对于index.html 文件)。

    所以,再说一遍:它有效,但有点笨拙。而且我不喜欢这种解决方案。

    【讨论】:

    • 我所做的更简单:只需在配置中的entry 参数中添加一个条目。例如:{ entry: { app: './src/main.js', config: './src/config/config.js' } }。但是,我还没有找到如何排除该文件被optimization.minifier缩小...
    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 2016-08-11
    • 2023-03-16
    • 2021-08-20
    • 2014-07-28
    • 2020-01-11
    • 2016-11-06
    • 2017-08-23
    相关资源
    最近更新 更多