简体   繁体   English

webpack 构建到多个文件

[英]webpack build to multiple files

I have web pack application and want to build output multiple files not a single javascript file.我有 web pack 应用程序,想要构建输出多个文件而不是单个 javascript 文件。 for example if I have such folder structure例如,如果我有这样的文件夹结构

components
 1.js
 2.js
actions
 1.js.
 2.js

my webpack build have to compile files in same folder structure.我的 webpack 构建必须在相同的文件夹结构中编译文件。 How i can achieve that?我怎么能做到这一点?

I tried babel cli:我试过 babel cli:

babel ./src --out-dir ./lib --source-maps --presets es2015,react --plugins babel-plugin-add-module-exports,babel-plugin-transform-decorators-legacy,babel-plugin-transform-class-properties --watch

It outputs files as I wanted but getting error它按照我的意愿输出文件但出现错误

Cannot resolve module无法解析模块

Because it does not know anything about webpack resolve.因为它对 webpack 解析一无所知。

Any suggestions?有什么建议吗?

Getting webpack to output multiple files is possible but it does have limitations.让 webpack 输出多个文件是可能的,但它确实有局限性。 First it's important to understand how this works.首先,了解它是如何工作的很重要。 Webpack actually provides a runtime script that can load code "chunks" as they are needed, this is important for large applications so the user doesn't have to download the javascript for the entire app just to see the homepage. Webpack 实际上提供了一个运行时脚本,可以根据需要加载代码“块”,这对于大型应用程序很重要,因此用户不必为了查看主页而下载整个应用程序的 javascript。 But webpack needs to keep track of these chunks and what they're named at build time to be able to load them correctly at run time.但是 webpack 需要跟踪这些块以及它们在构建时命名的内容,以便能够在运行时正确加载它们。 For that reason it has it's own file naming conventions to enable this functionality.出于这个原因,它有自己的文件命名约定来启用此功能。 See more here: https://webpack.js.org/guides/code-splitting/ .在此处查看更多信息: https : //webpack.js.org/guides/code-splitting/ So you can require.ensure all of your deps, but they won't be named and foldered they way you describe as webpack's runtime wouldn't be able to find them in that case.所以你可以 require.ensure 你所有的 deps,但它们不会按照你描述的方式命名和文件夹,因为在这种情况下 webpack 的运行时将无法找到它们。

The other thing that's important to consider is that webpack is a bundler, so it's meant to bundle files.另一件需要考虑的重要事情是 webpack 是一个打包器,所以它是用来打包文件的。 Essentially your saying you don't want to bundle your files.基本上你说你不想捆绑你的文件。 So if that's the case, you should probably look into using require.js.因此,如果是这种情况,您可能应该考虑使用 require.js。 Many people have moved from require to bundlers such as Wepback and Browserify as typically it's not efficient to download every little module seperately.许多人已经从 require 转向了诸如 Wepback 和 Browserify 之类的打包器,因为单独下载每个小模块通常效率不高。 But, every app is different so you may in fact have a good reason to not bundle.但是,每个应用程序都是不同的,因此您实际上可能有充分的理由不捆绑。

If you do in fact want to do some bundling but want to optimize how it's done, then I can help with that as well, and webpack is certainly a great tool for that.如果您确实想做一些捆绑但想优化它的完成方式,那么我也可以提供帮助,而 webpack 无疑是一个很好的工具。 But I'll need to understand your use case a little more to give the best advice.但是我需要更多地了解您的用例才能提供最佳建议。

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

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