ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.
MIT License
822 stars 71 forks source link

TS sources in `node_modules` are not compiled #199

Closed Fnxxxxo closed 2 years ago

Fnxxxxo commented 4 years ago

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

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>(); 8: const ittrProxiedCursorToOriginalProxy = new WeakMap(); 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. ```
ezolenko commented 4 years ago

You have

  "exclude": [
    "node_modules"
  ]

in your tsconfig, that's might be why.

Fnxxxxo commented 4 years ago

@ezolenko But without it, the IDE keep warning me the TS errors of all libs.

ezolenko commented 4 years ago

How did you try force including that lib in tsconfig?

Also, why do you import ts files from there? Normally packages are transpiled into js and have d.ts typings.

Fnxxxxo commented 4 years ago

@ezolenko I remove both include and exclude. The problem remains. I dont import the package directly. Just import from node modules. Not only modules. When import vue files within the same project. The problem exist.

(!) Error when using sourcemap for reporting an error: Can't resolve original location of error.
src\components\chat-input.vue: (19:53)
[!] Error: Bad character escape sequence (Note that you need plugins to import files that are not JavaScript)
src\components\chat-messenger.vue (19:53)
17:    * Created by Jaron Long on 2019/11/16
18:    */
19:   import Vue, { PropOptions } from 'vue'
                                            ^
20:   import Item from '@vuex-orm/core/lib/data/Item'
21:   import LcChatFloor from '../components/chat-floor.vue'
Error: Bad character escape sequence (Note that you need plugins to import files that are not JavaScript)
ezolenko commented 4 years ago

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?

Fnxxxxo commented 4 years ago

@ezolenko Sorry for the late. You means the following lines?

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'

It's required by the lib since the async iterator is bundled in different chunk. See: https://github.com/jakearchibald/idb#async-iterators https://github.com/jakearchibald/idb/issues/113 https://github.com/jakearchibald/idb/pull/120 I follow the suggestion to import the js file. But it is resolved to a ts file. I dont know why.

desmap commented 4 years ago

I have the same issue, I import a local package which is compiled to js (also tried mjs) but the plugin tries to import the ts version in that dir:

index.ts → index.mjs...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../greeter/index.ts (2:6)
1: export default class {
2:   name: string
         ^
3:
desmap commented 4 years ago

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.

agilgur5 commented 2 years ago

Also, why do you import ts files from there? Normally packages are transpiled into js and have d.ts typings.

I follow the suggestion to import the js file. But it is resolved to a ts file. I dont know why.

Looking at OP's rollup.config.js, I think the issue is with their plugin order -- plugin-node-resolve comes after rpt2 in their config, instead of before. The compatibility docs explicitly say to put it before. So it seems like TS was trying to resolve a source TS file in node_modules, while normally one would import compiled JS files instead. Putting plugin-node-resolve before rpt2 should make that resolve to a JS file properly.

That's tsc behavior too, so that's not a bug in this plugin, but just Rollup misconfiguration causing a lot of head-scratching issues.

@ezolenko But without it, the IDE keep warning me the TS errors of all libs.

Indeed, it seems like it was unintentional of OP to import source TS files from node_modules, despite what the title of their issue says. Either that, or OP needed to use two tsconfigs, one for their IDE and one for building with rpt2.

@ezolenko I remove both include and exclude. The problem remains.

Also worth noting is that node_modules is part of tsconfig's default exclude, so if you remove exclude entirely from your tsconfig (as opposed to making it an empty array), you will still be excluding node_modules due to the default.