Attempt at getting .d.ts files in meteor packages to be usefull

I was looking for a way to get d.ts files in meteor packages to be usefull in the projects using those packages.

I took a look at the typescript package came up with this idea:

  extensions: ["ts", "tsx"],
}, function () {
  return new TypeScriptCompiler({
    react: true,
    typescript: true,
  }, (babelOptions, file) => {
    if (file.hmrAvailable() && ReactFastRefresh.babelPlugin) {
      babelOptions.plugins = babelOptions.plugins || [];

class TypeScriptCompiler extends BabelCompiler {
  processFilesForTarget(inputFiles) {
    return super.processFilesForTarget(inputFiles
      .map(file => { //This map is my attempt at copying the d.ts files. Everything else is unchanged
        if (file.getPathInPackage().endsWith(".d.ts") && !file.getPathInPackage().startsWith("node_modules")) {
          const pathInPackage = file.getPathInPackage()
          const pathBits = pathInPackage.split('/')
          const fileName = pathBits[pathBits.length - 1] // there should probably be a better way to get the file name !?
          const packageName = file.getPackageName()
          const sourcePath = file._resourceSlot.packageSourceBatch.sourceRoot + '/' + pathInPackage
          const destinationPath = file._resourceSlot.packageSourceBatch.processor.sourceRoot + '/node_modules/meteor/' + packageName + '/' + fileName
          console.log(sourcePath + ' -> ' + destinationPath )
          //  /Users/jan-michaelpilgenroeder/Code/meteor-packages/sdui-table/imports/ui/MeteorDataAutoTable.d.ts -> /Users/jan-michaelpilgenroeder/Code/sdui-dev/node_modules/meteor/janmp:sdui-table/MeteorDataAutoTable.d.ts
          //  /Users/jan-michaelpilgenroeder/Code/meteor-packages/sdui-table/imports/ui/MeteorDataAutoTable.d.ts -> /Users/jan-michaelpilgenroeder/Code/sdui-dev/node_modules/meteor/janmp:sdui-table/MeteorDataAutoTable.d.ts
          // the paths look good for my local packages (this should probably fail on windows though and I have not tried this with atmosphere packages)
          // the idea was to use fs.copyFile now, but I can't get it imported or required
        return file;
      // TypeScript .d.ts declaration files look like .ts files, but it's
      // important that we do not compile them using the TypeScript
      // compiler, as it will fail with a cryptic error message.
      file => ! file.getPathInPackage().endsWith(".d.ts")

Great work!

There was some previous discussion around whether or not it was a good idea to modify node_modules or not. It’s definitely nice because it’s a zero configuration solution, but it does kind of feel like npm should be the only thing updating those files.

Another option could be to have a typings or .meteor/local/typings and the default tsconfig.json could reference that directory for Meteor projects.

The problem ist that I have not figured out yet how to actually do the copying. I can’t require or import fs in that file.

What happens when you try to require or import the fs module?

With import (first line and top level of file):

   While loading plugin `compile-typescript` from package `janmp:typescript`:
   packages/compile-typescript_plugin.js:16: 'import' and 'export' may only appear at the top level. (16:0)

with require:

   While loading plugin `compile-typescript` from package `janmp:typescript`:
   packages/compile-typescript_plugin.js:16:10: require is not defined

here is the repo GitHub - JanMP/typescript