Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-import-plugin

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-import-plugin - npm Package Compare versions

Comparing version 1.6.7 to 2.0.0

60

esm/index.js

@@ -1,2 +0,21 @@

import { __assign, __spreadArrays } from "tslib";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import * as ts from 'typescript';

@@ -21,7 +40,7 @@ import { join as pathJoin, sep } from 'path';

var str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, function ($1) { return "-" + $1.toLowerCase(); });
return str.replace(/([A-Z])/g, function ($1) { return "-".concat($1.toLowerCase()); });
}
function camel2Underline(_str) {
var str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, function ($1) { return "_" + $1.toLowerCase(); });
return str.replace(/([A-Z])/g, function ($1) { return "_".concat($1.toLowerCase()); });
}

@@ -63,3 +82,3 @@ function getImportedStructs(node) {

}
function createDistAst(struct, options) {
function createDistAst(context, struct, options) {
var astNodes = [];

@@ -78,3 +97,3 @@ var libraryName = options.libraryName, libraryOverride = options.libraryOverride;

if (process.env.NODE_ENV !== 'production' && libraryDirectory == null) {
console.warn("custom libraryDirectory resolve a " + libraryDirectory + " path");
console.warn("custom libraryDirectory resolve a ".concat(libraryDirectory, " path"));
}

@@ -85,3 +104,3 @@ var importPath = !libraryOverride ? join(libraryName, libraryDirectory) : libraryDirectory;

require.resolve(importPath, {
paths: __spreadArrays([process.cwd()], options.resolveContext),
paths: __spreadArray([process.cwd()], options.resolveContext, true),
});

@@ -91,14 +110,21 @@ }

canResolveImportPath = false;
astNodes.push(ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(_importName))])), ts.createLiteral(libraryName)));
if (options.failIfNotFound) {
throw new Error("Can not find component for library: ".concat(options.libraryName, " in ").concat(importPath));
}
astNodes.push(context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, undefined, context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(_importName)),
])), context.factory.createStringLiteral(libraryName)));
}
if (canResolveImportPath) {
var scriptNode = ts.createImportDeclaration(undefined, undefined, ts.createImportClause(struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? ts.createNamedImports([
ts.createImportSpecifier(options.transformToDefaultImport
? ts.createIdentifier('default')
: ts.createIdentifier(struct.importName), ts.createIdentifier(struct.variableName)),
var scriptNode = context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? context.factory.createNamedImports([
context.factory.createImportSpecifier(false, options.transformToDefaultImport
? context.factory.createIdentifier('default')
: context.factory.createIdentifier(struct.importName), context.factory.createIdentifier(struct.variableName)),
])
: options.transformToDefaultImport
? undefined
: ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(struct.importName))])), ts.createLiteral(importPath));
: context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(struct.importName)),
])), context.factory.createStringLiteral(importPath));
astNodes.push(scriptNode);

@@ -113,6 +139,6 @@ if (options.style) {

// eslint-disable-next-line no-restricted-syntax
stylePath = importPath + "/style/" + (style === true ? 'index' : style) + ".js";
stylePath = "".concat(importPath, "/style/").concat(style === true ? 'index' : style, ".js");
}
if (stylePath) {
var styleNode = ts.createImportDeclaration(undefined, undefined, undefined, ts.createLiteral(stylePath));
var styleNode = context.factory.createImportDeclaration(undefined, undefined, undefined, context.factory.createStringLiteral(stylePath));
astNodes.push(styleNode);

@@ -129,5 +155,7 @@ }

camel2DashComponentName: true,
camel2UnderlineComponentName: false,
transformToDefaultImport: true,
resolveContext: [],
libraryOverride: false,
failIfNotFound: false,
};

@@ -158,3 +186,3 @@ export function createTransformer(_options) {

return Array.from(structs).reduce(function (acc, struct) {
var nodes = createDistAst(struct, options);
var nodes = createDistAst(context, struct, options);
return acc.concat(nodes);

@@ -161,0 +189,0 @@ }, []);

@@ -11,2 +11,3 @@ import * as ts from 'typescript';

resolveContext?: string[];
failIfNotFound?: boolean;
}

