简体   繁体   English

如何更改html(jade模板)在生产中使用* .min js和css?

[英]How to change html (jade template) to use *.min js and css in production?

I'm using Grunt to compile jade templates to html, uglify/concat js and minimize/concat css. 我正在使用Grunt将jade模板编译为html,uglify / concat js和最小化/ concat css。

During development I use the combination of connect and watch to serve my front-end and pick up changes automatically. 在开发过程中,我使用connect和watch的组合来为我的前端提供服务并自动获取更改。 In this I also use the 'source' js and css (and not the uglified/concatted/minified version). 在这里我也使用'source'js和css(而不是uglified / concatted / minified版本)。

Now when I generate the production html, js and css I wonder what the best solution is to change the inclusions of the *.min js and css. 现在,当我生成生产html,js和css时,我想知道最佳解决方案是什么来改变* .min js和css的包含。

To be more specific in my html I eg include: a.css b.css c.css a.js b.js 在我的html中更具体,我包括:a.css b.css c.css a.js b.js

for development this is fine, but when generating the production version I want: 对于开发这很好,但在生成我想要的生产版本时:

common-min.css common-min.js common-min.css common-min.js

Of course I don't want to change the Jade templates manually so I'm looking for a better approach, probably with the use of some Grunt plugin. 当然我不想手动更改Jade模板,因此我正在寻找更好的方法,可能需要使用一些Grunt插件。

You can pass data into your template that indicates what environment you are in, and then switch what you're including based on that. 您可以将数据传递到模板中,以指示您所处的环境,然后根据该模板切换您所包含的内容。

// In your route:
res.render("index", { env: "development" }); // maybe use NODE_ENV in here?

// Then in your jade template:
head
  if env == 'development'
    link(href="a.css", rel="stylesheet", type="text/css")
    link(href="b.css", rel="stylesheet", type="text/css")
  else
    link(href="min.css", rel="stylesheet", type="text/css")

See the Jade docs, and search for "conditionals": http://jade-lang.com/reference 请参阅Jade文档,并搜索“条件”: http//jade-lang.com/reference

@Marcel @Marcel

What you're looking for is a jade build block processor (or more commonly an HTML build block processor). 您正在寻找的是一个玉构建块处理器(或更常见的HTML构建块处理器)。 Unfortunately, for jade there only appears to be a gulp plugin, and not one for grunt. 不幸的是,对于玉,似乎只有一个gulp插件,而不是一个grunt插件。

https://www.npmjs.com/package/gulp-processjade https://www.npmjs.com/package/gulp-processjade

This example may suit your needs. 这个例子可能适合您的需求。

// build:js app.min.js
script(src='app.js')
// /build

@Jakerella @Jakerella

For each version, a production build is typically run once. 对于每个版本,生产构建通常运行一次。 So it's more effective to use a build server, task manager, or dependency manager; 因此,使用构建服务器,任务管理器或依赖项管理器更有效; and less effective to dynamically generate the production version of the HTML page within the server's request handler. 并且在服务器的请求处理程序中动态生成HTML页面的生产版本效率较低。 Don't use env with res.render() - logic used to build a page for production isn't needed when the entire server is built for production. 不要将envres.render() - 当为生产构建整个服务器时,不需要用于构建生产页面的逻辑。 This production logic also makes the request handler less modular, because it's coupled to the HTML page. 此生产逻辑还使请求处理程序不那么模块化,因为它耦合到HTML页面。 Only build servers (ones dedicated to builds) should incorporate build logic. 只有构建服务器(专用于构建的服务器)应该包含构建逻辑。 And, while the dynamically generated pages can be cached to avoid the repeated computation in rendering the production version of the HTML page, that's memory overhead that could be avoided. 并且,虽然可以缓存动态生成的页面以避免在呈现HTML页面的生产版本时重复计算,但这是可以避免的内存开销。

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

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