Skip to content

Commit 7c16efb

Browse files
kangweichanNoviny
authored andcommitted
Load files again upon deploy if specific props have changed (#46)
* Load files again upon deploy if specific props have changed * Remove setState from componentDidUpdate and replace with class variable * Compare props using lodash.isEqual and resolve promises first where necessary * Replace return statements with else statements in componentDidUpdate
1 parent 0b60604 commit 7c16efb

File tree

6 files changed

+66
-18
lines changed

6 files changed

+66
-18
lines changed

.changeset/af5d2211/changes.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"releases": [{ "name": "react-codesandboxer", "type": "patch" }],
3+
"dependents": []
4+
}

.changeset/af5d2211/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Load files again upon deploy if specific props have been changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
"form-data": "^2.3.2",
6464
"html-webpack-plugin": "^3.2.0",
6565
"isomorphic-unfetch": "^2.0.0",
66+
"lodash.isequal": "^4.5.0",
67+
"lodash.pick": "^4.4.0",
6668
"lz-string": "^1.4.4",
6769
"meow": "^5.0.0",
6870
"mini-css-extract-plugin": "^0.4.0",

packages/react-codesandboxer/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"license": "MIT",
88
"dependencies": {
99
"codesandboxer": "^0.7.1",
10+
"lodash.isequal": "^4.5.0",
11+
"lodash.pick": "^4.4.0",
1012
"react-node-resolver": "^1.0.1"
1113
},
1214
"scripts": {

packages/react-codesandboxer/src/CodeSandboxer.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @flow
22
import React, { Component, type Node } from 'react';
33
import NodeResolver from 'react-node-resolver';
4+
import isEqual from 'lodash.isequal';
5+
import pick from 'lodash.pick';
46

57
import {
68
fetchFiles,
@@ -96,6 +98,8 @@ export default class CodeSandboxDeployer extends Component<Props, State> {
9698
style: { display: 'inline-block' },
9799
};
98100

101+
shouldReload = false
102+
99103
loadFiles = () => {
100104
let { onLoadComplete, providedFiles, dependencies, name } = this.props;
101105

@@ -181,13 +185,37 @@ export default class CodeSandboxDeployer extends Component<Props, State> {
181185
if (isDeploying) return null;
182186
this.setState({ isDeploying: true });
183187

184-
if (deployPromise) {
188+
if (!this.shouldReload && deployPromise) {
185189
deployPromise.then(this.deploy);
186190
} else {
191+
this.shouldReload = false;
187192
this.loadFiles().then(this.deploy);
188193
}
189194
};
190195

196+
componentDidUpdate(prevProps: Props) {
197+
/* If props related to loading files have been changed, next deploy should reload files */
198+
/* The props that are compared should be the same as the arguments of fetchFiles */
199+
const compareKeys = ['examplePath', 'gitInfo', 'importReplacements',
200+
'dependencies', 'providedFiles', 'name', 'extensions', 'template'];
201+
if (!isEqual(pick(this.props, compareKeys), pick(prevProps, compareKeys))) {
202+
this.shouldReload = true;
203+
} else {
204+
/* pkgJSON and example also need to be compared, but may be promises, which must be resolved before they can be compared */
205+
Promise.all([this.props.example, prevProps.example]).then(([example, prevExample]) => {
206+
if (example !== prevExample) {
207+
this.shouldReload = true;
208+
} else {
209+
Promise.all([this.props.pkgJSON, prevProps.pkgJSON]).then(([pkgJSON, prevPkgJSON]) => {
210+
if (!isEqual(pkgJSON, prevPkgJSON)) {
211+
this.shouldReload = true;
212+
}
213+
});
214+
}
215+
});
216+
}
217+
}
218+
191219
componentDidMount() {
192220
if (this.props.autoDeploy) {
193221
this.deployToCSB();

yarn.lock

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,11 @@ async@^1.4.0, async@^1.5.2:
738738
integrity sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=
739739

740740
async@^2.1.4, async@^2.5.0:
741-
version "2.6.1"
742-
resolved "https://registry.npmjs.org/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610"
743-
integrity sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==
741+
version "2.6.2"
742+
resolved "https://registry.npmjs.org/async/-/async-2.6.2.tgz#18330ea7e6e313887f5d2f2a904bac6fe4dd5381"
743+
integrity sha512-H1qVYh1MYhEEFLsP97cVKqCGo7KfCyTt6uEWqsTBr9SO84oK9Uwbyd/yCW+6rKJLHksBNUVWZDAjfS+Ccx0Bbg==
744744
dependencies:
745-
lodash "^4.17.10"
745+
lodash "^4.17.11"
746746

747747
asynckit@^0.4.0:
748748
version "0.4.0"
@@ -2750,14 +2750,14 @@ debug@2, debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, de
27502750
dependencies:
27512751
ms "2.0.0"
27522752

2753-
debug@3.1.0, debug@=3.1.0:
2753+
debug@3.1.0:
27542754
version "3.1.0"
27552755
resolved "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
27562756
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
27572757
dependencies:
27582758
ms "2.0.0"
27592759

2760-
debug@^3.1.0, debug@^3.2.5:
2760+
debug@^3.1.0, debug@^3.2.5, debug@^3.2.6:
27612761
version "3.2.6"
27622762
resolved "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
27632763
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
@@ -3803,11 +3803,11 @@ flush-write-stream@^1.0.0, flush-write-stream@^1.0.2:
38033803
readable-stream "^2.3.6"
38043804

38053805
follow-redirects@^1.0.0:
3806-
version "1.6.1"
3807-
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.6.1.tgz#514973c44b5757368bad8bddfe52f81f015c94cb"
3808-
integrity sha512-t2JCjbzxQpWvbhts3l6SH1DKzSrx8a+SsaVf4h6bG4kOXUuPYS/kg2Lr4gQSb7eemaHqJkOThF1BGyjlUkO1GQ==
3806+
version "1.7.0"
3807+
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz#489ebc198dc0e7f64167bd23b03c4c19b5784c76"
3808+
integrity sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==
38093809
dependencies:
3810-
debug "=3.1.0"
3810+
debug "^3.2.6"
38113811

38123812
for-in@^1.0.1, for-in@^1.0.2:
38133813
version "1.0.2"
@@ -5766,11 +5766,21 @@ lodash.camelcase@^4.3.0:
57665766
resolved "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
57675767
integrity sha1-soqmKIorn8ZRA1x3EfZathkDMaY=
57685768

5769+
lodash.isequal@^4.5.0:
5770+
version "4.5.0"
5771+
resolved "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
5772+
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
5773+
57695774
lodash.memoize@^4.1.2:
57705775
version "4.1.2"
57715776
resolved "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
57725777
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
57735778

5779+
lodash.pick@^4.4.0:
5780+
version "4.4.0"
5781+
resolved "https://registry.npmjs.org/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
5782+
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=
5783+
57745784
lodash.sortby@^4.7.0:
57755785
version "4.7.0"
57765786
resolved "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -5786,7 +5796,7 @@ lodash@^3.10.1:
57865796
resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
57875797
integrity sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y=
57885798

5789-
lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.3.0:
5799+
lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.3.0:
57905800
version "4.17.11"
57915801
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
57925802
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
@@ -7414,10 +7424,11 @@ prompts@^0.1.9:
74147424
sisteransi "^0.1.1"
74157425

74167426
prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2:
7417-
version "15.7.1"
7418-
resolved "https://registry.npmjs.org/prop-types/-/prop-types-15.7.1.tgz#2fa61e0a699d428b40320127733ee2931f05d9d1"
7419-
integrity sha512-f8Lku2z9kERjOCcnDOPm68EBJAO2K00Q5mSgPAUE/gJuBgsYLbVy6owSrtcHj90zt8PvW+z0qaIIgsIhHOa1Qw==
7427+
version "15.7.2"
7428+
resolved "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
7429+
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
74207430
dependencies:
7431+
loose-envify "^1.4.0"
74217432
object-assign "^4.1.1"
74227433
react-is "^16.8.1"
74237434

@@ -7781,9 +7792,9 @@ readdirp@^2.0.0, readdirp@^2.2.1:
77817792
readable-stream "^2.0.2"
77827793

77837794
realpath-native@^1.0.0:
7784-
version "1.0.2"
7785-
resolved "https://registry.npmjs.org/realpath-native/-/realpath-native-1.0.2.tgz#cd51ce089b513b45cf9b1516c82989b51ccc6560"
7786-
integrity sha512-+S3zTvVt9yTntFrBpm7TQmQ3tzpCrnA1a/y+3cUHAc9ZR6aIjG0WNLR+Rj79QpJktY+VeW/TQtFlQ1bzsehI8g==
7795+
version "1.1.0"
7796+
resolved "https://registry.npmjs.org/realpath-native/-/realpath-native-1.1.0.tgz#2003294fea23fb0672f2476ebe22fcf498a2d65c"
7797+
integrity sha512-wlgPA6cCIIg9gKz0fgAPjnzh4yR/LnXovwuo9hvyGvx3h8nX4+/iLZplfUWasXpqD8BdnGnP5njOFjkUwPzvjA==
77877798
dependencies:
77887799
util.promisify "^1.0.0"
77897800

0 commit comments

Comments
 (0)