@hqjs/babel-plugin-transform-json-imports
Advanced tools
Comparing version 0.0.4 to 0.0.5
25
index.js
const path = require('path'); | ||
const RELATIVE_PATTERN = /\s*(\.{1,2})\/([^'"]*)/g; | ||
const notJsonImport = modName => !modName.endsWith('.json'); | ||
const replace = (baseURI, dirname) => | ||
(match, dots, rest) => `${baseURI}${path.join(dirname, dots, rest)}`; | ||
const notRequire = (t, nodePath) => { | ||
@@ -22,3 +17,3 @@ const [requireArg, ...rest] = nodePath.node.arguments; | ||
CallExpression(nodePath, stats) { | ||
const { dirname, fs = require('fs') } = stats.opts; | ||
const { dirname, fs = require('fs'), root = '' } = stats.opts; | ||
if (notRequire(t, nodePath)) return; | ||
@@ -28,5 +23,11 @@ const { node, parent } = nodePath; | ||
const { value: modName } = requireArg; | ||
if (notJsonImport(modName)) return; | ||
const filepath = modName.replace(RELATIVE_PATTERN, replace('', dirname)); | ||
const filepath = modName.startsWith('/') ? | ||
`${root}${modName}` : | ||
modName.startsWith('.') ? | ||
path.join(dirname, modName) : | ||
modName; | ||
const json = JSON.parse(fs.readFileSync(filepath, { encoding: 'utf8' })); | ||
@@ -37,3 +38,3 @@ | ||
ImportDeclaration(nodePath, stats) { | ||
const { dirname, fs = require('fs') } = stats.opts; | ||
const { dirname, fs = require('fs'), root = '' } = stats.opts; | ||
const { node } = nodePath; | ||
@@ -45,3 +46,9 @@ const { value: modName } = nodePath.node.source; | ||
const leftExpression = determineLeftExpression(t, node); | ||
const filepath = modName.replace(RELATIVE_PATTERN, replace('', dirname)); | ||
const filepath = modName.startsWith('/') ? | ||
`${root}${modName}` : | ||
modName.startsWith('.') ? | ||
path.join(dirname, modName) : | ||
modName; | ||
const json = JSON.parse(fs.readFileSync(filepath, { encoding: 'utf8' })); | ||
@@ -48,0 +55,0 @@ |
{ | ||
"name": "@hqjs/babel-plugin-transform-json-imports", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Transform json imports", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,3 +12,3 @@ # https://hqjs.org | ||
{ | ||
"plugins": [["hqjs@babel-plugin-transform-json-imports", { "dirname": "/json/root/directory" }]] | ||
"plugins": [["hqjs@babel-plugin-transform-json-imports", { "dirname": "/json/directory", "root": "/root/directory" }]] | ||
} | ||
@@ -15,0 +15,0 @@ ``` |
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
14224
88