我已经构建了一个 js 库,我想使用 grunt 将其打包为 AMD 模块。还有其他连接库文件的任务,所以我将非 AMD 模块作为开始。
这是看起来的Gruntfile
样子:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
all: {
options: {
separator: '\n\n',
stripBanners: true,
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n\n'
},
files: {
'dist/<%= pkg.name %>.js': ['src/file1.js', 'src/file2.js'], // this works
'dist/<%= pkg.name %>.amd.js': ['src/file1.js', 'src/file2.js'] // this is meant to create the AMD version of the library
}
}
}
});
grunt.registerTask('default', ['concat']);
};
生成的文件(用于 target library.amd.js
)旨在包含包含library.js
以下define(..., ..., ...)
部分的内容require.js
:
define('library', ['dependency1', 'dependency2'], function(dep1, dep2) {
// content of library.js here
});
我怎么能这样做呢?我曾考虑在 AMD 目标的构建源中添加header.js
(for define('library...
) 和footer.js
(for });
),但希望有一个更优雅的解决方案。我还查看了 grunt 模板,但找不到将完整文件包含到模板中的方法。