Skip to content

Unable to build TypeScript program using private scope modules #1363

@cmidgley

Description

@cmidgley

Build environment: Windows (I believe any)
Moddable SDK version: Requires recent version that supports private scope module build system changes
Target device: Any

Description
Private scope module resolution with Moddable requires an artificial path to create a common root (such as implemented by mcpack), which breaks TypeScript compilation. For example, to define the private scope package #example one might use a manifest.json containing a modules section similar to this:

"modules": {
	"root/main": "./src/main",
	"root/#example": "./src/example/index",
	"example/do-something": "./src/example/do-something"
}

The artificial path component root is used to assist module resolution in translating between the file system pathing of JS and the namespace resolution used in Moddable.

mcconfig then carries these artificial roots over to the generated tsconfig.json file:

"compilerOptions": {
	"paths": {
		"root/main": ["/some/path/main"],
		"root/#example": ["/some/path/example/index"],
		"example/do-something": ["/some/path/example/do-something"]
	}
}

This results in a TypeScript compilation error such as:

# tsc tsconfig.json
src/main.ts:1:29 - error TS2792: Cannot find module '#example'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

1 import { doSomething } from '#example';
                              ~~~~~~~~~~

Steps to Reproduce

  1. Clone this repo
  2. Switch to the "typescript" branch
  3. Run mcconfig -m -d

Thoughts

I have not come up with an obvious/clean way to indicate to mcconfig that the root is artificial. While the best solution may be to change module resolution to not need artificial roots (easier to implement/understand for devs), that feels like a high-risk and low-reward endeavor. My solution has been to change the typescript / compiler option in manifest.json to run a small script that front-ends tsc and removes the artificial roots from tsconfig.json. This is working well for me, but it's a hack and not generic enough to be useful for others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions