0

I've fallen in love with the ts-runtime-checks library and want to incorporate it into my NextJS project: issue is that it's a code transformer (changes how code compiles) and needs to be run before the main typescript compilation. Is there a way to hook this plugin into the NextJS compilation cleanly?

This library has a good, easy setup if one is using ts-loader for compilation. This is, from their documentation, as follows:

const TsRuntimeChecks = require("ts-runtime-checks").default;

options: {
      getCustomTransformers: program => {
        before: [TsRuntimeChecks(program)]
      }
}

I've seen in this question that there is a way to call ts-loader by altering the webpack in the project next.config however, it seems like this is just for compilation of additional files, and I can't get this working as a pre-compilation step.

I've also read in Next documentation that the compilation actually uses the SWC compiler and exposes some experimental options for adding custom plugins here. And, although it seems like these plugins have to probably be WASM-based, it also claims one can just enter the node package name as a plugin. However, the following next.config does not work (with failed to invoke <ts-runtime-checks path> as js transform plugin):

const TsRuntimeChecks = require("ts-runtime-checks").default;

const nextConfig = {
  reactStrictMode: true,
  // add ts runtime checks to pre-compilation
  experimental: {
    swcPlugins: [[ 'ts-runtime-checks', {} ]], 
  },
}

Is there any way to get Next to run this transformer natively? Should I try to build a SWC WASM wrapper for the library? Would appreciate insight from either direction...

Mikolaj Figurski
  • 133
  • 1
  • 2
  • 12

0 Answers0