@hqjs/babel-plugin-transform-json-imports
Advanced tools
Comparing version 0.0.3 to 0.0.4
18
index.js
@@ -24,3 +24,4 @@ const path = require('path'); | ||
if (notRequire(t, nodePath)) return; | ||
const [requireArg] = nodePath.node.arguments; | ||
const { node, parent } = nodePath; | ||
const [requireArg] = node.arguments; | ||
const { value: modName } = requireArg; | ||
@@ -32,3 +33,3 @@ if (notJsonImport(modName)) return; | ||
nodePath.replaceWith(t.valueToNode(json)); | ||
nodePath.replaceWith(t.valueToNode(cleanRequiredJSON(t, parent, json))); | ||
}, | ||
@@ -92,1 +93,14 @@ ImportDeclaration(nodePath, stats) { | ||
} | ||
function cleanRequiredJSON(t, node, json) { | ||
if (t.isObjectPattern(node.id)) { | ||
const res = {}; | ||
for (const property of node.id.properties) { | ||
const { name } = property.key; | ||
if (name in json) res[name] = json[name]; | ||
} | ||
return res; | ||
} | ||
return json; | ||
} |
{ | ||
"name": "@hqjs/babel-plugin-transform-json-imports", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Transform json imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,8 @@ # https://hqjs.org | ||
``` | ||
or similar expressions with require | ||
```js | ||
const values = require('./values.json'); | ||
// Or with destructure | ||
const {a, b} = require('./values.json'); | ||
``` | ||
we will obtain | ||
@@ -36,0 +42,0 @@ ```js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14140
83
45