question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS sources in `node_modules` are not compiled

See original GitHub issue

What happens and why it is wrong

I import a ts file of IDB lib. And some ts code pass through the compiler. Force including IDB in the tsconfig is not work.

Versions

  • typescript: 3.7.2
  • rollup: 1.27.8
  • rollup-plugin-typescript2: 0.24.3

rollup.config.js

import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import sourceMaps from 'rollup-plugin-sourcemaps'
import typescript from 'rollup-plugin-typescript2'
import json from 'rollup-plugin-json'
import { terser } from 'rollup-plugin-terser'
import analyze from 'rollup-plugin-analyzer'
import peerDepsExternal from 'rollup-plugin-peer-deps-external'

const libraryName = 'ScrmChatSocket'

function entry(input, output) {
  return {
    input,
    output,
    // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
    external: ['window', 'document'],
    watch: {
      include: 'src/**'
    },
    plugins: [
      // Allow json resolution
      json(),
      peerDepsExternal(),
      // Compile TypeScript files
      typescript({
        verbosity: 3,
        tsconfigDefaults: {
          extendedDiagnostics: process.env.NODE_ENV === 'production'
        },
        useTsconfigDeclarationDir: true,
        objectHashIgnoreUnknownHack: true,
        clean: process.env.NODE_ENV === 'production',
        include: ['*.ts+(|x)', '**/*.ts+(|x)', '*.js+(|x)', '**/*.js+(|x)']
      }),
      // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
      commonjs(),
      // Allow node_modules resolution, so you can use 'external' to control
      // which external modules to include in the bundle
      // https://github.com/rollup/rollup-plugin-node-resolve#usage
      resolve({
        browser: true
      }),

      // Resolve source maps to the original source
      sourceMaps()
    ].concat(process.env.NODE_ENV === 'production'
      ? [
        // Minify
        terser(),
        analyze({
          summaryOnly: true
        })
      ]
      : [])
  }
}

export default [
  entry(['src/index.ts'], [
    {
      dir: 'lib',
      name: libraryName,
      format: 'cjs',
      chunkFileNames: 'bundle/chunk.[format].[hash].js',
      entryFileNames: '[name].js',
      sourcemap: process.env.NODE_ENV === 'production'
    },
    {
      dir: 'lib',
      format: 'esm',
      chunkFileNames: 'bundle/chunk.[format].[hash].js',
      entryFileNames: '[name].[format].js',
      sourcemap: process.env.NODE_ENV === 'production'
    }
  ])
]


tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "module": "es2015",
    "lib": [
      "es5",
      "es2015",
      "es2016",
      "es2017",
      "dom"
    ],
    "strict": true,
    "allowJs": true,
    "sourceMap": true,
    "declaration": true,
    "declarationDir": "types",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "keyofStringsOnly": true,
    "types": [
      "@types/node"
    ],
    "typeRoots": [
      "src/interface"
    ]
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

plugin output with verbosity 3

