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

refactor(gulp): refactor and optimize tasks with gulp-cache #661

Merged
merged 4 commits into from
Jan 8, 2019
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
15 changes: 8 additions & 7 deletions build/gulp/plugins/gulp-example-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ export default () =>
const sourcePath = getRelativePathToSourceFile(file.path)
const source = createExampleSourceCode(file)

cb(
null,
new Vinyl({
path: sourcePath,
contents: Buffer.from(JSON.stringify(source, null, 2)),
}),
)
const sourceFile = new Vinyl({
path: sourcePath,
contents: Buffer.from(JSON.stringify(source, null, 2)),
})
// `gulp-cache` relies on this private entry
sourceFile._cachedKey = file._cachedKey

cb(null, sourceFile)
})
16 changes: 6 additions & 10 deletions build/gulp/plugins/gulp-react-docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import * as through2 from 'through2'
import * as Vinyl from 'vinyl'

import { getComponentInfo, getBufferChecksum, getInfoChecksum } from './util'
import { getComponentInfo } from './util'

const pluginName = 'gulp-react-docgen'

Expand All @@ -19,20 +19,16 @@ export default () =>
return
}

const infoFilename = file.basename.replace(/\.tsx$/, '.info.json')
const nextChecksum = getBufferChecksum(file.contents)

if (getInfoChecksum(infoFilename) === nextChecksum) {
cb(null)
return
}

try {
const contents = getComponentInfo(file.path, nextChecksum)
const infoFilename = file.basename.replace(/\.tsx$/, '.info.json')
const contents = getComponentInfo(file.path)

const infoFile = new Vinyl({
path: `./${infoFilename}`,
contents: Buffer.from(JSON.stringify(contents, null, 2)),
})
// `gulp-cache` relies on this private entry
infoFile._cachedKey = file._cachedKey

cb(null, infoFile)
} catch (err) {
Expand Down
21 changes: 0 additions & 21 deletions build/gulp/plugins/util/checksumUtils.ts

This file was deleted.

5 changes: 1 addition & 4 deletions build/gulp/plugins/util/getComponentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface BehaviorInfo {
category: string
}

const getComponentInfo = (filepath: string, checksum?: string) => {
const getComponentInfo = (filepath: string) => {
const absPath = path.resolve(process.cwd(), filepath)

const dir = path.dirname(absPath)
Expand Down Expand Up @@ -43,9 +43,6 @@ const getComponentInfo = (filepath: string, checksum?: string) => {
// remove keys we don't use
delete info.methods

// add checksum
info.checksum = checksum

// add exported Component info
const Component = require(absPath).default
info.constructorName = _.get(Component, 'prototype.constructor.name', null)
Expand Down
1 change: 0 additions & 1 deletion build/gulp/plugins/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './checksumUtils'
export { default as getComponentInfo } from './getComponentInfo'
export { default as getRelativePathToSourceFile } from './getRelativePathToSourceFile'
export { default as parseDefaultValue } from './parseDefaultValue'
Expand Down
15 changes: 13 additions & 2 deletions build/gulp/tasks/docs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,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 cache from 'gulp-cache'
import * as remember from 'gulp-remember'
import * as fs from 'fs'
import * as path from 'path'
Expand Down Expand Up @@ -33,6 +34,8 @@ const handleWatchUnlink = (group, path) => {
// Clean
// ----------------------------------------

task('clean:cache', () => cache.clearAll())

task('clean:docs:component-menu', cb => {
rimraf(paths.docsSrc('componentMenu.json'), cb)
})
Expand Down Expand Up @@ -83,7 +86,11 @@ const markdownSrc = [

task('build:docs:component-info', () =>
src(componentsSrc, { since: lastRun('build:docs:component-info') })
.pipe(gulpReactDocgen())
.pipe(
cache(gulpReactDocgen(), {
name: 'componentInfo',
}),
)
.pipe(dest(paths.docsSrc('componentInfo'))),
)

Expand All @@ -109,7 +116,11 @@ task('build:docs:example-menu', () =>

task('build:docs:example-sources', () =>
src(examplesSrc, { since: lastRun('build:docs:example-sources') })
.pipe(gulpExampleSource())
.pipe(
cache(gulpExampleSource(), {
name: 'exampleSources',
}),
)
.pipe(dest(paths.docsSrc('exampleSources'))),
)

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:docs": "gulp --series dll build:docs",
"build:dist": "gulp --series build:dist",
"ci": "yarn lint && yarn prettier && yarn test --strict",
"clean:cache": "gulp clean:cache",
"predeploy:docs": "cross-env NODE_ENV=production yarn build:docs",
"deploy:docs": "gulp deploy:docs",
"lint": "tslint -t stylish -p .",
Expand Down Expand Up @@ -114,6 +115,7 @@
"gh-pages": "^1.0.0",
"glob": "^7.1.2",
"gulp": "^4.0.0",
"gulp-cache": "^1.0.2",
"gulp-debug": "^4.0.0",
"gulp-load-plugins": "^1.5.0",
"gulp-plumber": "^1.2.0",
Expand Down
24 changes: 23 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,16 @@ cache-base@^1.0.1:
union-value "^1.0.0"
unset-value "^1.0.0"

cache-swap@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/cache-swap/-/cache-swap-0.3.0.tgz#1c541aa108a50106f630bdd98fe1dec8ba133f51"
integrity sha1-HFQaoQilAQb2ML3Zj+HeyLoTP1E=
dependencies:
graceful-fs "^4.1.2"
mkdirp "^0.5.1"
object-assign "^4.0.1"
rimraf "^2.4.0"

callsites@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50"
Expand Down Expand Up @@ -4312,6 +4322,18 @@ gud@^1.0.0:
resolved "https://registry.yarnpkg.com/gud/-/gud-1.0.0.tgz#a489581b17e6a70beca9abe3ae57de7a499852c0"
integrity sha512-zGEOVKFM5sVPPrYs7J5/hYEw2Pof8KCyOwyhG8sAF26mCAeUFAcYPu1mwB7hhpIP29zOIBaDqwuHdLp0jvZXjw==

gulp-cache@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/gulp-cache/-/gulp-cache-1.0.2.tgz#064ed713de47b0ff26b491abd899590891e426f4"
integrity sha512-SFtWmWYARVzbzb6UiRZhZxpGo9oyXCOuXdTa0ML8CWQdFRTYZCfddn/grXG6SzdiY/D4rOnqpDJ8R2Trv0SQRQ==
dependencies:
babel-runtime "^6.26.0"
cache-swap "^0.3.0"
object.pick "^1.3.0"
plugin-error "^0.1.2"
through2 "^2.0.3"
vinyl "^2.1.0"

gulp-cli@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/gulp-cli/-/gulp-cli-2.0.1.tgz#7847e220cb3662f2be8a6d572bf14e17be5a994b"
Expand Down Expand Up @@ -9007,7 +9029,7 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

rimraf@^2.2.8, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
rimraf@^2.2.8, rimraf@^2.4.0, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
integrity sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==
Expand Down