如何在webpack条目中添加通配符映射

node.js Webpack

村村番长

2020-03-24

我需要将所有js文件夹中的js文件打包到Web上。

module.exports = {
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: ["babel-loader"],
      }
    ],
  },
  entry: "./src/scripts/*.js",
  output: {
    path: './src/build',
    filename: '[name].js'
  }
};

我收到这样的错误,

ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory' ./s
rc/scripts/* in E:\Web project\ReactJS\react-tutorial
resolve file
  E:\Web project\ReactJS\react-tutorial\src\scripts\* doesn't exist
  E:\Web project\ReactJS\react-tutorial\src\scripts\*.webpack.js doesn't exist
  E:\Web project\ReactJS\react-tutorial\src\scripts\*.web.js doesn't exist
  E:\Web project\ReactJS\react-tutorial\src\scripts\*.js doesn't exist
  E:\Web project\ReactJS\react-tutorial\src\scripts\*.json doesn't exist
resolve directory
  E:\Web project\ReactJS\react-tutorial\src\scripts\* doesn't exist (directory d
efault file)
  E:\Web project\ReactJS\react-tutorial\src\scripts\*\package.json doesn't exist
 (directory description file)

它不是在搜索所有的js文件,而是在搜索* .js之类的文件。

第3370篇《如何在webpack条目中添加通配符映射》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
老丝阿飞 2020.03.24

对于大多数用例而言,只有一个或几个入口点就足够了,但是如果您真的想从目录中捆绑所有文件,则可以使用以下命令:

如此处所述:https : //github.com/webpack/webpack/issues/370

var glob = require("glob");
// ...
entry: glob.sync("./src/scripts/*.js")
凯西里 2020.03.24

entry值应解析为特定文件或特定文件列表。

webpack文档

如果传递字符串:字符串将解析为在启动时加载的模块。

如果传递数组:启动时将加载所有模块。最后一个已导出。

如果您只是尝试定义一个模块,请编辑您的entry值以指向您的主应用程序文件,然后从中请求其他模块。

如果您确实要捆绑目录中的所有文件,请参阅arseniews答案

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android