@@ -13,0 +14,0 @@ export interface ImportedStruct {

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransformer = void 0;
var tslib_1 = require("tslib");
var ts = require("typescript");

@@ -24,7 +43,7 @@ var path_1 = require("path");

var str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, function ($1) { return "-" + $1.toLowerCase(); });
return str.replace(/([A-Z])/g, function ($1) { return "-".concat($1.toLowerCase()); });
}
function camel2Underline(_str) {
var str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, function ($1) { return "_" + $1.toLowerCase(); });
return str.replace(/([A-Z])/g, function ($1) { return "_".concat($1.toLowerCase()); });
}

@@ -66,3 +85,3 @@ function getImportedStructs(node) {

}
function createDistAst(struct, options) {
function createDistAst(context, struct, options) {
var astNodes = [];

@@ -81,3 +100,3 @@ var libraryName = options.libraryName, libraryOverride = options.libraryOverride;

if (process.env.NODE_ENV !== 'production' && libraryDirectory == null) {
console.warn("custom libraryDirectory resolve a " + libraryDirectory + " path");
console.warn("custom libraryDirectory resolve a ".concat(libraryDirectory, " path"));
}

@@ -88,3 +107,3 @@ var importPath = !libraryOverride ? join(libraryName, libraryDirectory) : libraryDirectory;

require.resolve(importPath, {
paths: tslib_1.__spreadArrays([process.cwd()], options.resolveContext),
paths: __spreadArray([process.cwd()], options.resolveContext, true),
});

@@ -94,14 +113,21 @@ }

canResolveImportPath = false;
astNodes.push(ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(_importName))])), ts.createLiteral(libraryName)));
if (options.failIfNotFound) {
throw new Error("Can not find component for library: ".concat(options.libraryName, " in ").concat(importPath));
}
astNodes.push(context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, undefined, context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(_importName)),
])), context.factory.createStringLiteral(libraryName)));
}
if (canResolveImportPath) {
var scriptNode = ts.createImportDeclaration(undefined, undefined, ts.createImportClause(struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? ts.createNamedImports([
ts.createImportSpecifier(options.transformToDefaultImport
? ts.createIdentifier('default')
: ts.createIdentifier(struct.importName), ts.createIdentifier(struct.variableName)),
var scriptNode = context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? context.factory.createNamedImports([
context.factory.createImportSpecifier(false, options.transformToDefaultImport
? context.factory.createIdentifier('default')
: context.factory.createIdentifier(struct.importName), context.factory.createIdentifier(struct.variableName)),
])
: options.transformToDefaultImport
? undefined
: ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(struct.importName))])), ts.createLiteral(importPath));
: context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(struct.importName)),
])), context.factory.createStringLiteral(importPath));
astNodes.push(scriptNode);

@@ -116,6 +142,6 @@ if (options.style) {

// eslint-disable-next-line no-restricted-syntax
stylePath = importPath + "/style/" + (style === true ? 'index' : style) + ".js";
stylePath = "".concat(importPath, "/style/").concat(style === true ? 'index' : style, ".js");
}
if (stylePath) {
var styleNode = ts.createImportDeclaration(undefined, undefined, undefined, ts.createLiteral(stylePath));
var styleNode = context.factory.createImportDeclaration(undefined, undefined, undefined, context.factory.createStringLiteral(stylePath));
astNodes.push(styleNode);

@@ -132,9 +158,11 @@ }

