Socket
Socket
Sign inDemoInstall

babel-plugin-reshow-import-extension

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-reshow-import-extension - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

10

package.json
{
"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",

69

src/index.js

@@ -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 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc