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

Commit d86bbf6

Browse files
authored
refactor(gulp): refactor and optimize tasks with gulp-cache (#661)
* refactor(gulp): refactor and optimize tasks with `gulp-cache` * add clean cache command
1 parent 25d9f41 commit d86bbf6

File tree

8 files changed

+53
-46
lines changed

8 files changed

+53
-46
lines changed

build/gulp/plugins/gulp-example-source.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ export default () =>
4545
const sourcePath = getRelativePathToSourceFile(file.path)
4646
const source = createExampleSourceCode(file)
4747

48-
cb(
49-
null,
50-
new Vinyl({
51-
path: sourcePath,
52-
contents: Buffer.from(JSON.stringify(source, null, 2)),
53-
}),
54-
)
48+
const sourceFile = new Vinyl({
49+
path: sourcePath,
50+
contents: Buffer.from(JSON.stringify(source, null, 2)),
51+
})
52+
// `gulp-cache` relies on this private entry
53+
sourceFile._cachedKey = file._cachedKey
54+
55+
cb(null, sourceFile)
5556
})

build/gulp/plugins/gulp-react-docgen.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path'
33
import * as through2 from 'through2'
44
import * as Vinyl from 'vinyl'
55

6-
import { getComponentInfo, getBufferChecksum, getInfoChecksum } from './util'
6+
import { getComponentInfo } from './util'
77

88
const pluginName = 'gulp-react-docgen'
99

@@ -19,20 +19,16 @@ export default () =>
1919
return
2020
}
2121

