This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
docs(Examples): allow to use TS in examples #617
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5bcb881
docs(Examples): allow to use TS in examples
layershifter 9326241
add jsdoc
layershifter 4a0f1b8
Merge branch 'master' of https://github.com/stardust-ui/react into do…
layershifter e0bc87e
fix typo
layershifter 2cc4aa7
Merge branch 'master' of https://github.com/stardust-ui/react into do…
layershifter b7ccf3e
rename file
layershifter 1dcb574
add comment
layershifter cba2827
`createExample` to `createExampleSourceCode`
layershifter ac10ffe
rework with `path.relative()`
layershifter 4a7f5a3
remove JSON files on remove tsx
layershifter 8754a60
Merge branches 'docs/use-ts' and 'master' of https://github.com/stard…
layershifter bdb403f
Merge branches 'docs/use-ts' and 'master' of https://github.com/stard…
layershifter ed110c5
create getRelativePathToSource function
layershifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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' | ||
layershifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import * as historyApiFallback from 'connect-history-api-fallback' | |
import * as express from 'express' | ||
import { task, src, dest, lastRun, parallel, series, watch } from 'gulp' | ||
import * as remember from 'gulp-remember' | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import * as rimraf from 'rimraf' | ||
import * as through2 from 'through2' | ||
|
@@ -14,7 +15,9 @@ import config from '../../../config' | |
import gulpComponentMenu from '../plugins/gulp-component-menu' | ||
import gulpComponentMenuBehaviors from '../plugins/gulp-component-menu-behaviors' | ||
import gulpExampleMenu from '../plugins/gulp-example-menu' | ||
import gulpExampleSource from '../plugins/gulp-example-source' | ||
import gulpReactDocgen from '../plugins/gulp-react-docgen' | ||
import { getRelativePathToSourceFile } from '../plugins/util' | ||
|
||
const { paths } = config | ||
const g = require('gulp-load-plugins')() | ||
|
@@ -46,13 +49,18 @@ task('clean:docs:example-menus', cb => { | |
rimraf(paths.docsSrc('exampleMenus'), cb) | ||
}) | ||
|
||
task('clean:docs:example-sources', cb => { | ||
rimraf(paths.docsSrc('exampleSources'), cb) | ||
}) | ||
|
||
task( | ||
'clean:docs', | ||
parallel( | ||
'clean:docs:component-menu', | ||
'clean:docs:component-menu-behaviors', | ||
'clean:docs:dist', | ||
'clean:docs:example-menus', | ||
'clean:docs:example-sources', | ||
), | ||
) | ||
|
||
|
@@ -62,7 +70,8 @@ task( | |
|
||
const componentsSrc = [`${paths.posix.src()}/components/*/[A-Z]*.tsx`, '!**/Slot.tsx'] | ||
const behaviorSrc = [`${paths.posix.src()}/lib/accessibility/Behaviors/*/[a-z]*.ts`] | ||
const examplesSrc = `${paths.posix.docsSrc()}/examples/*/*/*/index.tsx` | ||
const examplesIndexSrc = `${paths.posix.docsSrc()}/examples/*/*/*/index.tsx` | ||
const examplesSrc = `${paths.posix.docsSrc()}/examples/*/*/*/!(*index|.knobs).tsx` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for filtering out |
||
const markdownSrc = [ | ||
'.github/CONTRIBUTING.md', | ||
'.github/setup-local-development.md', | ||
|
@@ -92,18 +101,25 @@ task('build:docs:component-menu-behaviors', () => | |
) | ||
|
||
task('build:docs:example-menu', () => | ||
src(examplesSrc, { since: lastRun('build:docs:example-menu') }) | ||
src(examplesIndexSrc, { since: lastRun('build:docs:example-menu') }) | ||
.pipe(remember('example-menu')) // FIXME: with watch this unnecessarily processes index files for all examples | ||
.pipe(gulpExampleMenu()) | ||
.pipe(dest(paths.docsSrc('exampleMenus'))), | ||
) | ||
|
||
task('build:docs:example-sources', () => | ||
src(examplesSrc, { since: lastRun('build:docs:example-sources') }) | ||
.pipe(gulpExampleSource()) | ||
.pipe(dest(paths.docsSrc('exampleSources'))), | ||
) | ||
|
||
task( | ||
'build:docs:json', | ||
parallel( | ||
series('build:docs:docgen', 'build:docs:component-menu'), | ||
'build:docs:component-menu-behaviors', | ||
'build:docs:example-menu', | ||
'build:docs:example-sources', | ||
), | ||
) | ||
|
||
|
@@ -218,10 +234,21 @@ task('watch:docs', cb => { | |
watch(componentsSrc, series('build:docs:docgen')).on('change', handleWatchChange) | ||
|
||
// rebuild example menus | ||
watch(examplesSrc, series('build:docs:example-menu')) | ||
watch(examplesIndexSrc, series('build:docs:example-menu')) | ||
.on('change', handleWatchChange) | ||
.on('unlink', path => handleWatchUnlink('example-menu', path)) | ||
|
||
watch(examplesSrc, series('build:docs:example-sources')) | ||
.on('change', handleWatchChange) | ||
.on('unlink', filePath => { | ||
log(`File ${filePath} was deleted, running tasks...`) | ||
|
||
const sourceFilename = getRelativePathToSourceFile(filePath) | ||
const sourcePath = config.paths.docsSrc('exampleSources', sourceFilename) | ||
|
||
fs.unlinkSync(sourcePath) | ||
}) | ||
|
||
watch(behaviorSrc, series('build:docs:component-menu-behaviors')) | ||
.on('change', handleWatchChange) | ||
.on('unlink', path => handleWatchUnlink('component-menu-behaviors', path)) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.