NodeJS/ExpressJS:在生产中提供单个串联 JS 文件
我正在处理许多单独的 JS 文件,如下所示:
<script defer src="/js/libs/jquery.min.js"></script>
<script defer src="/js/libs/plugins.js"></script>
<!-- application core -->
<script defer src="/js/application.js"></script>
<!-- modules -->
<script defer src="/js/modules/router.js"></script>
<script defer src="/js/modules/feed.js"></script>
<script defer src="/js/modules/files.js"></script>
<script defer src="/js/modules/members.js"></script>
<script defer src="/js/modules/sharebar.js"></script>
<script defer src="/js/modules/utils.js"></script>
在生产中,我使用 connect-assetmanager 来将所有这些文件连接到一个 script.js
中。如何动态更改我的网站 layout.jade
以像这样提供这个单个 JS 文件?
<script defer src="/js/script.js"></script>
I am working with lots of individual JS files served like so:
<script defer src="/js/libs/jquery.min.js"></script>
<script defer src="/js/libs/plugins.js"></script>
<!-- application core -->
<script defer src="/js/application.js"></script>
<!-- modules -->
<script defer src="/js/modules/router.js"></script>
<script defer src="/js/modules/feed.js"></script>
<script defer src="/js/modules/files.js"></script>
<script defer src="/js/modules/members.js"></script>
<script defer src="/js/modules/sharebar.js"></script>
<script defer src="/js/modules/utils.js"></script>
In production I use connect-assetmanager to concatenate all these files into one script.js
. How can I dynamically alter my site layout.jade
to serve this single JS file like so?
<script defer src="/js/script.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用了 RequireJS 因为它的优化功能可以为生产构建单个 JS 文件 (main.js)。在开发中,所有文件都是单独的 JS 文件,它们会异步加载,而在生产中,这些文件会连接成一个大 js 文件。
要点是页面的
部分(或加载脚本的任何位置)在生产和开发中保持不变。
I ended up using RequireJS as with it's optimisation features you can build a single JS file (main.js) for production. In development all files are separate JS files which get loaded asynchronously and in production these files are concatenated into one big js file.
The main point is that the
<head>
part of your page (or wherever you load your scripts) remains the same in production and development.为什么要为此在生产/开发之间采取不同的行为呢?它最终可能会让你痛不欲生,而且 AFAICT connect-assetmanager 并没有真正给开发周期添加任何障碍,所以只要一直使用它就可以了,不用担心。
但是,如果您必须这样做,只需查看 process.env['NODE_ENV'] 值和layout.jade 中的条件子句即可。
Why bother behaving differently between production/development for this? It will probably bite you in the ass eventually and AFAICT connect-assetmanager doesn't really add any hindrance to the development cycle, so just use it all the time and don't worry about it.
However, if you must, its just a matter of looking at the
process.env['NODE_ENV']
value and a conditional clause in your layout.jade.