简体   繁体   English

Webpack:使用多个构建目标

[英]Webpack: Using multiple build targets

Given an (Angular-)Webapp with a Webpack-buildprocess I would like to add several build-targets to my Webpack configuration. 给定一个具有Webpack-buildprocess的(Angular-)Webapp,我想向我的Webpack配置中添加几个构建目标。

There are plenty tutorials on how to modify your webpack.config at compile time, which is already helpful (and probably all I need) to get dev and prod builds running. 有很多关于如何在编译时修改webpack.config的教程,这对于使devprod构建运行已经很有帮助(也许是我所需要的)。

However I was hoping that there is a way to parametrize the build process so that different files are used for the build-process. 但是,我希望有一种参数化构建过程的方法,以便将不同的文件用于构建过程。

Question

How do i configure Webpack to replace all *.js / *.html -Files with files called *.mobile.js / *.mobile.html (if available) during the build-step? 我如何配置Webpack在构建步骤中将所有*.js / *.html -Files替换为名为*.mobile.js / *.mobile.html文件?

Bonusquestion: How do I exclude environment-specific import statements via webpack-config? 奖励问题:如何通过webpack-config排除特定于环境的import语句?

Example

I got several view/controller-pairs which are linked via Angulars $stateprovider . 我有几个通过Angulars $stateprovider链接的视图/控制器对。 In the mobile version of the webapp most of the HTML -views should be a mobile-optimized template. 在webapp的移动版本中,大多数HTML视图应该是针对移动设备优化的模板。

If I use several entry-points for webpack to generate a desktop-version and a mobile-version I would have to rewrite every file where HTML-Files are required or imported for each of my build-targets and specify the new filenames manually. 如果我为Webpack使用多个入口点来生成桌面版本和移动版本,则必须重写每个文件,这些文件是我的每个构建目标所必需或导入的HTML文件,并手动指定新文件名。

This would cause a lot of code-duplication and is hard to maintain. 这将导致大量的代码重复,并且难以维护。

I ended up using the following in my webpack.config 我最终在webpack.config中使用了以下内容

const target = env && env.mobile ? 'mobile' : 'desktop'

resolve: {
    extensions: [`.${target}.js`, '.js', `.${target}.html`, '.html']
}

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

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