-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Invalid code generation with --module=amd and --resolveJsonModule=true #25517
Comments
Oh come on. :( |
😂 imho the correct way to fix this is not to have a such error message, but make JSON Module resolution and generation work on all module systems. I think I could accept reporting errors in 3.0. But could you please not mark this as |
I am sorry, but complete fix is in the eyes of the beholders. AMD loaders do not automatically handle parsing and loading of JSON into objects, unlike the Node.js |
To be clear, statically building the JSON into an AMD module would not be a good solution either, because the whole intent is to allow the structure of the JSON to be typed checked at design time, but allow the runtime to still be a JSON file. The fact that the compiler copies it on build to the out directory could actually be argued outside of what TypeScript should actually be doing. Bundlers should be choosing to statically build assets or transform them to other formats. |
Defining objects as modules in AMD is perfectly fine and works great, especially with the new The proper fix for this issue is what I did in the diff. Instead, This issue was about when the JSON file is inside our project, not from #!/bin/bash
set -x
rm -rf node_modules package*.json import-json.ts bundle*js
npm init --yes
npm add [email protected] [email protected]
cat > 'import-json.ts' <<EOF
import phoneMetadata from 'libphonenumber-js/metadata.mobile.json';
console.log(phoneMetadata)
EOF
# tsc 2.9.2 works
npx tsc --esModuleInterop --resolveJsonModule --outFile bundle-2.9.2.js --module amd --moduleResolution node import-json.ts
# tsc 3.0.0 does not
npm add typescript@next
npx tsc --esModuleInterop --resolveJsonModule --outFile bundle-next.js --module amd --moduleResolution node import-json.ts |
actually my module setting is |
ESNext represents ECMAScript standards track features. as of now, ES Modules only target code modules (i.e. JS). anything else like json, css, images, etc.. are all not allowed. TS is trailing the ECMAScript specification on this one. |
@mhegazy understood. thanks for the explanation. |
I just want vscode to type check JSON files for me, so sad x_x |
TypeScript Version: Version 3.0.0-dev.20180707
Search Terms: amd json
Code
./run-commonjs.js
./run-amd.js
Expected behavior:
Both scripts output
Actual behavior:
Only the commonjs version works. The AMD version can be fixed by applying this patch:
As you can see, the compiler outputs the json data as-is, without wrapping it with the required module definition code. Note that
--module=system
generates the same plain json-object in the bundle.Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: