Gathering detailed insights and metrics for ts-import-plugin
Gathering detailed insights and metrics for ts-import-plugin
Gathering detailed insights and metrics for ts-import-plugin
Gathering detailed insights and metrics for ts-import-plugin
npm install ts-import-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
610 Stars
1,626 Commits
36 Forks
5 Watching
7 Branches
14 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
TypeScript (78.59%)
JavaScript (21.03%)
Shell (0.37%)
Cumulative downloads
Total Downloads
Last day
0.7%
3,581
Compared to previous day
Last week
0.3%
18,961
Compared to previous week
Last month
27.3%
82,090
Compared to previous month
Last year
0.1%
714,650
Compared to previous year
1
44
Modular import plugin for TypeScript, compatible with antd, antd-mobile and so on.
webpack template ./webpack.config.js
, run: npm start
to see the bundle analyzer.
This plugin is not work if your are using
import * as _ from 'lodash'
orimport _ from 'lodash'
transform such code:
1import { Alert, Card as C } from 'antd'
into:
1import Alert from 'antd/lib/alert' 2import 'antd/lib/alert/style/index.less' 3import { default as C } from 'antd/lib/card' 4import 'antd/lib/card/style/index.less'
1//tsconfig.json 2{ 3 ... 4 "module": "ESNext", 5 ... 6}
1// webpack.config.js 2const tsImportPluginFactory = require('ts-import-plugin') 3 4module.exports = { 5 // ... 6 module: { 7 rules: [ 8 { 9 test: /\.(jsx|tsx|js|ts)$/, 10 loader: 'ts-loader', 11 options: { 12 transpileOnly: true, 13 getCustomTransformers: () => ({ 14 before: [tsImportPluginFactory(/** options */)], 15 }), 16 compilerOptions: { 17 module: 'es2015', 18 }, 19 }, 20 exclude: /node_modules/, 21 }, 22 ], 23 }, 24 // ... 25}
1//tsconfig.json 2{ 3 ... 4 "module": "ESNext", 5 ... 6}
1// webpack.config.js 2const tsImportPluginFactory = require('ts-import-plugin') 3 4module.exports = { 5 // ... 6 module: { 7 rules: [ 8 { 9 test: /\.tsx?$/, 10 loader: 'awesome-typescript-loader', 11 options: { 12 getCustomTransformers: () => ({ 13 before: [tsImportPluginFactory(/** options */)], 14 }), 15 }, 16 exclude: /node_modules/, 17 }, 18 ], 19 }, 20 // ... 21}
1import typescript from 'rollup-plugin-typescript2' // or '@rollup/plugin-typescript' 2import createTransformer from 'ts-import-plugin' 3 4const transformer = createTransformer({ 5 libraryDirectory: 'es', 6 libraryName: 'antd', 7 style: true, 8}) 9 10export default { 11 input: `./test/fixtures/index.tsx`, 12 plugins: [ 13 typescript({ 14 clean: true, 15 transformers: [ 16 () => ({ 17 before: transformer, 18 }), 19 ], 20 }), 21 ], 22 output: [ 23 { 24 file: `./dist/rollup.dist.js`, 25 format: 'esm', 26 }, 27 ], 28}
options
can be an object:
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'.
default 'antd'
style boolean | string | ((path: string) => string)
default false
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
- 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
- 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
- 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:
1tsImportPluginFactory({
2 libraryName: 'antd',
3 libraryDirectory: 'lib',
4 style: true,
5})
1{ 2 libraryName: '@material-ui/core', 3 libraryDirectory: '', 4 camel2DashComponentName: false 5}
options
can be an array:
example:
1;[ 2 { 3 libraryName: 'antd', 4 libraryDirectory: 'lib', 5 style: true, 6 }, 7 { 8 libraryName: '@material-ui/core', 9 libraryDirectory: '', 10 camel2DashComponentName: false, 11 }, 12]
1const transformerFactory = require('ts-import-plugin') 2// with less 3transformerFactory({ style: true }) 4// with css 5transformerFactory({ style: 'css' }) 6// without style 7transformerFactory()
notice you should manual
import 'lodash/core'
in your project if your are usingimport { chain } from 'lodash'
.
1transformerFactory({
2 style: false,
3 libraryName: 'lodash',
4 libraryDirectory: null,
5 camel2DashComponentName: false,
6})
1// with css.web
2transformerFactory({ libraryName: 'antd-mobile', style: 'css', styleExt: 'css.web' })
3
4// antd-mobile recently changed styleExt. If error occurs with prev, try next.
5transformerFactory({ libraryName: 'antd-mobile', style: 'css' })
1import { Button } from '@material-ui/core' 2import { Remove, Refresh, Add } from '@material-ui/icons'
1transformerFactory({ 2 libraryName: '@material-ui/core', 3 libraryDirectory: '', 4 camel2DashComponentName: false, 5}) 6 7// svg-icons 8transformerFactory({ 9 libraryDirectory: (importName) => { 10 const stringVec = importName 11 .split(/([A-Z][a-z]+|[0-9]*)/) 12 .filter((s) => s.length) 13 .map((s) => s.toLocaleLowerCase()) 14 15 return stringVec.reduce((acc, cur, index) => { 16 if (index > 1) { 17 return acc + '-' + cur 18 } else if (index === 1) { 19 return acc + '/' + cur 20 } 21 return acc + cur 22 }, '') 23 }, 24 libraryName: '@material-ui/icons', 25 style: false, 26 camel2DashComponentName: false, 27})
1import { Button } from 'element-ui'
1transformerFactory({ 2 libraryName: 'element-ui', 3 libraryDirectory: 'lib', 4 camel2DashComponentName: true, 5 style: (path: string) => join('element-ui', 'lib', 'theme-chalk', `${camel2Dash(basename(path, '.js'))}.css`), 6})
see rxjs-webpack-treeshaking-example for more details
only compatible for 5.5+
1transformerFactory({
2 libraryDirectory: '../_esm2015/operators',
3 libraryName: 'rxjs/operators',
4 style: false,
5 camel2DashComponentName: false,
6 transformToDefaultImport: false,
7})
1transformerFactory([
2 {
3 libraryDirectory: '../_esm5/internal/operators',
4 libraryName: 'rxjs/operators',
5 camel2DashComponentName: false,
6 transformToDefaultImport: false,
7 },
8 {
9 libraryDirectory: '../_esm5/internal/observable',
10 libraryName: 'rxjs',
11 camel2DashComponentName: false,
12 transformToDefaultImport: false,
13 },
14])
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
2 existing vulnerabilities detected
Details
Reason
9 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 7
Reason
Found 0/13 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More