简体   繁体   English

Webpack中多个入口点的通用样板

[英]Common boilerplate for multiple entry points in Webpack

In Webpack (4) are there ways to pass export(s) of a given entry file into a helper module, which itself acts as entry? 在Webpack(4)中有没有办法将给定条目文件的导出传递给辅助模块,辅助模块本身就作为条目? Basically a way to "wrap" entry points. 基本上是一种“包装”入口点的方法。

Now, I know I can easily use CommonChunks to import into an entry, but what I trying to do is export from the entry for another module to actually do something in its place. 现在,我知道我可以轻松地使用CommonChunks 导入到一个条目中,但我想要做的是另一个模块的条目导出实际上做某事。 Here's why. 这就是原因。

I have a react application which I'm using Webpack to build. 我有一个反应应用程序,我正在使用Webpack来构建。 I am porting from a NEXT.js stack which, for my purposes, is a source of overhead. 我从NEXT.js堆栈移植,为了我的目的,这是一个开销的来源。 One feature I want to keep though, is putting routes in a pages/ folder where I export default Component in each page, rendering automagically. 我想要保留的一个功能是将路由放在pages/文件夹中,我在每个页面中export default Component ,自动渲染。 This would be very nice behavior to have for a Webpack project. 这对于Webpack项目来说是非常好的行为。

Right now, I get my directory-based-multiple-entry roughly like this. 现在,我得到的基于目录的多条目大致是这样的。

/* webpack.config.js */

const pages = {};

for(let route of fs.readdirSync('./pages')){
    pages[route] = path.resolve('./pages', route);
}

module.exports = {
    entry: pages,
    /* ... */
}

It works; 有用; but I do need to require and setup ReactDOM.render for every entry. 但我需要为每个条目要求并设置ReactDOM.render Alone this is not a big deal, but when getting into HMR (hot-reloading) and other things, it adds a lot of redundancy. 单独这不是什么大问题,但是当进入HMR(热重载)和其他东西时,它增加了很多冗余。 It also makes it harder to keep my code dev/production agnostic. 它还使我的代码开发/生产不可知变得更加困难。

What I'd like to do is this. 我想做的就是这个。 Have one module somewhere in my project which does the actual rendering of my pages, something like: 在我的项目中的某个地方有一个模块可以实现我的页面的实际渲染,例如:

/* init.js */

const MyEntryPoint = require("🤷‍♂️").default;

const intoContainer = 
    document.body.appendChild(document.createElement("div"))

ReactDOM.render(MyEntryPoint, intoContainer);

with my multiple entry pages looking like 我的多个条目页面看起来像

/* pages/login/index.js */

export default () => (
    <div>Welcome to the login page!</div>
)

I know there is a way to pass an array for given entries, but to my understanding they are concatenated and cannot import one another. 我知道有一种方法可以为给定的条目传递数组,但根据我的理解,它们是连接在一起的,不能相互导入。 My only other theory is to export each page as a library and manually include bootstrap code as a script-tag. 我唯一的另一个理论是将每个页面导出为库,并手动将bootstrap代码作为脚本标记包含在内。

Is there any better way to do this? 有没有更好的方法来做到这一点?

I did actually figure this out at some point. 我确实在某个时候想到了这一点。 Ended up rolling my own solution called 结束了我自己的解决方案
Webpack Intermediate Entry . Webpack中级入门

What it does is load a given insert file, specified in IntermediateEntryPlugin . 它的作用是加载在IntermediateEntryPlugin指定的给定insert文件。

You can install and import this plugin via NPM as webpack-intermediate-entry . 您可以通过NPM安装和导入此插件作为webpack-intermediate-entry

It then replaces all entries with that file, and replacing "__webpack_entry__" import, with each in-turn. 然后它用该文件替换所有条目,并将每个条目替换为"__webpack_entry__" import。

Hopefully it helps! 希望它有所帮助!

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

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