我正在创建一个 grunt 配置来将我的所有 Typescript 文件编译为 Javascript。我想将所有生成的 Javascript 文件保存在构建文件夹中,但也保持相同的文件夹结构。
示例:
src/controllers/myController.ts
将编译为:build/controllers/myController.js
我创建了一个 grunt 配置来完成这件事,但由于某些原因,它还在构建目录中生成了一个 node_modules 文件夹,这需要很多时间。我的 grunt 配置如下所示:
module.exports = function(grunt) {
grunt.config.set('ts', {
dev: {
files: [
{
src: ['**/*.ts'],
dest: 'build/'
}
],
options: {
target: 'es5',
fast: 'watch',
comments: false,
sourceMap: false,
failOnTypeErrors: true,
flatten: false,
expand: true,
module: 'system',
moduleResolution: 'classic'
}
}
});
grunt.loadNpmTasks('grunt-ts');
};
有什么方法可以禁用node_modules生成过程吗?因为我认为我不需要它们,这使得编译过程非常慢。
最佳答案
以下配置应该可以满足您的要求。它将忽略 node_modules
目录并在生成的 build
目录中重现相同的源目录结构(如 src/
中所示):
module.exports = function(grunt) {
grunt.config.set('ts', {
dev: {
options: {
rootDir: 'src/',
target: 'es5',
fast: 'watch',
comments: false,
sourceMap: false,
failOnTypeErrors: true,
module: 'system',
moduleResolution: 'classic'
},
src: 'src/**/*.ts',
dest: 'build/'
}
});
grunt.loadNpmTasks('grunt-ts');
};
注释:
rootDir
属性已添加到options
对象,并将其值设置为'src/'
。flatten: false
和expand: true
均已从options
对象中删除。files
属性已替换为src
和dest
属性,其值设置为src/**分别为/*.ts
和build/
。
结果目录结构示例:
以下目录结构:
.
├── src
│ ├── another-folder
│ │ └── quux.ts
│ ├── controllers
│ │ └── myController.ts
│ └── foo.ts
├── Gruntfile.js
├── node_modules
│ └── ...
└── ...
运行$ grunt ts
后结果如下:
.
├── build
│ ├── another-folder
│ │ └── quux.js
│ ├── controllers
│ │ └── myController.js
│ └── foo.js
├── src
│ ├── another-folder
│ │ └── quux.ts
│ ├── controllers
│ │ └── myController.ts
│ └── foo.ts
├── Gruntfile.js
├── node_modules
│ └── ...
└── ...
关于javascript - Grunt 生成新的node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367362/