babel-plugin-reshow-import-extension
Advanced tools
Comparing version
{ | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"name": "babel-plugin-reshow-import-extension", | ||
@@ -15,3 +15,5 @@ "repository": { | ||
"dependencies": { | ||
"reshow-constant": "*" | ||
"@babel/core": "*", | ||
"@babel/template": "*", | ||
"reshow-runtime": "*" | ||
}, | ||
@@ -22,2 +24,6 @@ "devDependencies": { | ||
"main": "./src/index.js", | ||
"exports": { | ||
".": "./src/index.js", | ||
"./resolveExt": "./src/resolveExt.js" | ||
}, | ||
"scripts": { | ||
@@ -24,0 +30,0 @@ "mochaFor": "mocha -r jsdom-global/register", |
@@ -1,47 +0,12 @@ | ||
/* | ||
Copyright 2021 silane | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
const { parseSync } = require("@babel/core"); | ||
const { default: template } = require("@babel/template"); | ||
const resolveExt = require("reshow-runtime/helpers/resolveExt"); | ||
const resolveExt = (filepath, extMap) => { | ||
if (0 !== filepath.indexOf(".")) { | ||
return filepath; // not local import | ||
} | ||
const buildImport = template( | ||
`import resolveExt from 'reshow-runtime/helpers/resolveExt';` | ||
); | ||
const resolveExtLib = (filepath, extMapp) => resolveExt(filepath, extMapp); | ||
const baseName = filepath.split("/").splice(-1)[0]; | ||
const extDotPos = baseName.lastIndexOf("."); | ||
const extNone = extMap[""] && delete extMap[""]; | ||
if (-1 === extDotPos) { | ||
// No extension | ||
return extNone ? filepath + extNone : filepath; | ||
} | ||
const astResolveExtension = () => parseSync(`(${resolveExtLib.toString()})`); | ||
const extPos = extDotPos + 1; | ||
const extKeys = Object.keys(extMap); | ||
let i = extKeys.length; | ||
while (i--) { | ||
const origExt = extKeys[i]; | ||
if (extPos === baseName.lastIndexOf(origExt)) { | ||
filepath = filepath.slice(0, -origExt.length) + extMap[origExt]; | ||
break; | ||
} | ||
} | ||
return filepath; | ||
}; | ||
const astTransformExtension = () => parseSync(`(${resolveExt.toString()})`); | ||
const getOption = (state, key) => { | ||
@@ -61,11 +26,19 @@ const opts = state.opts || {}; | ||
} | ||
source.value = resolveExt(source.value, extMap); | ||
source.value = resolveExt(source.value, { ...extMap }); | ||
}; | ||
module.exports = function ({ types: t }) { | ||
let imported = false; | ||
let root; | ||
const resolver = astResolveExtension().program.body[0].expression; | ||
return { | ||
visitor: { | ||
Program(path) { | ||
root = path; | ||
}, | ||
ImportDeclaration(path, state) { | ||
resetNodeSource(path, state); | ||
}, | ||
// For re-exporting | ||
@@ -75,4 +48,9 @@ "ExportNamedDeclaration|ExportAllDeclaration"(path, state) { | ||
}, | ||
// For dynamic import | ||
CallExpression(path, state) { | ||
if (!imported) { | ||
imported = true; | ||
root.unshiftContainer("body", buildImport()); | ||
} | ||
const extMap = getOption(state, "extMapping"); | ||
@@ -94,6 +72,3 @@ if (!extMap) { | ||
argument.replaceWith( | ||
t.callExpression(astTransformExtension().program.body[0].expression, [ | ||
argument.node, | ||
astExtMapping, | ||
]) | ||
t.callExpression(resolver, [argument.node, astExtMapping]) | ||
); | ||
@@ -100,0 +75,0 @@ }, |
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 3 instances in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
-100%3324
-16.04%3
200%65
-25.29%4
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed