Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a note that this requires a bundler that can rewrite the module paths #1

Open
ngbrown opened this issue Jun 18, 2021 · 4 comments

Comments

@ngbrown
Copy link
Contributor

ngbrown commented Jun 18, 2021

The way @types relies on a non-rooted module import conflicts with what Screeps Arena actually expects. It would be useful to point that out somewhere.

This is handled by a paths rule in the rollup config like this:

https://github.com/screepers/screeps-arena-typescript-starter/blob/c1cbaae579dcdb793d0e4269ce296ea343a2bc69/rollup.config.js#L31-L38

Or like this for esbuild:

const screepsArenaImportExternal = {
  name: "screeps-arena-import-external",
  setup(build) {
    build.onResolve({ filter: /^\/?(arena|game)($|\/.*)/ }, (args) => {
      return {
        path: args.path.startsWith("/") ? args.path : `/${args.path}`,
        external: true,
      };
    });
  },
};

require('esbuild').build({
  // ...
  plugins: [screepsArenaImportExternal],
  // ...
}).catch(() => process.exit(1));
@thmsndk
Copy link
Collaborator

thmsndk commented Jun 18, 2021

@ngbrown That is a very valid point, the reason we do that currently is because relative paths where not allowed for declaring modules. At least not as far as I know. If we can solve it in other ways, I'm very open for changing it.

@ngbrown
Copy link
Contributor Author

ngbrown commented Jun 18, 2021

Maybe there's a way using this in the tsconfig.json:

"baseUrl": "types",
"typeRoots": ["types"],

And export pseudo implementations from there instead of module type definitions? Last time I tried to mess with baseUrl or typeRoots the results were not very consistent, so I stay away from it.

@thmsndk
Copy link
Collaborator

thmsndk commented Jun 18, 2021

@ngbrown What I meant was that I was not able to declare the module with a relative path e.g. declare module "/game" {.. and typescript where not able to find the types when doing from "/game"; I assume because that is reserved for relative paths, in your actual project. not sure how to solve it or if it can be solved at all 🤔

@ngbrown
Copy link
Contributor Author

ngbrown commented Jun 19, 2021

@thmsndk If the absolute pathed format could be kept, then types could work for players that don't want to bundle or transpile and just work in standard javascript. I seemingly got a start of it working. There's a complication that TypeScript doesn't handle .mjs, so the files have to be renamed .js. tsc runs and checks the files and Visual Studio Code will provide intellisense.

So I have a tsconfig.json like this:

{
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true,
    "checkJs": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": false,
    "module": "ES2020",
    "moduleResolution": "Node",
    "target": "ES2020",
    "importsNotUsedAsValues": "error",
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "/arena": ["types/arena"],
      "/arena/*": ["types/arena/*"],
      "/game/*": ["types/game/*"]
    }
  },
  "include": ["**/*.js"],
  "exclude": ["node_modules"]
}

With a folder structure starting like this:
image

This types/ folder is at the common directory level of all the created folders (C:\Users\johnsmith\ScreepsArena\)

Each file is similar to the existing files, just without the declare module "...." { portion.

These types could then be installed with a create-screeps-arena type package, which would be used like this:

npm init screeps-arena

But how to update the types? Maybe the path isn't to types/ but to node_modules/typed-screeps-arena/types or something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants