Rules_nodejs: resolveJsonModule doesn't work

Created on 7 Sep 2019  路  13Comments  路  Source: bazelbuild/rules_nodejs

馃悶 bug report

Affected Rule

ts_library

Is this a regression?

No.

Description

resolveJsonModule is not supported.

ts_library supports only .ts and .tsx sources.

https://www.typescriptlang.org/docs/handbook/compiler-options.html

Can Close? bug typescript

Most helpful comment

Shouldn't .js be allowed as well?

All 13 comments

Matter of fact it does but it requires some configuration in tsconfig.json file.

Like so

{
  ...
  "bazelOptions": {
    "devmodeTargetOverride": "es2018",
    "googmodule": true
  }
}

Keep in mind this is just a workaround. worked well in my case.

I'm surprised that workaround works because I thought ts_library restricts inputs
https://github.com/bazelbuild/rules_nodejs/blob/master/packages/typescript/src/internal/build_defs.bzl#L279

Yeah, it doesn't work (because of the reason both @alexeagle and I mentioned).

I think it hsould be reasonable to allow .json imports of tsc allows it

Shouldn't .js be allowed as well?

Wouldn't this be blocked anyway due to the use of the UMD module type? Typescript doesn't allow resolveJsonModule with UMD. https://github.com/microsoft/TypeScript/pull/26825/files#diff-942ae3cd2a8bbd85ed86a60cd7c43307R7015 (I can't imagine why .. we are talking about static json files here, it shouldn't be this hard right).

error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs', 'amd', 'es2015' or 'esNext'.

Our tsconfig.json has "module": "commonjs". I didn't understand the error the other day (we are new and trying to pilot bazel). I hastily removed resolveJsonModule and forgot about it.

Then, I set about patching this in to your rules (I couldn't add .json into src). Eventually, I think I got that right, the symlinks are finally there and valid. But still the json files weren't resolved during compilation, despite now being in place.

[I could publish this patch if interested, I believe it's close though I didn't update tests. It was at least useful for me to gain more of an understanding of how bazel works .. inputs and outputs].

Now I see I forgot about the setting in the tsconfig. I put that back, and now despite my efforts to patch I am still blocked, due to the above with UMD. I suppose I will try creating a json.d.ts file as described here https://stackoverflow.com/a/40473566 and try to move on.

I was able to get around it by creating a typings.d.ts file for as described in the link above. It's working at least. I had to update some of our imports.

-import * as data from 'data/obj.json';
+import * as data from '@lib/this-project-name/data/obj.json';

-import * as fixture from 'tests/fixtures/test-data.json';
+import * as fixture from '@test/this-project-name/tests/fixtures/test-data.json';

It's possible I may have had to do that anyway and it's unrelated to resolveJsonModule. Those paths were a bit un-anchored before and the @module thing is nice.

I now see that the 'devmode_module' option is there, I didn't explore setting this to non umd to see if resolveJsonModule + my patch actually worked.

But, for sure it would be nice if *.json and *.js were allowed in as pass through source files to ts_library. It would simplify some of our globs and would allow us to delete a few macros that the JS projects need; they could then just use the same ones for the TS projects.

This issue has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!

I believe this is fixed

This is not fixed, as I'm running into it now on 2.0.1 (which should be after #1995, which only fixed this for ts_project I believe)

Is fixing this for ts_library a lost cause?

Yeah sorry still true for ts_library.
Can be fixed locally on the 3.x branch which no longer syncs with google3

I took a shot at it for ts_library, without necessarily needing to touch the code coming out of rules_typescript

Let me know what y'all think, we'll probably use a local branch of this for the time-being.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Toxicable picture Toxicable  路  6Comments

Globegitter picture Globegitter  路  5Comments

UlysseM picture UlysseM  路  5Comments

alexeagle picture alexeagle  路  4Comments

hperl picture hperl  路  5Comments