如何在webpack的入口中添加通配符映射

Pra*_*Raj 44 node.js webpack

我需要webpack脚本文件夹中的所有js文件.我试过这个

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

我收到这样的错误,

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)
Run Code Online (Sandbox Code Playgroud)

它没有搜索所有的js文件,而是搜索那样的*.js.帮我找错过的

小智 77

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

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

var glob = require("glob");
// ...
entry: glob.sync("./src/scripts/*.js")
Run Code Online (Sandbox Code Playgroud)

  • glob.sync("./ src/scripts/**/*.js")递归地在任何子目录上工作 (15认同)
  • 虽然这对于'.js'来说有点儿行为,但对于`css | less | sass | ...`来说可能非常方便(对于我们这些放弃了全Webpack解决方案的人来说...) (2认同)

kom*_*cha 14

的WebPack期待一个文件列表entry配置,而不是glob模式.

您必须手动列出文件,或使用此代码段自动列出文件

var fs = require('fs'),
    entries = fs.readdirSync('./src/scripts/').filter(function(file) {
        return file.match(/.*\.js$/);
    });
Run Code Online (Sandbox Code Playgroud)

然后将其传递给webpack的配置.


dun*_*all 11

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

来自webpack文档:

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

如果传递数组:启动时加载所有模块.最后一个是导出的.

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

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

  • 我不知道这是自己的处境,但[有时候](https://github.com/webpack/webpack/tree/master/examples/multiple-entry-points)很有意义的多个入口点.但总的来说,入门点就足够了. (3认同)

Jka*_*nen 7

我在Webpack 2.4.1中遇到了一些文件路径问题,所以就这样做了.除了多个条目,这还会创建多个.html文件.

const HtmlWebpackPlugin = require('html-webpack-plugin');
const fs = require('fs');

function getEntries (){
    return fs.readdirSync('./src/pages/')
        .filter(
            (file) => file.match(/.*\.js$/)
        )
        .map((file) => {
            return {
                name: file.substring(0, file.length - 3),
                path: './pages/' + file
            }
        }).reduce((memo, file) => {
            memo[file.name] = file.path
            return memo;
        }, {})
}

const config = {
    entry: getEntries, 
    output: {
        path: resolve('./public'),
        filename: '[name].js'
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'My App',
            filename: '[name].html',
            template: './pages/_template.html'
        })
    ]
}
Run Code Online (Sandbox Code Playgroud)