log
src/index.ts → lib, lib...
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "inlineSourceMap": false,
    "outDir": "E:\\projects\\Git\\LC\\xcrm-frontier-app\\packages\\_ws-plugin\\node_modules\\.cache\\rollup-plugin-typescript2/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true
}
rpt2: parsed tsconfig: {
    "options": {
        "moduleResolution": 2,
        "target": 1,
        "module": 5,
        "lib": [
            "lib.es5.d.ts",
            "lib.es2015.d.ts",
            "lib.es2016.d.ts",
            "lib.es2017.d.ts",
            "lib.dom.d.ts"
        ],
        "strict": true,
        "allowJs": true,
        "sourceMap": true,
        "declaration": true,
        "declarationDir": "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/types",
        "allowSyntheticDefaultImports": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "resolveJsonModule": true,
        "keyofStringsOnly": true,
        "types": [
            "@types/node"
        ],
        "typeRoots": [
            "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface"
        ],
        "configFilePath": "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "inlineSourceMap": false,
        "outDir": "E:\\projects\\Git\\LC\\xcrm-frontier-app\\packages\\_ws-plugin\\node_modules\\.cache\\rollup-plugin-typescript2/placeholder",
        "allowNonTsExtensions": true
    },
    "fileNames": [
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/connection.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/core.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/index.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/utils.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/client.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/connection.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/db.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/events.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/index.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/socket.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/interface/storage.interface.ts",
        "E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/types/global.shim.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "extendedDiagnostics": true,
        "compilerOptions": {
            "moduleResolution": "node",
            "target": "es5",
            "module": "es2015",
            "lib": [
                "es5",
                "es2015",
                "es2016",
                "es2017",
                "dom"
            ],
            "strict": true,
            "allowJs": true,
            "sourceMap": true,
            "declaration": true,
            "declarationDir": "types",
            "allowSyntheticDefaultImports": true,
            "experimentalDecorators": true,
            "emitDecoratorMetadata": true,
            "resolveJsonModule": true,
            "keyofStringsOnly": true,
            "types": [
                "@types/node"
            ],
            "typeRoots": [
                "src/interface"
            ]
        },
        "include": [
            "./src/**/*"
        ],
        "exclude": [
            "node_modules"
        ],
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {
        "e:/projects/git/lc/xcrm-frontier-app/packages/_ws-plugin/src": 1
    },
    "compileOnSave": false,
    "configFileSpecs": {
        "includeSpecs": [
            "./src/**/*"
        ],
        "excludeSpecs": [
            "node_modules"
        ],
        "validatedIncludeSpecs": [
            "./src/**/*"
        ],
        "validatedExcludeSpecs": [
            "node_modules"
        ],
        "wildcardDirectories": {
            "e:/projects/git/lc/xcrm-frontier-app/packages/_ws-plugin/src": 1
        }
    }
}
rpt2: typescript version: 3.7.2
rpt2: tslib version: 1.10.0
rpt2: rollup version: 1.27.8
rpt2: rollup-plugin-typescript2 version: 0.24.3
rpt2: plugin options:
{
    "verbosity": 3,
    "tsconfigDefaults": {
        "extendedDiagnostics": true
    },
    "useTsconfigDeclarationDir": true,
    "objectHashIgnoreUnknownHack": true,
    "clean": true,
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)",
        "*.js+(|x)",
        "**/*.js+(|x)"
    ],
    "check": true,
    "cacheRoot": "E:\\projects\\Git\\LC\\xcrm-frontier-app\\packages\\_ws-plugin\\node_modules\\.cache\\rollup-plugin-typescript2",
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "tsconfigOverride": {},
    "transformers": [],
    "typescript": "version 3.7.2"
}
rpt2: rollup config:
{
    "chunkGroupingSize": 5000,
    "experimentalCacheExpiry": 10,
    "inlineDynamicImports": false,
    "input": [
        "src/index.ts"
    ],
    "perf": false,
    "plugins": [
        {
            "name": "json"
        },
        {
            "name": "peer-deps-external"
        },
        {
            "name": "rpt2"
        },
        {
            "name": "commonjs"
        },
        {
            "name": "node-resolve"
        },
        {
            "name": "sourcemaps"
        },
        {
            "name": "terser"
        },
        {
            "name": "rollup-plugin-analyzer"
        }
    ],
    "strictDeprecations": false,
    "watch": {
        "include": "src/**"
    }
}
rpt2: tsconfig path: E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/tsconfig.json
rpt2: included:
[
    "*.ts+(|x)",
    "**/*.ts+(|x)",
    "*.js+(|x)",
    "**/*.js+(|x)"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts"
]
rpt2: transpiling 'E:\projects\Git\LC\xcrm-frontier-app\packages\_ws-plugin\src\index.ts'
rpt2: generated declarations for 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/index.ts'
rpt2: dependency 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts'
rpt2:     imported by 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/index.ts'
rpt2: resolving './db' imported by 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/index.ts'
rpt2:     to 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts'
rpt2: transpiling 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts'
rpt2: generated declarations for 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts'
rpt2: resolving 'idb/with-async-ittr.js' imported by 'E:/projects/Git/LC/xcrm-frontier-app/packages/_ws-plugin/src/db.ts'
rpt2:     to 'E:/projects/Git/LC/xcrm-frontier-app/node_modules/idb/with-async-ittr.ts'
rpt2: resolving './lib/index' imported by 'E:/projects/Git/LC/xcrm-frontier-app/node_modules/idb/with-async-ittr.ts'
rpt2:     to 'E:/projects/Git/LC/xcrm-frontier-app/node_modules/idb/lib/index.ts'
rpt2: resolving './lib/async-iterators' imported by 'E:/projects/Git/LC/xcrm-frontier-app/node_modules/idb/with-async-ittr.ts'
rpt2:     to 'E:/projects/Git/LC/xcrm-frontier-app/node_modules/idb/lib/async-iterators.ts'
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
..\..\node_modules\idb\lib\async-iterators.ts (6:15)
4:
5: const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
6: const methodMap: { [s: string]: Func } = {};
                  ^
7: const advanceResults = new WeakMap<IDBPCursor, Promise<IDBPCursor | null>>();
8: const ittrProxiedCursorToOriginalProxy = new WeakMap<IDBPCursor, IDBPCursor>();
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (E:\projects\Git\LC\xcrm-frontier-app\node_modules\rollup\dist\rollup.js:5363:30)
    at Module.error (E:\projects\Git\LC\xcrm-frontier-app\node_modules\rollup\dist\rollup.js:9701:9)
    at tryParse (E:\projects\Git\LC\xcrm-frontier-app\node_modules\rollup\dist\rollup.js:9610:16)
    at Module.setSource (E:\projects\Git\LC\xcrm-frontier-app\node_modules\rollup\dist\rollup.js:9926:33)
    at Promise.resolve.catch.then.then.then (E:\projects\Git\LC\xcrm-frontier-app\node_modules\rollup\dist\rollup.js:12196:20)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
desmapcommented, Aug 7, 2020

also when using @rollup/plugin-alias, hence redirecting to that local package via ../greeter and not via node_modules (via pnpm workspace feature or I tried also tsconfig paths redirecting) I get the same error. Even if I set the alias to ../greeter/index.js, the same.

1reaction
ezolenkocommented, Dec 12, 2019

vue has its own rollup plugin you need to use before this one.

My question was, why do you import ts files instead of js files out of node_modules? Is that how idb package supposed to be used?

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript compiling types under node_modules even after ...
While trying to compile my typescript source code, I see that the compiler is also trying to compile the types under my node_modules...
Read more >
Common TypeScript module problems and how to solve them
Problem 1: Irregular location of dependencies ; node_modules in the ; src directory even though ; node_modules is located outside the ; src ......
Read more >
Debugging in node (typescript compiled), sourcemaps work ...
Debugging in node (typescript compiled), sourcemaps work for the main project but not any files imported from node_modules - even though they are...
Read more >
Compiling and bundling TypeScript libraries with Webpack
TypeScript comes with the tsc (TS compiler) that takes care of that: it compiles your code to ES5 and creates the *.d.ts files...
Read more >
Can't compile the TS files on vs2017: node_modules not ...
Change the Build Action of tsconfig.json files in your lib_node_modules folder from "Compile" to "None", (screenshot below) or; Exclude the entire " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found