Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): fix issues with local builds #3098

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ class ComponentExample extends PureComponent {
}

componentWillReceiveProps(nextProps) {
const { examplePath, exampleSources, location } = nextProps
const nextSourceCode = exampleSources[examplePath]

// deactivate examples when switching from one to the next
if (
this.isActiveHash() &&
this.isActiveState() &&
this.props.location.hash !== nextProps.location.hash
) {
if (this.isActiveHash() && this.isActiveState() && this.props.location.hash !== location.hash) {
this.clearActiveState()
}

// for local environment
if (process.env.NODE_ENV !== 'production' && this.getOriginalSourceCode() !== nextSourceCode) {
this.setState({ sourceCode: nextSourceCode })
}
}

clearActiveState = () => {
Expand Down
17 changes: 6 additions & 11 deletions gulp/plugins/gulp-example-source.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Vinyl from 'vinyl'
import gutil from 'gulp-util'
import _ from 'lodash'
import path from 'path'
import through from 'through2'
import Vinyl from 'vinyl'

// Heads up!
// This plugin is not universal, so it's okay to keep all existing sources as the state.
// https://github.com/Semantic-Org/Semantic-UI-React/issues/3095
const exampleSources = {}
const pluginName = 'gulp-example-source'

export default () => {
const exampleSources = {}

function bufferContents(file, enc, cb) {
if (file.isNull()) {
cb(null, file)
Expand Down Expand Up @@ -41,16 +43,9 @@ export default () => {
function endStream(cb) {
const file = new Vinyl({
path: './exampleSources.json',
contents: Buffer.from(JSON.stringify(exampleSources, null, 2)),
})
let existingSources = {}

// Heads up!
// In watch mode we should update only single entry that matches changed file.
if (file.contents) {
existingSources = JSON.parse(file.contents.toString())
}

file.contents = Buffer.from(JSON.stringify({ ...existingSources, ...exampleSources }, null, 2))
this.push(file)
cb()
}
Expand Down
37 changes: 27 additions & 10 deletions gulp/tasks/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ const { log } = g.util
const handleWatchChange = filename =>
log(`File ${path.basename(filename)} was changed, running tasks...`)

/**
* Converts paths with globs to supported by chokidar, is more specific for Windows where
* is different path sep.
* Example of the failure behaviour: "C:\projects\docs/examples/index.js"
*
* @param {String} directory
* @param {String} glob
* @returns {String}
*/
const toUniversalGlob = (directory, glob) => {
const relative = path
.relative(process.cwd(), directory)
.split(path.sep)
.join('/')

return `${relative}/${glob}`
}

// ----------------------------------------
// Clean
// ----------------------------------------
Expand Down Expand Up @@ -76,17 +94,17 @@ task('build:docs:static:start', (cb) => {
// ----------------------------------------

const componentsSrc = [
`${paths.src()}/addons/*/*.js`,
`${paths.src()}/behaviors/*/*.js`,
`${paths.src()}/elements/*/*.js`,
`${paths.src()}/collections/*/*.js`,
`${paths.src()}/modules/*/*.js`,
`${paths.src()}/views/*/*.js`,
toUniversalGlob(paths.src(), 'addons/*/*.js'),
toUniversalGlob(paths.src(), 'behaviors/*/*.js'),
toUniversalGlob(paths.src(), 'elements/*/*.js'),
toUniversalGlob(paths.src(), 'collections/*/*.js'),
toUniversalGlob(paths.src(), 'modules/*/*.js'),
toUniversalGlob(paths.src(), 'views/*/*.js'),
'!**/index.js',
]

const examplesSectionsSrc = `${paths.docsSrc()}/examples/*/*/*/index.js`
const examplesSrc = `${paths.docsSrc()}/examples/*/*/*/!(*index).js`
const examplesSectionsSrc = toUniversalGlob(paths.docsSrc(), 'examples/*/*/*/index.js')
const examplesSrc = toUniversalGlob(paths.docsSrc(), 'examples/*/*/*/!(*index).js')

task('build:docs:cname', (cb) => {
sh(`echo react.semantic-ui.com > ${paths.docsDist('CNAME')}`, cb)
Expand Down Expand Up @@ -151,7 +169,7 @@ task('deploy:docs', (cb) => {
// Watch
// ----------------------------------------

task('watch:docs', (cb) => {
task('watch:docs', () => {
// rebuild component info
watch(componentsSrc, series('build:docs:docgen', 'build:docs:static:reload')).on(
'change',
Expand All @@ -169,7 +187,6 @@ task('watch:docs', (cb) => {
'change',
handleWatchChange,
)
cb()
})

// ----------------------------------------
Expand Down