【问题标题】:Grunt generates new node_modulesGrunt 生成新的 node_modules
【发布时间】:2018-05-16 09:39:41
【问题描述】:

我正在创建一个 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 生成过程?因为我认为我不需要它们,它使编译过程非常缓慢。

【问题讨论】:

    标签: javascript node.js typescript gruntjs grunt-ts


    【解决方案1】:

    以下配置应满足您的要求。它将忽略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: falseexpand: true 都已从 options 对象中删除。

    • files 属性已替换为 srcdest 属性,其值分别设置为 src/**/*.tsbuild/

    结果目录结构示例:

    如下目录结构:

    .
    ├── 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
    │   └── ...
    └── ...
    

    【讨论】:

    • 这正是我所需要的!谢谢!我确实注意到的一件事是,当有多个 Typescript 文件时,Grunt 只在构建目录中重构文件夹结构。而且因为我只使用 1 个文件进行测试,所以它没有在构建目录中创建地图。感谢您的帮助:)
    【解决方案2】:

    您的项目中有 tsconfig.json 设置吗?

    您可能需要排除 node_modules 目录(请参阅文档:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)。

    然后您可以在 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"]);
    };
    

    带有相应的 tsconfig.json 文件,如:

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

    }

    注意:使用 tsconfig.json 是使用 TypeScript 的最佳方式。

    希望这有帮助吗?

    【讨论】:

    • 我已经尝试过了,但出于某种原因,Grunt 似乎不尊重我的tsconfig.json 文件。如果我使用 tsconfig.json 文件并将其添加到我的 Grunt 配置中,它不会执行任何操作。我使用了以下 grunt 配置:module.exports = function(grunt) { grunt.config.set('ts', { dev: { default : { tsconfig: './tsconfig.json' }, files: [ { src: ['**/*.ts'], dest: 'build/' } ], } }); grunt.loadNpmTasks('grunt-ts'); };
    • 尝试将 ['**/*.ts' , "!node_modules/**"] 添加到 src。它应该排除 node_modules 目录。我不确定为什么您的 Grunt 设置不尊重您的 tsconfig.json。你试过我提到的极简设置吗?正如 grunt-ts github 页面所指出的,仅使用 tsconfig.json 是最佳实践。
    • 我试过了,当我这样做时,不会生成 node_modules。但如果我这样做,构建文件夹结构就会变得扁平。所以我所有的 Javascript 文件都在同一个文件夹中。而且我需要build目录的文件夹结构和src目录的一样
    • 不确定您的确切设置和配置。当您指定src 如下:["src/**/*.ts"] 时会发生什么?它会将文件夹结构保留在 outDir 中吗?也许您需要将rootDir: "." 设置为您的选项?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多