Import a JSON file at compile time
See original GitHub issueType of Issue
[ X ] Bug Report
[ X ] Feature Request
Description
Im not really sure if I am looking at a bug or a missing feature.
What I need: I simply want do import a JSON file (containing my translation strings) at build time.
Typescript should be able to import a JSON file as long there is an appropriate module type declared. So this is my importing file translations.ts
:
/// <reference path="./json.d.ts" />
import * as de from './de.json';
import * as en from './en.json';
export default {
de,
en
};
I use the triple slash notation to import a custom json type, like described here. The type looks like in this article.
json.d.ts
declare module "*.json" {
const value: any;
export default value;
}
Both imported json files are existent and valid. Anyway, the moment I build this, my JSON files won’t be resolved resulting in this error message:
Building Angular Package
Building entry point 'demo'
Rendering Stylesheets
Rendering Templates
Compiling TypeScript sources through ngc
Bundling to FESM2015
BUILD ERROR
Could not resolve './de.json' from dist/demo/esm2015/assets/i18n/translation.js
Error: Could not resolve './de.json' from dist/demo/esm2015/assets/i18n/translation.js
at error (/home/[truncated]/module-workbench/node_modules/rollup/dist/rollup.js:199:15)
at /home/[truncated]/module-workbench/node_modules/rollup/dist/rollup.js:20784:25
Could not resolve './de.json' from dist/demo/esm2015/assets/i18n/translation.js
Error: Could not resolve './de.json' from dist/demo/esm2015/assets/i18n/translation.js
at error (/home/[truncated]/module-workbench/node_modules/rollup/dist/rollup.js:199:15)
at /home/[truncated]/module-workbench/node_modules/rollup/dist/rollup.js:20784:25
How To Reproduce
You would have to set up a simple case trying to import a JSON file.
Version Information
➜ module-workbench master $ node_modules/.bin/ng-packagr --version
ng-packagr: 3.0.6
@angular/compiler: 6.1.1
rollup: 0.59.4
tsickle: 0.32.1
typescript: 2.7.2
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How to convert .JSON into struct at compile time - Quora
It would be possible to write a parser that generated static C++ structs from JSON import, but in practice they're little reason to...
Read more >How to generate code for a json file (with its data) at compile ...
There is an online tools that converts JSON to C# called quicktype.io, which you can get packaged as a Visual Studio Code Extension....
Read more >Json Files at Compilation Time - Questions / Help - Elixir Forum
Hello guys, I'm wondering what is the correct way of compiling big json files. There is this json file I'll be needing for...
Read more >Is there a way of running code getting it's output at compile time?
you can use include_str! to load the json during compile time, then use the file as if you read it during runtime. Most...
Read more >How to parse JSON at compile time? - Nim forum
I'm not sure its possible to parse JSON at compile time. I've tried reading the file with staticRead and passing the resulting const...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
At the moment Angular Compiler doesn’t support the TypeScript resolveJsonModules feature.
See: https://github.com/angular/angular/issues/25456
IMHO, this should be blocked until Angular Compiler can support Json imports, and eventually we can implement the JSON Inlining for FESM and UMD formats only via rollup.
As otherwise we need to implement a workaround to support JSON in ESM modules as well. Note that having a lot of json files will increase the library package size as each json will need to be copied 6 times, unless rewired which adds an additional layer of complexity.
Apart from that for FESM and UMD should json be inlined at all?
I think this needs to be discussed a bit more with the Angular guys, mainly to see what are their thoughts about inlining vs not, and come up with a proper specification.
On Mon, 20 Aug 2018 at 22:28, l0rn notifications@github.com wrote:
Oh that is good to know… Although it doesn’t in angular world, ran into this one: https://github.com/angular/angular/issues/25456
Anyone already got a workaround?