如何配置webpack来请求捆绑的模块而不是源文件?

时间:2019-04-16 16:17:51

标签: javascript webpack knex.js

我正在创建一个程序包,我想捆绑knex以及我的迁移文件。

尝试执行此操作时的常见问题是由knex/lib/migrate/sources引起的,其中存在一些动态需求。阅读建议here之后,我想到了以下配置:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: './src/index.js',
  target: 'node',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
  },
  resolve: {
    extensions: ['.js'],
  },
  plugins: [
    // usage of webpack.IgnorePlugin removed for brevity

    // The relevant part
    new webpack.ContextReplacementPlugin(
      /knex\/lib\/migrate\/sources/,
      `${path.join(path.resolve('./'), 'src/migrations')}`,
    ),
  ],
};

我的迁移文件位于/src/migrations,我希望它们会被捆绑到文件build/index.js中。实际上,我已经检查了生成的index.js,并且可以看到我的迁移文件已捆绑在一起。

我想将生成的脚本用作独立脚本,但是在删除src文件夹后,在运行时出现以下错误:Unhandled rejection Error: ENOENT: no such file or directory, scandir '/workdir/packages/migrator/src/migrations'

我不明白为什么build/index.jssrc文件夹中查找文件,因为它们是文件包的一部分。

如何告诉webpack不要查看src文件夹?

0 个答案:

没有答案