New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@nevware21/grunt-ts-plugin

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nevware21/grunt-ts-plugin

TypeScript Compilation Plugin for GruntJS

  • 0.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
188
increased by16.77%
Maintainers
1
Weekly downloads
 
Created
Source

@Nevware21 grunt-ts-plugin

Grunt-ts-plugin is an npm package that handles TypeScript compilation work in GruntJS build scripts.

Grunt-ts-plugin provides a Grunt-compatible wrapper for the tsc command-line compiler using the --project switch to compile existing TypeScript projects files (tsconfig.json), and provides some additional functionality that improves the TypeScript development workflow.

The plugin itself is both written and compiled using TypeScript and it also uses itself to compile via grunt.

Unlike other plugins (such as grunt-ts, this plugin focuses on providing support for using your existing TypeScript project files (tsconfig.json) rather than exposing all of the command-line options of the tsc compiler. However, it does provide the capability to pass additional command-line options via the additionalFlags option.

As a consequence of this it does NOT support the use of all standard GruntJS functionality such as the use of the files object, etc.

Grunt-ts-plugin Features

  • Supports TypeScript Projects via tsconfig.json via the tsc --project option and therefore all features of the TypeScript tsc compiler.
  • Allows the developer to select a custom TypeScript compiler version for their project, or even use a custom (in-house) version.
  • Dynamically process the tsc errors to pass or fail the build
  • Add "extra" files to an existing tsconfig.json via the src properties, these are dynamically added to the tsconfig.json files or include properties.

Unsupported Grunt features

  • Identifying files to be compiled (*.ts) via the use of the files object or globbing, etc.
  • Path Mapping feature as it is not supported by tsc from the command-line

Support for tsc Switches

Grunt-ts provides explicit support for most tsc switches. Any arbitrary switches can be passed to tsc via the additionalFlags feature.

Quickstart

npm install @nevware21/grunt-ts-plugin --save-dev

As this is a development only tool if only needs to be intalled into your root project (if you have a mono-repo) and only as a devDependency

To install @nevware21/grunt-ts-plugin, you must already have installed TypeScript and GruntJS.

  • If you don't have TypeScript installed in your project, run npm install typescript --save-dev.
  • If you don't have GruntJS installed in your project, run npm install grunt --save-dev.
  • If you have never used Grunt on your system, install the grunt-cli: npm install grunt-cli.

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

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

Options

The options can be specified at the global options or task level, with the task level values overridding any value defined in the global options

Task only level options
NameTypeDescription
tsconfigstringThe path to the tsConfig file to use, when specified
srcstringstring[]
outstringConcatenate the output into a single file using the tsc --out parameter. If the tscConfig also includes an outDir this value will be ignored
Common options: Global and Task
NameTypeDescription
srcstringstring[]
additionalFlagsString
Default: Empty String
Pass in additional flags to the tsc compiler (added to the end of the command line)
failOnTypeErrorsBoolean
Defaults: false
Should the compile run fail when type errors are identified
tscPathString
Defaults: reverse scan from project path for node_modules/typescript/bin folder
Identify the root path of the version of the TypeScript is installed, this may include be either the root folder of where the node_modules/typescript/bin folder is located or the location of the command-line version of tsc.
compilerString<br/ >Defaults: to "tsc" within the located or defined tscPathIdentify the complete path to the command line version of tsc
onErrorErrorHandlerResponse
This callback function will be called when an error matching "error: TS\d+:" is found, the errorNumber is the detected value and line is the entire line containing the error message.
debugBoolean
Defaults: false
Log additional debug messages during the build, you can also enable grunt --verbose mode.
logOutputBoolean
Defaults: false
Log the output of the execute response
module.exports = function(grunt) {
  grunt.initConfig({
    ts: {
        options: { // Shared options for all projects
            debug: true,
            comments: true
        },
        "shared_utils": {
            tsconfig: "./shared/tsconfig.json"
        },
        "ts_plugin": {
            debug: false,   // Override the default defined in the options
            tsconfig: "./ts-plugin/tsconfig.json"
        }
    }
  });

  grunt.loadNpmTasks("@nevware21/grunt-ts-plugin");
  grunt.registerTask("ts_plugin", [ "ts:ts_plugin" ]);
  grunt.registerTask("shared_utils", [ "ts:shared_utils" ]);
};

OnErrorHandler

The onError callback allows you to re-classify errors that are emitted by the tsc compiler to cause them to be Ignored, just logged or cause a build failure.

This error handler is called for every tsc output line that contains an error matching the error: TS(\d+): regular expression where the (\d+) group identifies the error number.

OnErrorHandler = (errorNumber: string, line?: string) => ErrorHandlerResponse;

ParamDescription
errorNumberThe identified error number
lineThe full line containing the error

Example Error line: error TS6059: File 'xxxx' is not under 'rootDir' 'xxx'. 'rootDir' is expected to contain all source files.

Note:

Even if you qualify an error to be ignored, if the tsc compiler returns a non-zero value this will still be treated as a failure. To "avoid" that scenario you will need to provide a compiler option and wrap the tsc compiler to change any returned value.

ErrorHandlerResponse
NameValueDescription
Undefined0The handler did not identify whether this should be treated as an error, warning or ignore. So follow normal built in handling. Null and undefined responses are treated the same as this value.
Ignore1Ignore this error with no logging
Silent2Include the error in the log, but don't treat as an error or warning
Error3Treat as an error and fail the build
module.exports = function(grunt) {

  // A simple onError handler which ignores error 6082
  function tsErrorHandler(errorNo, line) {
    if (errorNo === "6082") {
      return 2;
    }

    return 0;
  }

  grunt.initConfig({
    ts: {
        options: { // Shared options for all projects
            debug: true,
            comments: true,
            onError: tsErrorHandler
        },
        "shared_utils": {
            tsconfig: "./shared/tsconfig.json"
        },
        "ts_plugin": {
            debug:false,   // Override the default defined in the options
            tsconfig: "./ts-plugin/tsconfig.json"
        }
    }
  });

  grunt.loadNpmTasks("@nevware21/grunt-ts-plugin");
  grunt.registerTask("ts_plugin", [ "ts:ts_plugin" ]);
  grunt.registerTask("shared_utils", [ "ts:shared_utils" ]);
};

Contributing

Thank you for your interest in contributing!

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

See The ChangeLog

Keywords

FAQs

Package last updated on 19 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc