简体   繁体   English

Grunt生成新的node_modules

[英]Grunt generates new node_modules

I'm creating a grunt config to compile all my Typescript files to Javascript. 我正在创建一个grunt配置,以将我所有的Typescript文件编译为Javascript。 I want to save all my generated Javascript files in a build folder but also keep the same folder structure. 我想将所有生成的Javascript文件保存在build文件夹中,但还要保持相同的文件夹结构。

Example: src/controllers/myController.ts will compile to: build/controllers/myController.js 示例: src/controllers/myController.ts将编译为: build/controllers/myController.js

I created a grunt config which does this exact thing but for some reasons it also generates a node_modules folder in the build directory and this takes a lot of time. 我创建了一个grunt config来完成此操作,但是由于某些原因,它还在build目录中生成了一个node_modules文件夹,这花费了很多时间。 My grunt config looks as followed: 我的咕unt的配置如下所示:

    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');
    };

Is there any way to disable the node_modules generation process? 有什么方法可以禁用node_modules生成过程吗? Because I don't think i need them and it makes the compiling process incredibly slow. 因为我认为我不需要它们,这使编译过程非常缓慢。

The following configuration should meet your requirement. 以下配置应满足您的要求。 It will ignore the node_modules directory and reproduce the same source directory structure, (as found in src/ ), in the resultant build directory: 它将忽略node_modules目录和再现相同的源的目录结构,(如在发现src/ ),在所得build目录:

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');
};

Notes: 笔记:

  • The rootDir property was added to the options object and it's value set to 'src/' . rootDir属性已添加到options对象,并且其值设置为'src/'

  • Both flatten: false and expand: true have been removed from the options object. flatten: falseexpand: true都已从options对象中删除。

  • The files property has been replaced with src and dest properties with their values set to src/**/*.ts and build/ respectively. files属性已替换为srcdest属性,其值分别设置为src/**/*.tsbuild/

Example of resultant directory structure: 结果目录结构的示例:

The following directory structure: 以下目录结构:

.
├── src
│   ├── another-folder
│   │   └── quux.ts
│   ├── controllers
│   │   └── myController.ts
│   └── foo.ts
├── Gruntfile.js
├── node_modules
│   └── ...
└── ...

Will result as follows after running $ grunt ts : 运行$ 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
│   └── ...
└── ...

Do you have a tsconfig.json setup in your project? 您的项目中是否有tsconfig.json设置?

Probably you need to exclude the node_modules directory there (see documentation: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html ). 可能您需要在此排除node_modules目录(请参阅文档: https : //www.typescriptlang.org/docs/handbook/tsconfig-json.html )。

You can then use the tsconfig.json in your grunt config (see getting started section: https://github.com/TypeStrong/grunt-ts ). 然后,您可以在grunt配置中使用tsconfig.json(请参阅入门部分: https : //github.com/TypeStrong/grunt-ts )。

module.exports = function(grunt) { 
  grunt.initConfig({
    ts: {
      default : {
        tsconfig: './tsconfig.json'
      }
  }}); 
  grunt.loadNpmTasks("grunt-ts");
  grunt.registerTask("default", ["ts"]);
};

With a corresponding tsconfig.json file like: 带有对应的tsconfig.json文件,例如:

{
"include": [
    "src/**/*.ts*"
],
"exclude": [
    "node_modules"
],
"compilerOptions": {
    "target": "ES5",
    "fast": "watch,
    "sourceMap": false,
    "module": "system",
    "removeComments": true,
    "outDir": "build/",
    "rootDir" ".",
...
}

} }

Note: Using a tsconfig.json is the best way to use TypeScript. 注意:使用tsconfig.json是使用TypeScript的最佳方法。

Hope this helps? 希望这可以帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM