如果你使用r.js 优化器那么这个过程可以为您处理。在你的scripts
文件夹将所有模块、库和其他代码保留为开发目的而未压缩。我经常有这样的事情
scripts/
lib/
modules/
准备好部署时,创建一个build.js
并配置各种选项。我将给出一个我使用过的类似配置的示例:
({
baseUrl: '../scripts',
dir: '../scripts.min',
paths: {
'jquery': 'lib/jquery'
},
removeCombined: true,
optimize: 'uglify',
preserveLicenseComments: false,
uglify: {
max_line_length: 3500,
no_copyright: true
},
modules: [
{
name: 'module1'
},
{
name: 'module2',
exclude: ['jquery']
}
]
})
有关每个选项的更多详细信息可以在这个示例配置中找到但我会提请注意dir
and removeCombined
dir
显然是你的脚本最终的位置。我倾向于在我的脚本旁边创建一个带有后缀的文件夹.min
或类似的东西。然后,当您准备好投入生产时,只需更改您的需求配置即可baseUrl
to scripts.min
require.config({
baseUrl: '/scripts.min' // Now the site will load scripts from the optimised folder
// Other options
})
默认情况下,r.js 会复制所有脚本,无论它们是否已经合并到另一个 js 文件中。设置removeCombined
to true
将确保您的scripts.min
文件夹仅包含生产所需的文件。