This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(Examples): allow to use TS in examples (#617)
* docs(Examples): allow to use TS in examples * add jsdoc * fix typo * rename file * add comment * `createExample` to `createExampleSourceCode` * rework with `path.relative()` * remove JSON files on remove tsx * create getRelativePathToSource function
- Loading branch information
1 parent
d1a9105
commit 03c0bd2
Showing
37 changed files
with
389 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as T from '@babel/types' | ||
import { BabelPlugin } from './types' | ||
|
||
/** | ||
* Creates an default import: | ||
* - import React from 'react' | ||
*/ | ||
const createDefaultImportDeclaration = ( | ||
t: typeof T, | ||
declaration: T.ImportDeclaration, | ||
specifier: T.ImportNamespaceSpecifier, | ||
): T.ImportDeclaration => | ||
t.importDeclaration( | ||
[t.importDefaultSpecifier(t.identifier(specifier.local.name))], | ||
t.stringLiteral(declaration.source.value), | ||
) | ||
|
||
/** | ||
* A plugin for Babel that performs AST transform: | ||
* - from: import * as _ from 'lodash' | ||
* - to: import _ from 'lodash' | ||
*/ | ||
const starImportToDefaultPlugin: BabelPlugin = ({ types: t }) => ({ | ||
visitor: { | ||
ImportDeclaration: path => { | ||
const { specifiers } = path.node | ||
const specifier = specifiers[0] | ||
|
||
if (specifiers.length === 1 && t.isImportNamespaceSpecifier(specifier)) { | ||
path.replaceWith(createDefaultImportDeclaration(t, path.node, specifier)) | ||
} | ||
}, | ||
}, | ||
}) | ||
|
||
export default starImportToDefaultPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as T from '@babel/types' | ||
import { NodePath } from '@babel/traverse' | ||
|
||
export type BabelPluginArguments = { | ||
types: typeof T | ||
} | ||
|
||
type BabelPluginVisitorFunction<T> = (path: NodePath<T>) => void | ||
type BabelPluginVisitor<T> = | ||
| BabelPluginVisitorFunction<T> | ||
| { | ||
exit: BabelPluginVisitorFunction<T> | ||
} | ||
|
||
export type BabelPlugin = ( | ||
options: BabelPluginArguments, | ||
) => { | ||
visitor: { | ||
// This type is extendable, feel to add own visitor types. | ||
ImportDeclaration: BabelPluginVisitor<T.ImportDeclaration> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as Babel from '@babel/core' | ||
import * as gutil from 'gulp-util' | ||
import * as prettier from 'prettier' | ||
import * as through from 'through2' | ||
import * as Vinyl from 'vinyl' | ||
|
||
import * as prettierConfig from '../../../.prettierrc.json' | ||
import { ExampleSource } from '../../../docs/src/types' | ||
import transformStarImportPlugin from '../../babel/transform-star-import-plugin' | ||
import { getRelativePathToSourceFile } from './util' | ||
|
||
const pluginName = 'gulp-example-source' | ||
|
||
const createExampleSourceCode = (file: Vinyl): ExampleSource => { | ||
const tsSource = file.contents.toString() | ||
|
||
const transformResult = Babel.transform(tsSource, { | ||
plugins: [transformStarImportPlugin], | ||
presets: [['@babel/preset-typescript', { allExtensions: true, isTSX: true }]], | ||
sourceType: 'module', | ||
}) | ||
const jsSource = prettier.format(transformResult.code, { | ||
...prettierConfig, | ||
parser: 'babylon', | ||
}) | ||
|
||
return { | ||
js: jsSource, | ||
ts: tsSource, | ||
} | ||
} | ||
|
||
export default () => | ||
through.obj((file: Vinyl, enc, cb) => { | ||
if (file.isNull()) { | ||
cb(null, file) | ||
return | ||
} | ||
|
||
if (file.isStream()) { | ||
cb(new gutil.PluginError(pluginName, 'Streaming is not supported')) | ||
return | ||
} | ||
|
||
const sourcePath = getRelativePathToSourceFile(file.path) | ||
const source = createExampleSourceCode(file) | ||
|
||
cb( | ||
null, | ||
new Vinyl({ | ||
path: sourcePath, | ||
contents: Buffer.from(JSON.stringify(source, null, 2)), | ||
}), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as path from 'path' | ||
import config from '../../../../config' | ||
|
||
const examplesPath = config.paths.docsSrc('examples', 'components') | ||
|
||
/** | ||
* Generates a relative path to a source file, outputs: | ||
* Chat/Types/ChatExample.shorthand.source.json | ||
*/ | ||
const getRelativePathToSourceFile = (filePath: string): string => | ||
`${path.relative(examplesPath, filePath).replace(/\.tsx$/, '')}.source.json` | ||
|
||
export default getRelativePathToSourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 13 additions & 7 deletions
20
docs/src/examples/components/Ref/Types/RefForwardingExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleDisabled.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleDisabled.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleError.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleSuccess.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleSuccess.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleTemporary.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleTemporary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleTruncated.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/States/TextExampleTruncated.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/Types/TextSizesExample.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/src/examples/components/Text/Types/TextWeightsExample.shorthand.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.