webpack-typings-for-json
Version:
Webpack loader that generates TypeScript typings for JSON files
2 lines (1 loc) • 4.22 kB
JavaScript
"use strict";var e=require("node:fs"),n=require("camelcase"),t=require("handlebars/runtime");function r(e,t,l){const o={};return Object.entries(e).forEach((([e,a])=>{const c=function(e){return/^[a-z][a-z0-9\-_]+/gi.test(e)?n(e,{locale:"en-US"}):e}(e),u=l?`${l}.${e}`:e;if(Array.isArray(a)){o[c]=[];for(let e=0;e<a.length;e++)o[c][e]=r(a[e],t,`${u}[${e}]`)}else o[c]="object"==typeof a?r(a,t,u):t?a:u})),o}function l(e){const n=[];return Object.values(e).forEach((e=>{if(Array.isArray(e))for(let t=0;t<e.length;t++)n.push(...l(e[t]));else"object"==typeof e?n.push(...l(e)):n.push(e)})),n.filter(((e,t)=>n.indexOf(e)===t))}var o={1:function(e,n,t,r,l){var o,a=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return null!=(o=e.invokePartial(a(r,"entry"),n,{name:"entry",data:l,indent:" ",helpers:t,partials:r,decorators:e.decorators}))?o:""},3:function(e,n,t,r,l){var o,a=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return"export type LocaleKey = (\n"+(null!=(o=a(t,"each").call(null!=n?n:e.nullContext||{},a(n,"exportTypes"),{name:"each",hash:{},fn:e.program(4,l,0),inverse:e.noop,data:l,loc:{start:{line:13,column:0},end:{line:15,column:9}}}))?o:"")+");\n\n"},4:function(e,n,t,r,l){var o,a=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return" '"+(null!=(o=e.lambda(n,n))?o:"")+"'"+(null!=(o=a(t,"unless").call(null!=n?n:e.nullContext||{},a(l,"last"),{name:"unless",hash:{},fn:e.program(5,l,0),inverse:e.noop,data:l,loc:{start:{line:14,column:14},end:{line:14,column:44}}}))?o:"")+"\n"},5:function(e,n,t,r,l){return" |"},7:function(e,n,t,r,l){return"export type LocaleKey = string;\n\n"},compiler:[8,">= 4.3.0"],main:function(e,n,t,r,l){var o,a=null!=n?n:e.nullContext||{},c=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return"// *** This file is automatically generated - Do not edit! ***\n/* tslint:disable */\n/* eslint-disable */\n\ndeclare const locale = {\n"+(null!=(o=c(t,"each").call(a,c(n,"exports"),{name:"each",hash:{},fn:e.program(1,l,0),inverse:e.noop,data:l,loc:{start:{line:6,column:0},end:{line:8,column:9}}}))?o:"")+"} as const;\n\n"+(null!=(o=c(t,"if").call(a,c(n,"exportType"),{name:"if",hash:{},fn:e.program(3,l,0),inverse:e.program(7,l,0),data:l,loc:{start:{line:11,column:0},end:{line:21,column:7}}}))?o:"")+"export default locale;"},usePartial:!0,useData:!0},a={1:function(e,n,t,r,l){var o,a=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return"'"+(null!=(o=e.lambda(e.strict(l,"key",{start:{line:2,column:3},end:{line:2,column:7}}),n))?o:"")+"': {\n"+(null!=(o=a(t,"each").call(null!=n?n:e.nullContext||{},n,{name:"each",hash:{},fn:e.program(2,l,0),inverse:e.noop,data:l,loc:{start:{line:3,column:0},end:{line:5,column:9}}}))?o:"")+"},\n"},2:function(e,n,t,r,l){var o,a=e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]};return null!=(o=e.invokePartial(a(r,"entry"),n,{name:"entry",data:l,indent:" ",helpers:t,partials:r,decorators:e.decorators}))?o:""},4:function(e,n,t,r,l){var o,a=e.lambda;return"'"+(null!=(o=a(e.strict(l,"key",{start:{line:8,column:3},end:{line:8,column:7}}),n))?o:"")+"': '"+(null!=(o=a(n,n))?o:"")+"',\n"},compiler:[8,">= 4.3.0"],main:function(e,n,t,r,l){var o;return null!=(o=(e.lookupProperty||function(e,n){if(Object.prototype.hasOwnProperty.call(e,n))return e[n]})(t,"object").call(null!=n?n:e.nullContext||{},n,{name:"object",hash:{},fn:e.program(1,l,0),inverse:e.program(4,l,0),data:l,loc:{start:{line:1,column:0},end:{line:9,column:11}}}))?o:""},usePartial:!0,useData:!0};const c=t.template(o),u=t.template(a);t.registerPartial("entry",u),t.registerHelper("object",(function(e,n){return"object"==typeof e?n.fn(this):n.inverse(this)})),module.exports=function(n){this.cacheable&&this.cacheable();const t=this.getOptions(),o=JSON.parse(n),a=t&&!0===t.exportValues,u=t&&!0===t.exportType,i=r(o,a),s=l(i),p=this.resourcePath.replace(/\.json$/g,".json.d.ts"),f=c({exports:i,exportType:u,exportTypes:s});return e.writeFileSync(p,f),function(e){return`module.exports = ${JSON.stringify(e,null,3)};`}(i)};