简体   繁体   English

如何通过将非导入(非导出)文件包含在捆绑包中来缓慢集成Webpack?

[英]How to slowly integrate webpack by including in the bundle non imported (nor exported) files?

I'm working in a project where we want to integrate Webpack into our workflow. 我正在一个项目中,我们希望将Webpack集成到我们的工作流程中。 The problem is, we have over 1000 AngularJS files and adding import/export to all of them in one go is not an option for us. 问题是,我们有1000多个AngularJS文件,一次性无法向所有文件添加导入/导出。 We'd like to bundle all of them and slowly incorporate the import/exports as we work on each file over time. 我们希望将它们全部捆绑在一起,并随着时间的推移逐步处理每个文件的导入/导出。

How would you approach that problem? 您将如何解决这个问题? Any specific best practices when doing this? 执行此操作时是否有任何特定的最佳做法?

We literally had the same problem. 我们确实有同样的问题。 Essentially you want to create "entry point files" that perform require s for all your files, since this is how webpack works (it follows the dependency tree). 本质上,您要创建对所有文件都执行require的“入口点文件”,因为这是webpack的工作方式(遵循依赖关系树)。 Then point webpack at these "entry point files". 然后将webpack指向这些“入口点文件”。

The example at the link above uses TypeScript, but you can easily use ES5 like this: 上面链接中的示例使用TypeScript,但是您可以像这样轻松地使用ES5:

# ./entry-points/feature1.js
importAll = function(r) {
  r.keys().forEach(r);
};

importAll(require.context('./app/feature1', true, /module\.js$/));
importAll(require.context('./app/feature1', true, /(^(?!.*(spec|module)\.js).*\.js)$/));

You can grab a polyfill for Object.keys here , and Array.forEach` here . 你可以抓住的填充工具Object.keys 这里 ,和Array.forEach` 这里

Then point to this file from your webpack config like this: 然后从您的webpack配置中指向该文件,如下所示:

entry: {
  'feature1': './entry-points/feature1.js'
}

You can read more details here 您可以在此处阅读更多详细信息

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

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