Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include JSON file to build/output directory without import

Maybe the title is a bit strange, but I can't seem to find anything about on google.

Question: I have a folder that only contains .ts files and .json files.. Typescript compiles the .ts files and puts it into a separate directory (not as a bundle, just the directory structure 'as-is').

Src /

   Workers /

       [ModuleA.ts, ModuleA.json],

       [ModuleB.ts, ModuleB.json],

       [MobuleC.ts, ModuleC.json]

Most of the time I can just require('*.json') and the JSON file will be also placed in to build directory.

But now I have a situation, where importing the JSON will make no sense, because the JSON file gets updated every few seconds and I read the file with fs.readFile('*.json'), so I also don't want it floating around in the v8 cache (through require)

So how do I 'include' a JSON/None-Typescript file into the build, that is not explicitly being importing by either require or import?


For now I just used gulp to copy every .json file in the src folder over to the the respective dist/** folder.

But still find it strange typescript doesn't have something included for it..

like image 528
DutchKevv Avatar asked Nov 28 '16 12:11

DutchKevv


People also ask

How do I get package JSON files in TypeScript?

Typescript should be able to do it as follows: import * as pack from "../package. json" // access name and version like this: console. log(pack.name);


1 Answers

Maybe you should checkout --resolveJsonModule, it's a newer feature of typescript.

like image 106
cmaynard Avatar answered Nov 08 '22 14:11

cmaynard