NodeJS/ExpressJS:在生产中提供单个串联 JS 文件

发布于 2024-12-10 02:00:57 字数 970 浏览 0 评论 0原文

我正在处理许多单独的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-12-17 02:00:57

我最终使用了 RequireJS 因为它的优化功能可以为生产构建单个 JS 文件 (main.js)。在开发中,所有文件都是单独的 JS 文件,它们会异步加载,而在生产中,这些文件会连接成一个大 js 文件。

要点是页面的 部分(或加载脚本的任何位置)在生产和开发中保持不变。

<script data-main='/js/main.js' src='/js/plugins/require.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.

<script data-main='/js/main.js' src='/js/plugins/require.js'>
楠木可依 2024-12-17 02:00:57

为什么要为此在生产/开发之间采取不同的行为呢?它最终可能会让你痛不欲生,而且 AFAICT connect-assetmanager 并没有真正给开发周期添加任何障碍,所以只要一直使用它就可以了,不用担心。

但是,如果您必须这样做,只需查看 process.env['NODE_ENV'] 值和layout.jade 中的条件子句即可。

if production
  script(src="/js/script.js")
else
  script(src="/js/libs/jquery.min.js")
  script(src="/js/libs/plugins.js")
  #and so so

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.

if production
  script(src="/js/script.js")
else
  script(src="/js/libs/jquery.min.js")
  script(src="/js/libs/plugins.js")
  #and so so
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文