22-
const infoFilename = file.basename.replace(/\.tsx$/, '.info.json')
23-
const nextChecksum = getBufferChecksum(file.contents)
24-
25-
if (getInfoChecksum(infoFilename) === nextChecksum) {
26-
cb(null)
27-
return
28-
}
29-
3022
try {
31-
const contents = getComponentInfo(file.path, nextChecksum)
23+
const infoFilename = file.basename.replace(/\.tsx$/, '.info.json')
24+
const contents = getComponentInfo(file.path)
25+
3226
const infoFile = new Vinyl({
3327
path: `./${infoFilename}`,
3428
contents: Buffer.from(JSON.stringify(contents, null, 2)),
3529
})
30+
// `gulp-cache` relies on this private entry
31+
infoFile._cachedKey = file._cachedKey
3632

3733
cb(null, infoFile)
3834
} catch (err) {

build/gulp/plugins/util/checksumUtils.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

build/gulp/plugins/util/getComponentInfo.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface BehaviorInfo {
1212
category: string
1313
}
1414

15-
const getComponentInfo = (filepath: string, checksum?: string) => {
15+
const getComponentInfo = (filepath: string) => {
1616
const absPath = path.resolve(process.cwd(), filepath)
1717

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

46-
// add checksum
47-
info.checksum = checksum
48-
4946
// add exported Component info
5047
const Component = require(absPath).default
5148
info.constructorName = _.get(Component, 'prototype.constructor.name', null)

build/gulp/plugins/util/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './checksumUtils'
21
export { default as getComponentInfo } from './getComponentInfo'
32
export { default as getRelativePathToSourceFile } from './getRelativePathToSourceFile'
43
export { default as parseDefaultValue } from './parseDefaultValue'

build/gulp/tasks/docs.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as historyApiFallback from 'connect-history-api-fallback'
22
import * as express from 'express'
33
import { task, src, dest, lastRun, parallel, series, watch } from 'gulp'
4+
import * as cache from 'gulp-cache'
45
import * as remember from 'gulp-remember'
56
import * as fs from 'fs'
67
import * as path from 'path'
@@ -33,6 +34,8 @@ const handleWatchUnlink = (group, path) => {
3334
// Clean
3435
// ----------------------------------------
3536

37+
task('clean:cache', () => cache.clearAll())
38+
3639
task('clean:docs:component-menu', cb => {
3740
rimraf(paths.docsSrc('componentMenu.json'), cb)
3841
})
@@ -83,7 +86,11 @@ const markdownSrc = [
8386

8487
task('build:docs:component-info', () =>
8588
src(componentsSrc, { since: lastRun('build:docs:component-info') })
86-
.pipe(gulpReactDocgen())
89+
.pipe(
90+
cache(gulpReactDocgen(), {
91+
name: 'componentInfo',
92+
}),
93+
)
8794
.pipe(dest(paths.docsSrc('componentInfo'))),
8895
)
8996

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

110117
task('build:docs:example-sources', () =>
111118
src(examplesSrc, { since: lastRun('build:docs:example-sources') })
112-
.pipe(gulpExampleSource())
119+
.pipe(
120+
cache(gulpExampleSource(), {
121+
name: 'exampleSources',
122+
}),
123+
)
113124
.pipe(dest(paths.docsSrc('exampleSources'))),
114125
)
115126

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"build:docs": "gulp --series dll build:docs",
1717
"build:dist": "gulp --series build:dist",
1818
"ci": "yarn lint && yarn prettier && yarn test --strict",
19+
"clean:cache": "gulp clean:cache",
1920
"predeploy:docs": "cross-env NODE_ENV=production yarn build:docs",
2021
"deploy:docs": "gulp deploy:docs",
2122
"lint": "tslint -t stylish -p .",
@@ -114,6 +115,7 @@
114115
"gh-pages": "^1.0.0",
115116
"glob": "^7.1.2",
116117
"gulp": "^4.0.0",
118+
"gulp-cache": "^1.0.2",
117119
"gulp-debug": "^4.0.0",
118120
"gulp-load-plugins": "^1.5.0",
119121
"gulp-plumber": "^1.2.0",

yarn.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,16 @@ cache-base@^1.0.1:
16401640
union-value "^1.0.0"
16411641
unset-value "^1.0.0"
16421642

1643+
cache-swap@^0.3.0:
1644+
version "0.3.0"
1645+
resolved "https://registry.yarnpkg.com/cache-swap/-/cache-swap-0.3.0.tgz#1c541aa108a50106f630bdd98fe1dec8ba133f51"
1646+
integrity sha1-HFQaoQilAQb2ML3Zj+HeyLoTP1E=
1647+
dependencies:
1648+
graceful-fs "^4.1.2"
1649+
mkdirp "^0.5.1"
1650+
object-assign "^4.0.1"
1651+
rimraf "^2.4.0"
1652+
16431653
callsites@^2.0.0:
16441654
version "2.0.0"
16451655
resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50"
@@ -4312,6 +4322,18 @@ gud@^1.0.0:
43124322
resolved "https://registry.yarnpkg.com/gud/-/gud-1.0.0.tgz#a489581b17e6a70beca9abe3ae57de7a499852c0"
43134323
integrity sha512-zGEOVKFM5sVPPrYs7J5/hYEw2Pof8KCyOwyhG8sAF26mCAeUFAcYPu1mwB7hhpIP29zOIBaDqwuHdLp0jvZXjw==
43144324

4325+
gulp-cache@^1.0.2:
4326+
version "1.0.2"
4327+
resolved "https://registry.yarnpkg.com/gulp-cache/-/gulp-cache-1.0.2.tgz#064ed713de47b0ff26b491abd899590891e426f4"
4328+
integrity sha512-SFtWmWYARVzbzb6UiRZhZxpGo9oyXCOuXdTa0ML8CWQdFRTYZCfddn/grXG6SzdiY/D4rOnqpDJ8R2Trv0SQRQ==
4329+
dependencies:
4330+
babel-runtime "^6.26.0"
4331+
cache-swap "^0.3.0"
4332+
object.pick "^1.3.0"
4333+
plugin-error "^0.1.2"
4334+
through2 "^2.0.3"
4335+
vinyl "^2.1.0"
4336+
43154337
gulp-cli@^2.0.0:
43164338
version "2.0.1"
43174339
resolved "https://registry.yarnpkg.com/gulp-cli/-/gulp-cli-2.0.1.tgz#7847e220cb3662f2be8a6d572bf14e17be5a994b"
@@ -9007,7 +9029,7 @@ right-align@^0.1.1:
90079029
dependencies:
90089030
align-text "^0.1.1"
90099031

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

0 commit comments

Comments
 (0)