Closed birtles closed 2 years ago
- It all worked fine until I started to use fancy syntax in the
common/func.ts
file
Just to clarify what I mean by this, if I change:
export function func({ a }: { a: string }) {
console.log(`Hi ${a}`);
}
to:
export function func(a: string) {
console.log(`Hi ${a}`);
}
it all works as intended.
FWIW, I think I may have worked around this by adding a package.json
to common
, adding tslib
as a dependency there, and adding a preinstall step to each of the sibling directories to make them run npm install
in common
if it exists (since it won't when run under sam build
).
I guess it is the same reason why my wrapper for rollup throws the same error when I changed tslib
from a peer dependency to dependency. For side note, I am not using this plugin. š
It was the official plugin.
First of all, what a great project! It makes building TypeScript libraries so much simpler! We use rollup and this plugin in our project https://github.com/TelestionTeam/telestion-client and after the initial setup it worked flawlessly.
After a dependabot upgrade (https://github.com/TelestionTeam/telestion-client/pull/344) for TypeScript v4.2 the plugin stopped working with tslib
is not found or some parts are not exported (e. g. __spreadArray
).
It is important to mention that our project is a monorepo so the complexity increases further and there may be other parts that can cause this issue.
To compile the packages we need to call rollup in every package directory.
I've dug around a bit in the issues and dependencies and found something interesting:
After the installation, npm deduplicates the tslib
package only in an earlier version (for us v1.14.1
) and installs the newer version (here v2.1.0
) in the node_modules
folder for every package that needs it.
You can verify it with the command npm ls tslib
.
Ours looks like this:
Also I manually checked that and the installed versions are:
path | version |
---|---|
project_path/node_modules/tslib |
v1.14.0 |
project_path/node_modules/rollup-plugin-typescript2/node_modules/tslib |
v2.1.0 |
After explicity installing tslib
via npm i -D tslib
the deduplication for the older version is gone and the "working" version is located directly in the node_modules
of the project:
And the project transpiles again successfully.
I think, there is a resolution error either in the rollup-plugin or in rollup itself which leads to resolving to the package directly in the projects node_modules
and not the installed in the node_modules
of the installed package.
Unfortunately I am not familiar with the code base of both projects so it's only a guess.
Any ideas where this can happen?
Just for reference, the configuration files and the logs of our run:
Installed/used tools:
tool | version |
---|---|
node --version |
v14.15.4 |
npm --version |
v6.14.11 |
rollup --version |
v2.39.0 |
- It all worked fine until I started to use fancy syntax in the
common/func.ts
file
This makes sense as newer syntax is not implemented in older versions of ECMAScript and so a "helper" is needed to basically "polyfill" the syntax. TS has its helpers in the tslib
library.
Note that Babel has equivalent functionality for older versions with @babel/runtime
, which is a similar set of helpers.
Basically, if the syntax you're using isn't supported by your target
version, a helper must be imported from tslib
.
git clone https://github.com/birtles/rollup-ts-mono
- Tweaking
paths
andbaseUrl
as per this comment can seem to help, but appears to break other things.- typescript: 3.9.5
I checked out the repo and was able to reproduce this. I read the upstream issue linked and this seems to have been solved within TS itself later in that same issue: https://github.com/microsoft/TypeScript/issues/37991#issuecomment-794361347 (PR: https://github.com/microsoft/TypeScript/pull/43166).
Upgrading to a newer version of TS (at least v4.3? per the milestones mentioned there) makes this issue go away. For instance, I upgraded to TS 4.6.5
in your project and there was no error anymore.
It's written there that the issue is due to a "weak check" for the syntax. Your target
, es2018
, doesn't actually need a helper -- you'll see after running yarn build
that the resulting bundled index.js
doesn't actually have any imports.
FWIW, I think I may have worked around this by adding a
package.json
tocommon
, addingtslib
as a dependency there
This also makes sense as a workaround, as then TS would be able to find tslib
in common
.
Otherwise, you don't have a root package.json
with tslib
in the root's node_modules
, nor one in common
, so Node's resolution algorithm would be unable to find it. Node's resolution algorithm doesn't check sibling directories, it checks parent directories.
After the installation, npm deduplicates the
tslib
package only in an earlier version (for usv1.14.1
) and installs the newer version (herev2.1.0
) in thenode_modules
folder for every package that needs it. You can verify it with the commandnpm ls tslib
.After explicity installing
tslib
vianpm i -D tslib
the deduplication for the older version is gone and the "working" version is located directly in thenode_modules
of the project:
That doesn't sound like the same issue as OP, and given that explicit install of tslib
worked, that seems to duplicate #270 / the later portions of #12 instead.
tslib
explicitly to the installation section of the README
.tslib
in this plugin's dependencies
, so that should help with any dependency resolution issues as well.I think, there is a resolution error either in the rollup-plugin or in rollup itself which leads to resolving to the package directly in the projects
node_modules
and not the installed in thenode_modules
of the installed package. Unfortunately I am not familiar with the code base of both projects so it's only a guess.
This is a resolution issue, but I'm not sure it's in either project, but rather in how one's dependency tree has been setup (which #270 / later comments in #12 illustrate).
This project is fairly simple and there is tiny 20 LoC file called tslib.ts
that handles this. Can see on this line that it merely calls Node's require
and require.resolve
-- i.e. it just uses Node's resolution algorithm, and not anything custom.
This is within Rollup's resolution though, whereas the error is from the TS Compiler, which uses a resolution algorithm depending on the tsconfig
property moduleResolution
, and which rpt2 forces to node
per #12 / #14 as the legacy classic
option is not compatible. So, in that case, TS is similarly using Node's resolution algorithm, and not anything custom.
What happens and why it is wrong
This is similar to issue #12, #214, and #216 but none of the solutions mentioned there worked for me and I have clear steps to reproduce so I hope this bug is helpful.
Steps to reproduce:
git clone https://github.com/birtles/rollup-ts-mono
cd rollup-ts-mono/a
yarn install
yarn build
Expected results: it builds successfully.
Actual results:
Note that:
npx tsc
worksyarn test
works butnpx rollup -c
produces the above error)node-resolve
etc. since I don't want the external dependencies to be bundled into the result. (This is for building an AWS lambda function where thenode_modules
will be uploaded along with the compiled file and keeping the compiled file smaller makes debugging easier.)common/func.ts
filetsconfig.json
and extending from it doesn't seem to make a differencepaths
andbaseUrl
as per this comment can seem to help, but appears to break other things.Environment
Node v12.14.0 running on Ubuntu 18.04 under WSL 1.
Versions
rollup.config.js
tsconfig.json
package.json
plugin output with verbosity 3
log:
``` yarn run v1.22.4 $ rollup -c ./index.ts ā index.js... rpt2: built-in options overrides: { "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "inlineSourceMap": false, "outDir": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/placeholder", "moduleResolution": 2, "allowNonTsExtensions": true } rpt2: parsed tsconfig: { "options": { "alwaysStrict": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "module": 99, "moduleResolution": 2, "noImplicitAny": true, "noImplicitReturns": true, "noUnusedLocals": true, "pretty": true, "strictNullChecks": true, "target": 5, "configFilePath": "/c/Users/Brian/rollup-ts-mono/a/tsconfig.json", "noEmitHelpers": false, "importHelpers": true, "noResolve": false, "noEmit": false, "inlineSourceMap": false, "outDir": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/placeholder", "allowNonTsExtensions": true }, "fileNames": [ "/c/Users/Brian/rollup-ts-mono/a/index.ts", "/c/Users/Brian/rollup-ts-mono/common/func.ts" ], "typeAcquisition": { "enable": false, "include": [], "exclude": [] }, "raw": { "compilerOptions": { "alwaysStrict": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "module": "ESNext", "moduleResolution": "node", "noImplicitAny": true, "noImplicitReturns": true, "noUnusedLocals": true, "pretty": true, "strictNullChecks": true, "target": "es2018" }, "include": [ "./*.ts", "../common/*.ts" ], "exclude": [ "node_modules" ], "compileOnSave": false }, "errors": [], "wildcardDirectories": { "/c/Users/Brian/rollup-ts-mono/a": 0, "/c/Users/Brian/rollup-ts-mono/common": 0 }, "compileOnSave": false, "configFileSpecs": { "includeSpecs": [ "./*.ts", "../common/*.ts" ], "excludeSpecs": [ "node_modules" ], "validatedIncludeSpecs": [ "./*.ts", "../common/*.ts" ], "validatedExcludeSpecs": [ "node_modules" ], "wildcardDirectories": { "/c/Users/Brian/rollup-ts-mono/a": 0, "/c/Users/Brian/rollup-ts-mono/common": 0 } } } rpt2: typescript version: 3.9.5 rpt2: tslib version: 1.11.2 rpt2: rollup version: 2.15.0 rpt2: rollup-plugin-typescript2 version: 0.27.1 rpt2: plugin options: { "include": [ "*.ts", "../common/*.ts" ], "verbosity": 3, "check": true, "clean": false, "cacheRoot": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2", "exclude": [ "*.d.ts", "**/*.d.ts" ], "abortOnError": true, "rollupCommonJSResolveHack": false, "useTsconfigDeclarationDir": false, "tsconfigOverride": {}, "transformers": [], "tsconfigDefaults": {}, "objectHashIgnoreUnknownHack": false, "cwd": "/c/Users/Brian/rollup-ts-mono/a", "typescript": "version 3.9.5" } rpt2: rollup config: { "external": [], "input": "./index.ts", "plugins": [ { "name": "rpt2" }, { "name": "stdin" } ], "output": [ { "file": "index.js", "format": "cjs", "plugins": [] } ] } rpt2: tsconfig path: /c/Users/Brian/rollup-ts-mono/a/tsconfig.json rpt2: included: [ "*.ts", "../common/*.ts" ] rpt2: excluded: [ "*.d.ts", "**/*.d.ts" ] rpt2: Ambient types: rpt2: /c/Users/Brian/rollup-ts-mono/a/node_modules/@types/estree/index.d.ts rpt2: ambient types changed, redoing all semantic diagnostics rpt2: transpiling '/c/Users/Brian/rollup-ts-mono/a/index.ts' rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/code/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63' rpt2: cache miss rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/syntacticDiagnostics/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63' rpt2: cache miss rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/semanticDiagnostics/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63' rpt2: cache miss rpt2: dependency '/c/Users/Brian/rollup-ts-mono/common/func.ts' rpt2: imported by '/c/Users/Brian/rollup-ts-mono/a/index.ts' rpt2: resolving '../common/func' imported by '/c/Users/Brian/rollup-ts-mono/a/index.ts' rpt2: to '/c/Users/Brian/rollup-ts-mono/common/func.ts' rpt2: transpiling '/c/Users/Brian/rollup-ts-mono/common/func.ts' rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/code/cache/154c4f09b6a8aebbd439964a86673337ca5a9381' rpt2: cache miss rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/syntacticDiagnostics/cache/154c4f09b6a8aebbd439964a86673337ca5a9381' rpt2: cache miss rpt2: cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/semanticDiagnostics/cache/154c4f09b6a8aebbd439964a86673337ca5a9381' rpt2: cache miss [!] (plugin rpt2) Error: ../common/func.ts:1:24 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found. 1 export function func({ a }: { a: string }) { ~ ../common/func.ts Error: ../common/func.ts:1:24 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found. 1 export function func({ a }: { a: string }) { ~ at error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:213:30) at throwPluginError (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17117:12) at Object.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17911:24) at Object.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17290:38) at RollupContext.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:17195:30) at /c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24992:19 at arrayEach (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:535:11) at forEach (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:9361:14) at printDiagnostics (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24968:5) at Object.transform (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29071:17) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. ```