babel-plugin-reshow-import-extension
Advanced tools
Comparing version 0.0.0 to 0.0.1
{ | ||
"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 @@ }, |
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
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
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
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
3324
3
65
4
+ Added@babel/core@*
+ Added@babel/template@*
+ Addedreshow-runtime@*
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.24.7(transitive)
+ Added@babel/compat-data@7.25.4(transitive)
+ Added@babel/core@7.25.2(transitive)
+ Added@babel/generator@7.25.6(transitive)
+ Added@babel/helper-compilation-targets@7.25.2(transitive)
+ Added@babel/helper-module-imports@7.24.7(transitive)
+ Added@babel/helper-module-transforms@7.25.2(transitive)
+ Added@babel/helper-simple-access@7.24.7(transitive)
+ Added@babel/helper-string-parser@7.24.8(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/helper-validator-option@7.24.8(transitive)
+ Added@babel/helpers@7.25.6(transitive)
+ Added@babel/highlight@7.24.7(transitive)
+ Added@babel/parser@7.25.6(transitive)
+ Added@babel/template@7.25.0(transitive)
+ Added@babel/traverse@7.25.6(transitive)
+ Added@babel/types@7.25.6(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedbrowserslist@4.24.0(transitive)
+ Addedcaniuse-lite@1.0.30001663(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedelectron-to-chromium@1.5.28(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@2.5.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addedpicocolors@1.1.0(transitive)
+ Addedreshow-runtime@0.17.15(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
+ Addedupdate-browserslist-db@1.1.0(transitive)
+ Addedyallist@3.1.1(transitive)
- Removedreshow-constant@*