Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source files with only type declarations don't get declarations generated #106

Closed
houfio opened this issue Aug 23, 2018 · 4 comments · Fixed by #107 or #406
Closed

Source files with only type declarations don't get declarations generated #106

houfio opened this issue Aug 23, 2018 · 4 comments · Fixed by #107 or #406
Assignees
Labels
kind: bug Something isn't working properly solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: type-only / emit-less imports Related to importing type-only files that will not be emitted

Comments

@houfio
Copy link
Contributor

houfio commented Aug 23, 2018

What happens and why it is wrong

When building a project with files that only have type declarations, the declarations don't get generated for those files. Using tsc generated the declarations and declaration maps as expected.

screenshot from 2018-08-23 11-21-12

Versions

  • typescript: 3.0.1
  • rollup: 0.64.1
  • rollup-plugin-typescript2: 0.16.1

rollup.config.js

import { dirname, join } from 'path';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';

const root = process.cwd();
const { main, module, types, dependencies = {}, peerDependencies = {} } = require(join(root, 'package.json'));

export default {
  input: join(root, 'src/index.ts'),
  output: [
    {
      file: join(root, main),
      format: 'cjs',
      sourcemap: true
    },
    {
      file: join(root, module),
      format: 'es',
      sourcemap: true
    }
  ],
  external: [
    ...Object.keys(dependencies),
    ...Object.keys(peerDependencies)
  ],
  plugins: [
    resolve(),
    typescript({
      clean: true,
      useTsconfigDeclarationDir: true,
      tsconfigOverride: {
        compilerOptions: {
          rootDir: join(root, 'src'),
          declarationDir: join(root, dirname(types))
        }
      }
    }),
    commonjs()
  ]
};

tsconfig.json

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es5",
    "lib": [
      "es6",
      "dom"
    ],
    "jsx": "react",
    "moduleResolution": "node",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "strict": true,
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "src/"
  ]
}

package.json

{
  "scripts": {
    "prebuild": "lerna exec -- rimraf lib",
    "build": "lerna exec --parallel -- rollup --config \\$LERNA_ROOT_PATH/rollup.config.js"
  },
  "devDependencies": {
    "lerna": "^3.1.2",
    "rimraf": "^2.6.2",
    "rollup": "^0.64.1",
    "rollup-plugin-commonjs": "^9.1.5",
    "rollup-plugin-node-resolve": "^3.3.0",
    "rollup-plugin-typescript2": "^0.16.1",
    "typescript": "^3.0.1"
  }
}

plugin output with verbosity 3

Log
lerna notice cli v3.1.4
lerna info exec in 1 package: "rollup --config $LERNA_ROOT_PATH/rollup.config.js"
@: <path>/src/index.ts → lib/index.js, lib/index.es.js...
@: rpt2: typescript version: 3.0.1
@: rpt2: tslib version: 1.9.3
@: rpt2: rollup-plugin-typescript2 version: 0.16.1
@: rpt2: plugin options:
@: {
@:     "verbosity": 3,
@:     "clean": true,
@:     "useTsconfigDeclarationDir": true,
@:     "tsconfigOverride": {
@:         "compilerOptions": {
@:             "rootDir": "<path>/src",
@:             "declarationDir": "<path>/lib"
@:         }
@:     },
@:     "check": true,
@:     "cacheRoot": "<path>/.rpt2_cache",
@:     "include": [
@:         "*.ts+(|x)",
@:         "**/*.ts+(|x)"
@:     ],
@:     "exclude": [
@:         "*.d.ts",
@:         "**/*.d.ts"
@:     ],
@:     "abortOnError": true,
@:     "rollupCommonJSResolveHack": false,
@:     "typescript": "version 3.0.1",
@:     "transformers": [],
@:     "tsconfigDefaults": {}
@: }
@: rpt2: rollup config:
@: {
@:     "external": [
@:         "react",
@:         "",
@:         ""
@:     ],
@:     "inlineDynamicImports": false,
@:     "input": "<path>/src/index.ts",
@:     "chunkGroupingSize": 5000,
@:     "perf": false,
@:     "plugins": [
@:         {
@:             "name": "node-resolve"
@:         },
@:         {
@:             "name": "rpt2"
@:         },
@:         {
@:             "name": "commonjs"
@:         }
@:     ],
@:     "entry": "<path>/src/index.ts"
@: }
@: rpt2: built-in options overrides: {
@:     "noEmitHelpers": false,
@:     "importHelpers": true,
@:     "noResolve": false,
@:     "noEmit": false,
@:     "inlineSourceMap": false,
@:     "outDir": "<path>/.rpt2_cache/placeholder",
@:     "moduleResolution": 2
@: }
@: rpt2: parsed tsconfig: {
@:     "options": {
@:         "module": 6,
@:         "target": 1,
@:         "lib": [
@:             "lib.es2015.d.ts",
@:             "lib.dom.d.ts"
@:         ],
@:         "jsx": 2,
@:         "moduleResolution": 2,
@:         "declaration": true,
@:         "declarationMap": true,
@:         "sourceMap": true,
@:         "strict": true,
@:         "allowSyntheticDefaultImports": true,
@:         "rootDir": "<path>/src",
@:         "declarationDir": "<path>/lib",
@:         "configFilePath": "<path>/tsconfig.json",
@:         "noEmitHelpers": false,
@:         "importHelpers": true,
@:         "noResolve": false,
@:         "noEmit": false,
@:         "inlineSourceMap": false,
@:         "outDir": "<path>/.rpt2_cache/placeholder"
@:     },
@:     "fileNames": [],
@:     "typeAcquisition": {
@:         "enable": false,
@:         "include": [],
@:         "exclude": []
@:     },
@:     "raw": {
@:         "compilerOptions": {
@:             "module": "esnext",
@:             "target": "es5",
@:             "lib": [
@:                 "es6",
@:                 "dom"
@:             ],
@:             "jsx": "react",
@:             "moduleResolution": "node",
@:             "declaration": true,
@:             "declarationMap": true,
@:             "sourceMap": true,
@:             "strict": true,
@:             "allowSyntheticDefaultImports": true,
@:             "rootDir": "<path>/src",
@:             "declarationDir": "<path>/lib"
@:         },
@:         "include": [
@:             "src/"
@:         ],
@:         "compileOnSave": false
@:     },
@:     "errors": [
@:         {
@:             "messageText": "No inputs were found in config file '<path>/tsconfig.json'. Specified 'include' paths were '[\"src/\"]' and 'exclude' paths were '[\"<path>/lib\"]'.",
@:             "category": 1,
@:             "code": 18003
@:         }
@:     ],
@:     "wildcardDirectories": {
@:         "<path>/src/": 1
@:     },
@:     "compileOnSave": false,
@:     "configFileSpecs": {
@:         "includeSpecs": [
@:             "src/"
@:         ],
@:         "excludeSpecs": [
@:             "<path>/lib"
@:         ],
@:         "validatedIncludeSpecs": [
@:             "src/"
@:         ],
@:         "validatedExcludeSpecs": [
@:             "<path>/lib"
@:         ],
@:         "wildcardDirectories": {
@:             "<path>/src/": 1
@:         }
@:     }
@: }
@: rpt2: included:
@: '[
@:     "*.ts+(|x)",
@:     "**/*.ts+(|x)"
@: ]'
@: rpt2: excluded:
@: '[
@:     "*.d.ts",
@:     "**/*.d.ts"
@: ]'
@: rpt2: Ambient types:
@: rpt2:     <path>/node_modules/@types/estree/index.d.ts
@: rpt2:     <path>/node_modules/@types/node/index.d.ts
@: rpt2:     <path>/node_modules/@types/prop-types/index.d.ts
@: rpt2:     <path>/node_modules/@types/react/index.d.ts
@: rpt2: ambient types changed, redoing all semantic diagnostics
@: rpt2: transpiling '<path>/src/index.ts'
@: rpt2:     cache: 'cb5a72ce1d9ca58fa672e942573d3ba70cc6e65e'
@: rpt2:     cache miss
@: rpt2:     cache: 'cb5a72ce1d9ca58fa672e942573d3ba70cc6e65e'
@: rpt2:     cache miss
@: rpt2:     cache: 'cb5a72ce1d9ca58fa672e942573d3ba70cc6e65e'
@: rpt2:     cache miss
@: rpt2: generated declarations for '<path>/src/index.ts'
@: rpt2: dependency '<path>/src/constants.ts'
@: rpt2:     imported by '<path>/src/index.ts'
@: rpt2: resolving './constants'
@: rpt2:     to '<path>/src/constants.ts'
@: rpt2: transpiling '<path>/src/constants.ts'
@: rpt2:     cache: 'c0821b0be34b449d7f93ba52a5b0708ea09e39f7'
@: rpt2:     cache miss
@: rpt2:     cache: 'c0821b0be34b449d7f93ba52a5b0708ea09e39f7'
@: rpt2:     cache miss
@: rpt2:     cache: 'c0821b0be34b449d7f93ba52a5b0708ea09e39f7'
@: rpt2:     cache miss
@: rpt2: generated declarations for '<path>/src/constants.ts'
@: rpt2: generating target 1
@: rpt2: rolling caches
@: rpt2: writing declarations for '<path>/src/index.ts' to '<path>/lib/index.d.ts'
@: rpt2: writing declarations for '<path>/src/index.ts' to '<path>/lib/index.d.ts.map'
@: rpt2: writing declarations for '<path>/src/constants.ts' to '<path>/lib/constants.d.ts'
@: rpt2: writing declarations for '<path>/src/constants.ts' to '<path>/lib/constants.d.ts.map'
@: rpt2: generating target 2
@: rpt2: rolling caches
@: rpt2: writing declarations for '<path>/src/index.ts' to '<path>/lib/index.d.ts'
@: rpt2: writing declarations for '<path>/src/index.ts' to '<path>/lib/index.d.ts.map'
@: rpt2: writing declarations for '<path>/src/constants.ts' to '<path>/lib/constants.d.ts'
@: rpt2: writing declarations for '<path>/src/constants.ts' to '<path>/lib/constants.d.ts.map'
@: created lib/index.js, lib/index.es.js in 1.2s
@ezolenko
Copy link
Owner