camel2DashComponentName: true,
camel2UnderlineComponentName: false,
transformToDefaultImport: true,
resolveContext: [],
libraryOverride: false,
failIfNotFound: false,
};
function createTransformer(_options) {
if (_options === void 0) { _options = {}; }
var mergeDefault = function (options) { return (tslib_1.__assign(tslib_1.__assign({}, defaultOptions), options)); };
var mergeDefault = function (options) { return (__assign(__assign({}, defaultOptions), options)); };
var optionsArray = Array.isArray(_options)

@@ -161,3 +189,3 @@ ? _options.map(function (options) { return mergeDefault(options); })

return Array.from(structs).reduce(function (acc, struct) {
var nodes = createDistAst(struct, options);
var nodes = createDistAst(context, struct, options);
return acc.concat(nodes);

@@ -164,0 +192,0 @@ }, []);

@@ -57,3 +57,3 @@ import * as ts from 'typescript';

}
function createDistAst(struct, options) {
function createDistAst(context, struct, options) {
const astNodes = [];

@@ -83,14 +83,21 @@ const { libraryName, libraryOverride } = options;

canResolveImportPath = false;
astNodes.push(ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(_importName))])), ts.createLiteral(libraryName)));
if (options.failIfNotFound) {
throw new Error(`Can not find component for library: ${options.libraryName} in ${importPath}`);
}
astNodes.push(context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, undefined, context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(_importName)),
])), context.factory.createStringLiteral(libraryName)));
}
if (canResolveImportPath) {
const scriptNode = ts.createImportDeclaration(undefined, undefined, ts.createImportClause(struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? ts.createNamedImports([
ts.createImportSpecifier(options.transformToDefaultImport
? ts.createIdentifier('default')
: ts.createIdentifier(struct.importName), ts.createIdentifier(struct.variableName)),
const scriptNode = context.factory.createImportDeclaration(undefined, undefined, context.factory.createImportClause(false, struct.variableName || !options.transformToDefaultImport ? undefined : ts.createIdentifier(struct.importName), struct.variableName
? context.factory.createNamedImports([
context.factory.createImportSpecifier(false, options.transformToDefaultImport
? context.factory.createIdentifier('default')
: context.factory.createIdentifier(struct.importName), context.factory.createIdentifier(struct.variableName)),
])
: options.transformToDefaultImport
? undefined
: ts.createNamedImports([ts.createImportSpecifier(undefined, ts.createIdentifier(struct.importName))])), ts.createLiteral(importPath));
: context.factory.createNamedImports([
context.factory.createImportSpecifier(false, undefined, context.factory.createIdentifier(struct.importName)),
])), context.factory.createStringLiteral(importPath));
astNodes.push(scriptNode);

@@ -108,3 +115,3 @@ if (options.style) {

if (stylePath) {
const styleNode = ts.createImportDeclaration(undefined, undefined, undefined, ts.createLiteral(stylePath));
const styleNode = context.factory.createImportDeclaration(undefined, undefined, undefined, context.factory.createStringLiteral(stylePath));
astNodes.push(styleNode);

@@ -121,5 +128,7 @@ }

camel2DashComponentName: true,
camel2UnderlineComponentName: false,
transformToDefaultImport: true,
resolveContext: [],
libraryOverride: false,
failIfNotFound: false,
};

@@ -149,3 +158,3 @@ export function createTransformer(_options = {}) {

return Array.from(structs).reduce((acc, struct) => {
const nodes = createDistAst(struct, options);
const nodes = createDistAst(context, struct, options);
return acc.concat(nodes);

@@ -152,0 +161,0 @@ }, []);

{
"name": "ts-import-plugin",
"version": "1.6.7",
"version": "2.0.0",
"description": "babel-plugin-import TypeScript version",

@@ -14,7 +14,10 @@ "main": "./index.js",

"build:next": "tsc -p src/tsconfig.json -m esnext --target ES2018 --outDir next -d false",
"cover": "rm -rf ./coverage ./.nyc_output && nyc --reporter=html --reporter=lcov --exclude=node_modules --exclude=test ava && nyc report",
"cover": "rm -rf ./coverage && c8 --reporter=lcov --exclude=node_modules --exclude=test ava && c8 report",
"lint": "eslint . -c ./.eslintrc.yml 'src/**/*.{ts,tsx}' './*.js'",
"start": "rm -rf dist && NODE_ENV=production webpack",
"test": "ava",
"watch": "tsc -p ./src/tsconfig.json -w --diagnostics"
"watch": "tsc -p ./src/tsconfig.json -w --diagnostics",
"_postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
},

@@ -40,49 +43,50 @@ "repository": {

"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@swc-node/register": "^1.0.2",
"@types/lodash": "^4.14.165",
"@types/material-ui": "^0.21.8",
"@types/node": "^14.14.10",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/parser": "^4.9.0",
"antd": "^4.9.1",
"ava": "^3.13.0",
"codecov": "^3.8.1",
"css-loader": "^5.0.1",
"cssnano": "^4.1.10",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-sonarjs": "^0.5.0",
"husky": "^4.3.0",
"json-schema": "^0.2.5",
"lint-staged": "^10.5.2",
"lodash": "^4.17.20",
"lodash-es": "^4.17.15",
"@swc-node/register": "^1.4.0",
"@types/lodash": "^4.14.177",
"@types/material-ui": "^0.21.12",
"@types/node": "^16.11.10",
"@types/react": "^17.0.36",
"@types/react-dom": "^17.0.11",
"@typescript-eslint/parser": "^5.4.0",
"antd": "^4.17.1",
"ava": "^3.15.0",
"c8": "^7.10.0",
"codecov": "^3.8.3",
"css-loader": "^6.5.1",
"cssnano": "^5.0.11",
"eslint": "^8.3.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-sonarjs": "^0.10.0",
"husky": "7",
"json-schema": "^0.4.0",
"lint-staged": "^12.1.2",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"material-ui": "^0.20.2",
"mini-css-extract-plugin": "^1.3.1",
"nyc": "^15.1.0",
"postcss-loader": "^4.1.0",
"prettier": "^2.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"rollup": "^2.34.0",
"rollup-plugin-typescript2": "^0.29.0",
"rxjs": "^6.6.3",
"mini-css-extract-plugin": "^2.4.5",
"pinst": "^2.1.6",
"postcss": "^8.3.11",
"postcss-loader": "^6.2.0",
"prettier": "^2.4.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rollup": "^2.60.1",
"rollup-plugin-typescript2": "^0.31.1",
"rxjs": "^7.4.0",
"shx": "^0.3.3",
"source-map-support": "^0.5.19",
"style-loader": "^2.0.0",
"ts-loader": "^8.0.11",
"ts-node": "^9.0.0",
"typescript": "^4.1.2",
"webpack": "^5.9.0",
"webpack-bundle-analyzer": "^4.2.0",
"webpack-cli": "^4.2.0"
"source-map-support": "^0.5.21",
"style-loader": "^3.3.1",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"typescript": "^4.5.2",
"webpack": "^5.64.2",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-cli": "^4.9.1"
},
"dependencies": {
"tslib": "^2.0.3"
"peerDependencies": {
"typescript": ">= 4.2"
},

@@ -119,9 +123,2 @@ "ava": {

},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"exclude": [
"test/**/*.{js,ts}"
],
"check-coverage": true
},
"funding": {

@@ -128,0 +125,0 @@ "type": "github",

@@ -106,3 +106,3 @@ [![Financial Contributors on Open Collective](https://opencollective.com/ts-import-plugin/all/badge.svg?label=financial+contributors)](https://opencollective.com/ts-import-plugin) [![npm version](https://badge.fury.io/js/ts-import-plugin.svg)](https://www.npmjs.com/package/ts-import-plugin)

```js
import typescript from 'rollup-plugin-typescript2'
import typescript from 'rollup-plugin-typescript2' // or '@rollup/plugin-typescript'
import createTransformer from 'ts-import-plugin'

@@ -137,3 +137,4 @@

- libraryName `string`
- libraryName `string` - The name of the library in the import. e.g. If using `import { foo } from 'Bar'`
the library should be set to 'bar'.

@@ -144,15 +145,24 @@ default `'antd'`

default `false`
- libraryDirectory `string | ((name: string) => string)`
- libraryDirectory `string | ((name: string) => string)` - The directory within the library to replace the import with.
e.g. If you have `import { foo } from 'Bar'`, it will be replaced to `import foo from `\`Bar/${libraryDirectory}/foo\``
default `'lib'`
- camel2DashComponentName `boolean`
- camel2DashComponentName `boolean` - Builtin method to use to transform the component name. This does transform the
component name from camelCase to dashed. e.g. `FooBar` gets transformed to `foo-bar`
default `true`
- camel2UnderlineComponentName `boolean`
- camel2UnderlineComponentName `boolean` - Builtin method to use to transform the component name. This does transform the
component name from camelCase to snake_case. e.g. `FooBar` gets transformed to `foo_bar`
default `false`
- libraryOverride `boolean`
- libraryOverride `boolean` - Setting to false (default) prepends the `libraryName` to the `libraryDirectory` (with a path separator)
set to true if you want to use the `libraryDirectory` as the full path for the import.
default `false`
- failIfNotFound `boolean` - If the component is not found in the library, the full library is imported by default.
set to true to fail the build.
default `false`
example:

@@ -159,0 +169,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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