简体   繁体   English

yeoman webapp:用于多个HTML页面的精简JS

[英]yeoman webapp: Minified JS for multiple HTML pages

I'm playing around with the yeoman webapp generator and want to have multiple minified JavaScript files for my individual subpages for my page. 我正在使用yeoman Webapp生成器,并希望页面的各个子页面具有多个缩小的JavaScript文件。 However, the webapp concats all JavaScript files to one "big" main.js which does not match my setup. 但是,该Webapp将所有JavaScript文件连接到一个与我的设置不匹配的“大” main.js文件中。

In my setup, main.js is supposed to be some kind of common JavaScript file for all pages but each page should have an additional JavaScript file with stuff that only relates to this page. 在我的设置中, main.js应该是所有页面的某种通用JavaScript文件,但是每个页面都应该有一个附加的JavaScript文件,其中包含仅与此页面相关的内容。 Eg 例如

index.html -> uses main.js index.html >使用main.js

subpage1.html -> uses main.js and subpage1.js subpage1.html >使用main.jssubpage1.js

subpage2.html -> uses main.js and subpage2.js subpage2.html >使用main.jssubpage2.js

The result from gulp build is a big main.js that contains the main.js , subpage1.js and the subpage2.js which leads to errors: if I open subpage1.html , subpage2.js (the one that is included in the big main.js ) might try to access HTML nodes that only exist on subpage2.html but not on subpage1.html . main.js gulp build的结果是一个很大的main.js ,其中包含main.jssubpage1.jssubpage2.js ,这会导致错误:如果我打开subpage1.htmlsubpage2.jsmain.js )可能试图访问只存在于HTML节点subpage2.html但不是在subpage1.html

Eg: 例如:

subpage1.html includes the big main.js with subpage2.js subpage1.html包含大的main.jssubpage2.js

<script src="scripts/main.js"></script>

which contains subpage1.js AND subpage2.js, however if I do the following in subpage2.js 其中包含subpage1.js和subpage2.js,但是如果我在subpage2.js中执行以下操作

$('#IdThatOnlyExistsOnSubPage2').someMethod(...)

this fails of course. 这当然失败了。

How do I have to adjust the Gulpfile to solve this problem? 我必须如何调整Gulpfile才能解决此问题?

JS and CSS builds are generating with uglify (formerly known as usemin ) you could find more advanced options and features from their documentation but I think you should separate build block by your needs. JS和CSS建立与正在生成丑化 (原名usemin ),你可以找到它们的文档更高级的选项和功能,但我认为你应该在你需要分开构建块。

If you have a common JS file for every page, you have to put them together for each pages and create new block for page specific files. 如果每个页面都有一个通用的JS文件,则必须将每个页面放在一起,并为页面特定的文件创建新的块。

Here is example. 这是例子。

<!-- build:js scripts/main.js -->
<script src="config.js"></script>
<script src="app.js"></script>
<!-- endbuild -->

<!-- build:js scripts/page-dashboard.js -->
<script src="dashboard-charts.js"></script>
<script src="dashboard-widgets.js"></script>
<script src="dashboard-main.js"></script>
<!-- endbuild -->

Also you could do same thing for CSS file builds. 您也可以为CSS文件构建做同样的事情。

<!-- build:css styles/main.css -->
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="app.css">
<!-- endbuild -->

<!-- build:css style/page-dashboard.css -->
<link rel="stylesheet" href="dashboard-charts.css">
<link rel="stylesheet" href="dashboard-widgets.css">
<link rel="stylesheet" href="dashboard-main.css">
<!-- endbuild -->

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

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