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.

Monorepo example, using `@rollup/plugin-alias`

See original GitHub issue

What happens and why it is wrong

Environment

I am trying to setup a monorepo with local packages. I am using pnpm workspaces but could also go with tsconfig’s paths features. Whatever, I don’t get it to work and would love to see an example where somebody gets a monorepo working with this plugin.

My problem is that I get this all the time:

index.ts → index.js...
[!] 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:

note that the imported local package (greeter) has a index.js, index.mjs and index.d.ts file

Versions

  • typescript: 3.9.7
  • rollup: 2.23
  • rollup-plugin-typescript2: 3.1.1

rollup.config.js

import alias from '@rollup/plugin-alias'
import typescript from 'rollup-plugin-typescript2'

module.exports = {
  input: 'index.ts',
  output: {
    file: 'index.js',
    format: 'module',
  },
  plugins: [
    alias({
      entries: [{ find: 'greeter', replacement: '../greeter' }],
    }),
    typescript(),
  ],
}

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "moduleResolution": "node"
  }
}

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^3.9.7"
  },
  "dependencies": {
    "@rollup/plugin-alias": "^3.1.1",
    "greeter": "workspace:^1.0.0",
    "rollup-plugin-typescript2": "^0.27.1",
    "tslib": "^2.0.0"
  }
}

plugin output with verbosity 3

rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "inlineSourceMap": false,
    "outDir": "~/test/app/node_modules/.cache/rollup-plugin-typescript2/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true,
    "module": 5
}
rpt2: parsed tsconfig: {
    "options": {
        "target": 99,
        "moduleResolution": 2,
        "configFilePath": "~/test/app/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "inlineSourceMap": false,
        "outDir": "~/test/app/node_modules/.cache/rollup-plugin-typescript2/placeholder",
        "allowNonTsExtensions": true,
        "module": 5
    },
    "fileNames": [
        "~/test/app/index.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "compilerOptions": {
            "target": "esnext",
            "moduleResolution": "node"
        },
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {
        "~/test/app": 1
    },
    "compileOnSave": false,
    "configFileSpecs": {
        "includeSpecs": [
            "**/*"
        ],
        "validatedIncludeSpecs": [
            "**/*"
        ],
        "wildcardDirectories": {
            "~/test/app": 1
        }
    }
}
rpt2: typescript version: 3.9.7
rpt2: tslib version: 1.11.2
rpt2: rollup version: 2.23.0
rpt2: rollup-plugin-typescript2 version: 0.27.1
rpt2: plugin options:
{
    "verbosity": 3,
    "check": true,
    "clean": false,
    "cacheRoot": "~/test/app/node_modules/.cache/rollup-plugin-typescript2",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "useTsconfigDeclarationDir": false,
    "tsconfigOverride": {},
    "transformers": [],
    "tsconfigDefaults": {},
    "objectHashIgnoreUnknownHack": false,
    "cwd": "~/test/app",
    "typescript": "version 3.9.7"
}
rpt2: rollup config:
{
    "external": [],
    "input": "index.ts",
    "plugins": [
        {
            "name": "alias"
        },
        {
            "name": "rpt2"
        },
        {
            "name": "stdin"
        }
    ],
    "output": [
        {
            "file": "index.js",
            "format": "module",
            "plugins": []
        }
    ]
}
rpt2: tsconfig path: ~/test/app/tsconfig.json
rpt2: included:
[
    "*.ts+(|x)",
    "**/*.ts+(|x)"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts"
]
rpt2: ^[[34mAmbient types:^[[39m
rpt2:     ~/test/node_modules/.pnpm/@types/estree@0.0.39/node_modules/@types/estree/index.d.ts
rpt2: ^[[33mambient types changed, redoing all semantic diagnostics^[[39m
rpt2: ^[[34mtranspiling^[[39m '~/test/app/index.ts'
rpt2:     cache: '~/test/app/node_modules/.cache/rollup-plugin-typescript2/rpt2_4a23a64245ad76cbf88c568d3f033c4b438c240a/code/cache/02a6dc8db60bdf18980df68001d986771ff54b6f'
rpt2: ^[[33m    cache miss^[[39m
rpt2:     cache: '~/test/app/node_modules/.cache/rollup-plugin-typescript2/rpt2_4a23a64245ad76cbf88c568d3f033c4b438c240a/syntacticDiagnostics/cache/02a6dc8db60bdf18980df68001d986771ff54b6f'
rpt2: ^[[33m    cache miss^[[39m
rpt2:     cache: '~/test/app/node_modules/.cache/rollup-plugin-typescript2/rpt2_4a23a64245ad76cbf88c568d3f033c4b438c240a/semanticDiagnostics/cache/02a6dc8db60bdf18980df68001d986771ff54b6f'
rpt2: ^[[33m    cache miss^[[39m
rpt2: ^[[34mresolving^[[39m '../greeter' imported by '~/test/app/index.ts'
rpt2:     to '~/test/greeter/index.ts'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ezolenkocommented, Oct 29, 2020

PR is merged, build is in master. Could somebody with this problem verify if it works for them before I do a release?

1reaction
desmapcommented, May 26, 2022

I think I found the solution:

alias({
  entries: [{ find: 'greeter', replacement: '../greeter/index.mjs' }],
                                                              ^
})

it needs to be ../greeter/index.mjs and not ../greeter nor ../greeter.js (so the "type": "module" didn’t help).

Then it compiles flawlessly! I don’t know why tho. 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

@rollup/plugin-alias - npm
A Rollup plugin for defining aliases when bundling packages. Alias 101. Suppose we have the following import defined in a hypothetical file:.
Read more >
Top 5 @rollup/plugin-alias Code Examples - Snyk
To help you get started, we've selected a few @rollup/plugin-alias examples, based on popular ways it is used in public projects.
Read more >
Directly point to source code when referencing dependencies ...
Scenes In a monorepo project, we may have dozens of lib modules, ... as @rollup/plugin-alias, but its configuration is static For example, ...
Read more >
How to get rollup to include a dependency from another ...
You can run Babel from @rollup/plugin-babel and transpile ts files with Babel directly. Babel will go over files in node_modules .
Read more >
rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code ... The something example from above would then be attached...
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