Javascript在开发中需要JS,但在生产中编译
我开始评估javascript模块工具,如用于javascript模块化的RequireJS。这似乎很有用,尤其是在开发过程中,所以每当我更改一个依赖文件时,我不需要将所有js文件重新编译成Javascript在开发中需要JS,但在生产中编译,javascript,requirejs,modularity,Javascript,Requirejs,Modularity,我开始评估javascript模块工具,如用于javascript模块化的RequireJS。这似乎很有用,尤其是在开发过程中,所以每当我更改一个依赖文件时,我不需要将所有js文件重新编译成mylib-.js 我的应用程序同时发布html和javascript文件,在生产中,我希望使用javascript文件的编译版本 所以在开发过程中,我的html文件可能看起来像 <html> <head> <script data-main="scripts/mai
mylib-.js
我的应用程序同时发布html和javascript文件,在生产中,我希望使用javascript文件的编译版本
所以在开发过程中,我的html文件可能看起来像
<html>
<head>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
</html>
但在生产中,我希望它看起来更像
<html>
<head>
<script src="mylib-1.0.js"></script>
</head>
</html>
如果我分发一个编译过的文件,我认为不需要引用requirejs
有没有一种方法可以做到这一点,而不必在发布应用程序之前手动更改html文件?RequireJs有一个,它可以帮助您缩小和连接模块。它有很多选项,可能很难使用,但是使用像or(尤其是)这样的构建工具,它会变得更容易,它使用GruntJs来构建
在这两种情况下,您都可以使用rjs
任务(优化模块),但同样Yeoman更容易一些,因为它有生成器,可以为您配置:
// usemin handler should point to the file containing
// the usemin blocks to be parsed
'usemin-handler': {
html: 'index.html'
},
// rjs configuration. You don't necessarily need to specify the typical
// `path` configuration, the rjs task will parse these values from your
// main module, using http://requirejs.org/docs/optimization.html#mainConfigFile
//
// name / out / mainConfig file should be used. You can let it blank if
// you're using usemin-handler to parse rjs config from markup (default
// setup)
rjs: {
// no minification, is done by the min task
optimize: 'none',
baseUrl: './scripts',
wrap: true,
name: 'main'
},
在index.html
中,您只需使用注释行指定哪些js文件应缩小/连接到哪个输出文件:
<!-- build:js scripts/amd-app.js -->
<script data-main="scripts/main" src="scripts/vendor/require.js"></script>
<!-- endbuild -->
我的建议:使用Yeoman让生活更轻松(至少在处理缩小/连接时是这样)。首先,您必须使用。在那之后,你可以像AMD那样的条带下载器。至少您必须找到一种方法来更改索引html中的url
看看哪一个可以使整个过程自动化,有一大堆任务可以帮助您完成这一过程。如果我将index.html与我的应用程序一起分发,它仍然会依赖于scripts/vendor/require.js,对吗?不,这是源代码。当Yeoman构建时,它会将所有内容复制到目标文件夹(本例中为dist)。看到editOh了,我看到了,所以它也编译html文件?它做了很多事情:优化图像文件,将coffeescript编译成javascript,将compass编译成css,缩小,制作缓存清单,…grunt/yeoman的一个功能。您必须将其视为一组有用的任务(其中rjs是其中之一,它是requirejs优化器)。
<script src="scripts/15964141.amd-app.js"></script>