简体   繁体   English

将目录中的所有文件作为 webpack 入口点的最佳方法?

[英]Best way to have all files in a directory be entry points in webpack?

I want to create multiple entry points for a website, which is pretty easily done in Webpack using an object for the entry property, like here .我想为一个网站创建多个入口点,这在 Webpack 中使用entry属性的对象很容易完成,就像这里

But as the site grows (and it inevitably will) having to add each entry point seems cumbersome and prone to error.但是随着站点的增长(并且不可避免地会),必须添加每个入口点似乎很麻烦并且容易出错。 So I'd like to simply point at a directory and say "here are all the entry points."所以我想简单地指向一个目录并说“这里是所有的入口点”。

So I've cooked this up:所以我做了这个:

var path = require('path');
var fs = require('fs');
var entryDir = path.resolve(__dirname, '../source/js');
var entries = fs.readdirSync(entryDir);
var entryMap = {};
entries.forEach(function(entry){
    var stat = fs.statSync(entryDir + '/' + entry);
    if (stat && !stat.isDirectory()) {
        var name = entry.substr(0, entry.length -1);
        entryMap[name] = entryDir + '/' + entry;
    }
});

module.exports = {
  entry: entryMap,
  output: {
    path: path.resolve(__dirname, '../static/js'),
    filename: "[name]"
  },
...

This works fine, but is there a feature or configuration option in Webpack that would handle this for me?这工作正常,但是 Webpack 中是否有可以为我处理此问题的功能或配置选项?

I think glob is the right way to go here (AFAIK webpack wont do this for you).我认为 glob 是去这里的正确方法(AFAIK webpack 不会为你做这件事)。 This is what I ended up with, it will find all files in a directory and create an entry with a name matching the file:这就是我最终得到的结果,它将在目录中查找所有文件并创建一个名称与文件匹配的条目:

var glob = require('glob');
var path = require('path');

module.exports = {
  entry: glob.sync('../source/js/**.js').reduce(function(obj, el){
     obj[path.parse(el).name] = el;
     return obj
  },{}),
  output: {
    path: path.resolve(__dirname, '../static/js'),
    filename: "[name]"
  },
...

adapt the search path to meet your specific needs.调整搜索路径以满足您的特定需求。 It might also be useful to pass in {cwd: someRoot} as the second argument to sync if you have a special scripts directory which will make this the new root of relative path searches.如果您有一个特殊的脚本目录,将{cwd: someRoot}作为第二个参数传递给同步也可能很有用,这将使该目录成为相对路径搜索的新根。

I have used Glob for this.我为此使用了Glob

 var path = require('path'); var glob = require('glob'); module.exports = { entry: { 'app' : glob.sync('./scripts/**/*.ts*') }, output: { path: path.join(__dirname, '/wwwroot/dist'), filename: '[name].bundle.js', sourceMapFilename: '[name].map' }, module: { rules: [ { test: /\\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, } ] }, resolve: { extensions: [".ts", ".js"] } };

In my opinion, only a little Node skill is needed, and it doesn't have to be that complicated.在我看来,只需要一点 Node 技能就可以了,不需要那么复杂。

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

const fileNames = fs.readdirSync('./src').reduce((acc, v) => ({ ...acc, [v]: `./src/${v}` }), {});

const config = {
  entry: fileNames,
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]',
  },
};

module.exports = config;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM