traintocode

Editing Source Code

The transform hook allows you to manipulate the contents of each module before it's bundled. It receives two arguments: the raw code string and the id (usually the path to the file).

This is especially useful for:

  • Replacing placeholders
  • Adding custom macros
  • Injecting imports
transform(code, id) {
  if (id.endsWith('.js')) {
    return code.replace('__HELLO__', '"Hello from plugin!"')
  }
}

If you want to change how source code behaves or inject logic, this is your hook.

Exercise

Add a transform hook that looks for MY_NAME and replaces it with your actual name in any .ts or .js file.

    You cannot do the coding exercises on this device, visit this page on a larger screen.
    myPlugin.ts
    Loading...