Check out this line in the verbose log:

"messageText": "No inputs were found in config file '<path>/tsconfig.json'. Specified 'include' paths were '[\"src/\"]' and 'exclude' paths were '[\"<path>/lib\"]'.",

Not sure why it didn't print it out as an actual error, but that must have something to do with it. Type-only ts files are invisible to rollup, so the plugin relies on include list in tsconfig to get the missing files to extract declarations from.

This is different from plugin's own include/exclude values -- those are only used to filter out files that rollup sees.

@houfio
Copy link
Contributor Author

houfio commented Aug 24, 2018

I see! Adding the followijg to the tsconfig overrides fixes the problem:

        include: [
          join(root, 'src')
        ]

TypeScript seems to use the path relative to the tsconfig and not the cwd.
I'd be great if the plugin actually notified the users about any ts config errors. Can I make a PR for that?

@ezolenko
Copy link
Owner

Sure, PRs are welcome. Check parse-tsconfig.ts for a starting point. Plugin should already print out hard parsing errors, so I suppose this is some kind of warning that goes in a separate channel.

@agilgur5
Copy link
Collaborator

agilgur5 commented May 25, 2022

See #211 for the root cause of this. The original intent of the issue is mostly the same as that one, but then it ended up being a bugfix for not reporting tsconfig errors properly.
Fixing the tsconfig error allowed a workaround from #24 to get this to work, but the root cause of both is #211

@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue labels May 25, 2022
Repository owner locked as resolved and limited conversation to collaborators May 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Something isn't working properly solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: type-only / emit-less imports Related to importing type-only files that will not be emitted
Projects
None yet
3 participants