【问题标题】:Grunt compress : How could I only include runtime node module dependencies?Grunt compress:我怎么能只包含运行时节点模块依赖项?
【发布时间】:2023-04-07 19:20:01
【问题描述】:
我的申请是MEAN stack 风格。我想生成一个包含所有 Nodejs 和 AngularJs 文件的包,所以我可以解压缩包并在其他环境中运行。
我使用grunt-contrib-compress 来压缩并生成一个zip 文件。一切运行良好,但包含许多开发节点模块,例如 grunt*。我只需要在 package.json 中定义的运行时节点模块。它将大大减小包的大小。
我可以一个一个地包含节点模块,但是有没有一个好方法只在打包时包含运行时模块?
【问题讨论】:
标签:
node.js
angularjs
gruntjs
package
mean-stack
【解决方案1】:
好的,我找到了一个解决方案,它加载 package.json 并将运行时依赖项映射到目标文件夹中。
compress: {
main: {
options: {
archive: 'myapp.zip'
},
files: [
{src: ['dist/**','app/**','config/**','server.js'],dest:'.'},
{src: Object.keys(require('./package.json').dependencies).map(function(module){
return "node_modules/" +module+"/**"
}),dest:'.'},
]
}
}
【解决方案2】:
我今天有完全相同的问题,在提出并提出非常相似的解决方案后,我找到了您的问题。这是我的,类似但略有不同的方法:
function getDependencies(pkg) {
return Object.keys(pkg.dependencies).map(function(val) { return val + '/**'; });
}
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
var config = {
pkg: pkg,
clean: ["public/"],
compress: {
validate: {
options: {
archive: 'public/Lambda.zip'
},
files: [
{ expand: true, cwd: 'src/', src: ['**'], dest: '/' },
{ expand: true, cwd: 'node_modules/', src: getDependencies(pkg), dest: '/node_modules' }
]
}
}
};
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.registerTask('build', ['clean', 'compress']);
}