@hqjs/babel-plugin-transform-json-imports
Advanced tools
Comparing version 0.0.2 to 0.0.3
20
index.js
@@ -1,2 +0,1 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
@@ -23,3 +22,3 @@ | ||
CallExpression(nodePath, stats) { | ||
const { dirname } = stats.opts; | ||
const { dirname, fs = require('fs') } = stats.opts; | ||
if (notRequire(t, nodePath)) return; | ||
@@ -36,3 +35,3 @@ const [requireArg] = nodePath.node.arguments; | ||
ImportDeclaration(nodePath, stats) { | ||
const { dirname } = stats.opts; | ||
const { dirname, fs = require('fs') } = stats.opts; | ||
const { node } = nodePath; | ||
@@ -50,3 +49,3 @@ const { value: modName } = nodePath.node.source; | ||
leftExpression, | ||
t.valueToNode(json), | ||
t.valueToNode(cleanJSON(t, node, json)), | ||
), | ||
@@ -81,1 +80,14 @@ ])); | ||
} | ||
function cleanJSON(t, node, json) { | ||
if (isDestructuredImportExpression(t, node)) { | ||
const res = {}; | ||
for (const specifier of node.specifiers) { | ||
const { name } = specifier.imported; | ||
if (name in json) res[name] = json[name]; | ||
} | ||
return res; | ||
} | ||
return json; | ||
} |
{ | ||
"name": "@hqjs/babel-plugin-transform-json-imports", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Transform json imports", | ||
@@ -20,3 +20,3 @@ "main": "index.js", | ||
], | ||
"author": "hqjs <hqjsorg@gmail.com>", | ||
"author": "hqjs <hqjs.org@gmail.com>", | ||
"license": "MIT", | ||
@@ -23,0 +23,0 @@ "bugs": { |
@@ -9,2 +9,10 @@ # https://hqjs.org | ||
# Usage | ||
```json | ||
{ | ||
"plugins": [["hqjs@babel-plugin-transform-json-imports", { "dirname": "/json/root/directory" }]] | ||
} | ||
``` | ||
If you are invoking this plugin from javascript it becomes possible to pass filesystem implementation trough `fs` option, it expects the object with `readFileSync` method defined. | ||
# Transformation | ||
@@ -16,3 +24,4 @@ Transforms `.json` imports into inplace definition e.g. having file values.json | ||
"a": 1, | ||
"b": 2 | ||
"b": 2, | ||
"c": 3 | ||
} | ||
@@ -29,4 +38,4 @@ ``` | ||
```js | ||
const values = {a: 1, b: 2}; | ||
const values = {a: 1, b: 2, c: 3}; | ||
const {a, b} = {a: 1, b: 2}; | ||
``` |
Sorry, the diff of this file is not supported yet
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
13646
71
39
2