Microbundle: typescript definitions directory

Created on 28 Feb 2019  路  5Comments  路  Source: developit/microbundle

I just found out about microbundle, I tried it and I think it's awesome, however I cant manage to set the declaration files directory other that the dist folder. I tried setting the "types" and "typings" fields in the package.json file, I also tried to set the "declarationDir" of my tsconfig.json file without success.

I am starting a project using rollup but now I am considering to switch to microbundle, it will save me some config time.

I've noticed the "useTsconfigDeclarationDir" option in the rollup-plugin-typescript2 has not been set to true, did that cause any issues in the past? otherwise it would be a good idea to set it. If you wish I'd be more than happy to create a pull request.

Alternatively a CLI option can be setup (_--declaration_ or _--declartionDir_ for example) or get the directory from the "types" or "typings" of the package.json.

enhancement good first issue help wanted

All 5 comments

Ah interesting - are you saying that if we had Microbundle use useTsconfigDeclarationDir:true, it woudl respect the typings field you defined in your package.json and output d.ts files there instead? If so that sounds like a great feature.

In general, we're vastly in favor of infering any configuration from well-known package.json fields whenever possible. This seems like a great case where we can!

Ah interesting - are you saying that if we had Microbundle use useTsconfigDeclarationDir:true, it woudl respect the typings field you defined in your package.json and output d.ts files there instead? If so that sounds like a great feature.

In general, we're vastly in favor of infering any configuration from well-known package.json fields whenever possible. This seems like a great case where we can!

In fact useTsconfigDeclarationDir: true will respect the "declarationDir" in your tsconfig.json file, but it would take very little effort to implement for it to check the "typings" (or "types") fields in your package.json as a fallback, in case you don't really need a tsconfig.json file.

This is what I did, I hope you understand what I am about to write... if not have a look at my version of the index.js file.

// this is what you have in your code now
let config = {
  // ...
  outputOptions: {
    // ...
    file: resolve(
      options.cwd,
      (format === 'es' && moduleMain) ||
      (format === 'umd' && umdMain) ||
      cjsMain,
    ),
  },
};

I put that in a variable because I'll need it twice.

// this is the variable
const outputFile = resolve(
  options.cwd,
  (format === 'es' && moduleMain) || (format === 'umd' && umdMain) || cjsMain,
);

let config = {
  // ...
  inputOptions: {
    // ...
    // plugins
    useTypescript &&
      typescript({
        typescript: require('typescript'),
        cacheRoot: `./.rts2_cache_${format}`,
        tsconfigDefaults: {
          compilerOptions: {
            sourceMap: options.sourcemap,
            declaration: true,
            jsx: options.jsx,
            declarationDir: pkg.typings || pkg.types || dirname(outputFile), // this line !!!
          },
        },
        tsconfigOverride: {
          compilerOptions: {
            target: 'esnext',
          },
        },
        useTsconfigDeclarationDir: true,  // this line !!!
      }),
  // ...
  outputOptions: {
    // ...
    file: outputFile,
  },
};

This will will respect tsconfig.json over package.json, if you want the opposite some changes need to be made.

It works perfectly for me, give it a try. I hope it helps.

Hi there, I think you didn't get notified about my previous comment, Anyway I am starting my new library using rollup because I need my types in a different directory, looking forward to use microbundle next time, it would really make my life easier. I hope my ideas and comments serve you in some way. Need some help, just ask, happy to help. Once again great job!.

Should be easy enough to add 馃憤

I'm trying to write this, and if it goes well a PR will follow

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breadadams picture breadadams  路  3Comments

MarkLyck picture MarkLyck  路  4Comments

adriengibrat picture adriengibrat  路  4Comments

jlkiri picture jlkiri  路  4Comments

kesla picture kesla  路  4Comments