Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit a9683e2

Browse files
committed
restore imports, introduce transformer
1 parent 1929f00 commit a9683e2

File tree

14 files changed

+123
-41
lines changed

14 files changed

+123
-41
lines changed

build/gulp/tasks/dist.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as rimraf from 'rimraf'
44
import * as webpack from 'webpack'
55

66
import config from '../../../config'
7+
import importsTransformer from '../../ts/transformer-namespace-imports'
78

89
const { paths } = config
910
const g = require('gulp-load-plugins')()
@@ -38,7 +39,16 @@ task('build:dist:commonjs', () => {
3839
task('build:dist:es', () => {
3940
const tsConfig = paths.base('build/tsconfig.es.json')
4041
const settings = { declaration: true }
41-
const typescript = g.typescript.createProject(tsConfig, settings)
42+
const typescript = g.typescript.createProject(tsConfig, {
43+
...settings,
44+
getCustomTransformers: () => ({
45+
before: [
46+
importsTransformer({
47+
moduleNames: ['classnames', 'color'],
48+
}),
49+
],
50+
}),
51+
})
4252

4353
const { dts, js } = src(paths.src('**/*.{ts,tsx}')).pipe(typescript())
4454
const types = src(paths.base('types/**'))
@@ -76,7 +86,7 @@ task('build:dist:umd', cb => {
7686
})
7787
})
7888

79-
task('build:dist', parallel('build:dist:commonjs', 'build:dist:es', 'build:dist:umd'))
89+
task('build:dist', parallel('build:dist:es'))
8090

8191
// ----------------------------------------
8292
// Default
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as ts from 'typescript'
2+
3+
type Options = {
4+
moduleNames: string[]
5+
}
6+
7+
/**
8+
* A factory that creates ES default import, i.e: import _ from 'lodash'
9+
*/
10+
const createDefaultImport = (importName: string, moduleName: string) =>
11+
ts.createImportDeclaration(
12+
undefined,
13+
undefined,
14+
ts.createImportClause(ts.createIdentifier(importName), undefined),
15+
ts.createStringLiteral(moduleName),
16+
)
17+
18+
/**
19+
* A functions transforms ES import to default if it's required:
20+
* import * as _ from 'lodash' - will be transformed
21+
* import { pick } from 'lodash' - will be skipped
22+
* import _ from 'lodash' - will be skipped
23+
*/
24+
const transformImportIfRequired = (
25+
node: ts.ImportDeclaration,
26+
moduleNames: string[],
27+
): ts.ImportDeclaration => {
28+
const importClause = node.importClause
29+
// Heads up!
30+
// We need there slice quoutes: 'color' => color
31+
const importName = node.moduleSpecifier.getText().slice(1, -1)
32+
const includes = moduleNames.indexOf(importName) !== -1
33+
34+
if (includes && ts.isNamespaceImport(importClause.namedBindings)) {
35+
return createDefaultImport(importClause.namedBindings.name.text, importName)
36+
}
37+
38+
return node
39+
}
40+
41+
const createTransformer = (
42+
options: Partial<Options> = {},
43+
): ts.TransformerFactory<ts.SourceFile> => {
44+
return (context: ts.TransformationContext) => {
45+
return (node: ts.SourceFile) => {
46+
const visitor: ts.Visitor = (node: ts.Node) => {
47+
if (ts.isImportDeclaration(node)) {
48+
return transformImportIfRequired(node, options.moduleNames)
49+
}
50+
51+
return ts.visitEachChild(node, visitor, context)
52+
}
53+
54+
return ts.visitNode(node, visitor)
55+
}
56+
}
57+
}
58+
59+
export default createTransformer

docs/src/views/IntegrateCustomComponents.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import cx from 'classnames'
2+
import * as cx from 'classnames'
33
import { NavLink } from 'react-router-dom'
44
import { Header } from 'semantic-ui-react'
55
import {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
},
7676
"devDependencies": {
7777
"@babel/standalone": "^7.1.0",
78+
"@types/classnames": "^2.2.6",
7879
"@types/color": "^3.0.0",
7980
"@types/enzyme": "^3.1.14",
8081
"@types/faker": "^4.1.3",
@@ -112,7 +113,7 @@
112113
"gulp-rename": "^1.3.0",
113114
"gulp-replace": "^1.0.0",
114115
"gulp-transform": "^3.0.5",
115-
"gulp-typescript": "^5.0.0-alpha.1",
116+
"gulp-typescript": "^5.0.0",
116117
"gulp-util": "^3.0.8",
117118
"html-webpack-plugin": "^3.2.0",
118119
"husky": "^0.14.3",

src/components/Chat/ChatMessage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import * as PropTypes from 'prop-types'
3-
import cx from 'classnames'
3+
import * as cx from 'classnames'
44

55
import {
66
childrenExist,

src/components/Input/Input.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import * as PropTypes from 'prop-types'
3-
import cx from 'classnames'
3+
import * as cx from 'classnames'
44
import * as _ from 'lodash'
55

66
import {

src/components/ItemLayout/ItemLayout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import * as PropTypes from 'prop-types'
3-
import cx from 'classnames'
3+
import * as cx from 'classnames'
44

55
import {
66
createShorthandFactory,

src/components/Layout/Layout.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import * as PropTypes from 'prop-types'
3-
import cx from 'classnames'
3+
import * as cx from 'classnames'
44

55
import { UIComponent, UIComponentProps, commonPropTypes } from '../../lib'
66
import { Extendable } from '../../../types/utils'

src/components/Menu/MenuItem.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as _ from 'lodash'
2-
import cx from 'classnames'
2+
import * as cx from 'classnames'
33
import * as PropTypes from 'prop-types'
44
import * as React from 'react'
55

src/lib/accessibility/FocusZone/FocusZone.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
FocusZoneProps,
99
} from './FocusZone.types'
1010
import * as keyboardKey from 'keyboard-key'
11-
import cx from 'classnames'
11+
import * as cx from 'classnames'
1212
import * as _ from 'lodash'
1313
import {
1414
getNextElement,

src/lib/factories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as _ from 'lodash'
2-
import cx from 'classnames'
2+
import * as cx from 'classnames'
33
import * as React from 'react'
44
import {
55
ShorthandValue,

src/lib/renderComponent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cx from 'classnames'
1+
import * as cx from 'classnames'
22
import * as React from 'react'
33
import * as _ from 'lodash'
44
import { FelaTheme } from 'react-fela'

types/classnames/index.d.ts

-17
This file was deleted.

yarn.lock

+41-12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.9.tgz#b5990152604c2ada749b7f88cab3476f21f39d7b"
101101
integrity sha512-q6LuBI0t5u04f0Q4/R+cGBqIbZMtJkVvCSF+nTfFBBdQqQvJR/mNHeWjRkszyLl7oyf2rDoKUYMEjTw5AV0hiw==
102102

103+
"@types/classnames@^2.2.6":
104+
version "2.2.6"
105+
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.6.tgz#dbe8a666156d556ed018e15a4c65f08937c3f628"
106+
integrity sha512-XHcYvVdbtAxVstjKxuULYqYaWIzHR15yr1pZj4fnGChuBVJlIAp9StJna0ZJNSgxPh4Nac2FL4JM3M11Tm6fqQ==
107+
103108
"@types/color-convert@*":
104109
version "1.9.0"
105110
resolved "https://registry.yarnpkg.com/@types/color-convert/-/color-convert-1.9.0.tgz#bfa8203e41e7c65471e9841d7e306a7cd8b5172d"
@@ -505,16 +510,16 @@ ansi-colors@^1.0.1:
505510
dependencies:
506511
ansi-wrap "^0.1.0"
507512

508-
ansi-colors@^2.0.2:
509-
version "2.0.2"
510-
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-2.0.2.tgz#1a95e0163c461846d44cbdea97bb2ac1aa35f1b2"
511-
integrity sha512-lqxzclDUlcMM72ab/b4imVvMCyO+jELG+a5DPuE2qFAwGkzQilb6iEgzQPhSmNCM7onBHsq/9Mggh4HaqilTlA==
512-
513513
ansi-colors@^3.0.0:
514514
version "3.2.1"
515515
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.1.tgz#9638047e4213f3428a11944a7d4b31cba0a3ff95"
516516
integrity sha512-Xt+zb6nqgvV9SWAVp0EG3lRsHcbq5DDgqjPPz6pwgtj6RKz65zGXMNa82oJfOSBA/to6GmRP7Dr+6o+kbApTzQ==
517517

518+
ansi-colors@^3.0.5:
519+
version "3.2.2"
520+
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.2.tgz#e49349137dbeb6d381b91e607c189915e53265ba"
521+
integrity sha512-kJmcp4PrviBBEx95fC3dYRiC/QSN3EBd0GU1XoNEk/IuUa92rsB6o90zP3w5VAyNznR38Vkc9i8vk5zK6T7TxA==
522+
518523
ansi-cyan@^0.1.1:
519524
version "0.1.1"
520525
resolved "https://registry.yarnpkg.com/ansi-cyan/-/ansi-cyan-0.1.1.tgz#538ae528af8982f28ae30d86f2f17456d2609873"
@@ -4206,15 +4211,15 @@ gulp-transform@^3.0.5:
42064211
gulp-util "^3.0.8"
42074212
tslib "^1.7.1"
42084213

4209-
gulp-typescript@^5.0.0-alpha.1:
4210-
version "5.0.0-alpha.3"
4211-
resolved "https://registry.yarnpkg.com/gulp-typescript/-/gulp-typescript-5.0.0-alpha.3.tgz#c49a306cbbb8c97f5fe8a79208671b6642ef9861"
4212-
integrity sha512-6iSBjqBXAUqRsLUh/9XtlOnSzpPMbLrr5rqGj4UPLtGpDwFHW/fVTuRgv6LAWiKesLIUDDM0ourxvcpu2trecQ==
4214+
gulp-typescript@^5.0.0:
4215+
version "5.0.0"
4216+
resolved "https://registry.yarnpkg.com/gulp-typescript/-/gulp-typescript-5.0.0.tgz#aac065a88bca95d1f960d4934a307feed2d091f8"
4217+
integrity sha512-lMj2U+Ni6HyFaY2nr1sSQ6D014eHil5L1i52XWBaAQUR9UAUUp9btnm4yRBT2Jb8xhrwqmhMssZf/g2B7cinCA==
42134218
dependencies:
4214-
ansi-colors "^2.0.2"
4219+
ansi-colors "^3.0.5"
42154220
plugin-error "^1.0.1"
42164221
source-map "^0.7.3"
4217-
through2 "^2.0.3"
4222+
through2 "^3.0.0"
42184223
vinyl "^2.1.0"
42194224
vinyl-fs "^3.0.3"
42204225

@@ -8394,6 +8399,15 @@ [email protected], readable-stream@^1.1.8, readable-stream@~1.1.9:
83948399
isarray "0.0.1"
83958400
string_decoder "~0.10.x"
83968401

8402+
"readable-stream@2 || 3":
8403+
version "3.0.6"
8404+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.0.6.tgz#351302e4c68b5abd6a2ed55376a7f9a25be3057a"
8405+
integrity sha512-9E1oLoOWfhSXHGv6QlwXJim7uNzd9EVlWK+21tCU9Ju/kR0/p2AZYPz4qSchgO8PlLIH4FpZYfzwS+rEksZjIg==
8406+
dependencies:
8407+
inherits "^2.0.3"
8408+
string_decoder "^1.1.1"
8409+
util-deprecate "^1.0.1"
8410+
83978411
readable-stream@~2.0.6:
83988412
version "2.0.6"
83998413
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
@@ -9688,6 +9702,13 @@ string_decoder@^1.0.0, string_decoder@~1.1.1:
96889702
dependencies:
96899703
safe-buffer "~5.1.0"
96909704

9705+
string_decoder@^1.1.1:
9706+
version "1.2.0"
9707+
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.2.0.tgz#fe86e738b19544afe70469243b2a1ee9240eae8d"
9708+
integrity sha512-6YqyX6ZWEYguAxgZzHGL7SsCeGx3V2TtOTqZz1xSTSWnqsbWwbptafNyvf/ACquZUXV3DANr5BDIwNYe1mN42w==
9709+
dependencies:
9710+
safe-buffer "~5.1.0"
9711+
96919712
string_decoder@~0.10.x:
96929713
version "0.10.31"
96939714
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
@@ -9945,6 +9966,14 @@ through2@^2.0.0, through2@^2.0.3, through2@~2.0.0:
99459966
readable-stream "^2.1.5"
99469967
xtend "~4.0.1"
99479968

9969+
through2@^3.0.0:
9970+
version "3.0.0"
9971+
resolved "https://registry.yarnpkg.com/through2/-/through2-3.0.0.tgz#468b461df9cd9fcc170f22ebf6852e467e578ff2"
9972+
integrity sha512-8B+sevlqP4OiCjonI1Zw03Sf8PuV1eRsYQgLad5eonILOdyeRsY27A/2Ze8IlvlMvq31OH+3fz/styI7Ya62yQ==
9973+
dependencies:
9974+
readable-stream "2 || 3"
9975+
xtend "~4.0.1"
9976+
99489977
through@^2.3.6:
99499978
version "2.3.8"
99509979
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
@@ -10581,7 +10610,7 @@ use@^3.1.0:
1058110610
dependencies:
1058210611
kind-of "^6.0.2"
1058310612

10584-
util-deprecate@~1.0.1:
10613+
util-deprecate@^1.0.1, util-deprecate@~1.0.1:
1058510614
version "1.0.2"
1058610615
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
1058710616
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=

0 commit comments

Comments
 (0)