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

Allow webpack cache isReady only for initial chunks #568

Merged
merged 2 commits into from
Jun 7, 2020
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
36 changes: 18 additions & 18 deletions packages/babel-plugin/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`plugin Magic comment should remove only needed comments 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ exports[`plugin Magic comment should transpile arrow functions 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -125,7 +125,7 @@ exports[`plugin Magic comment should transpile function expression 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ exports[`plugin Magic comment should transpile shortand properties 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -245,7 +245,7 @@ exports[`plugin aggressive import should work with destructuration 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -305,7 +305,7 @@ exports[`plugin aggressive import with "webpackChunkName" should replace it 1`]
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -361,7 +361,7 @@ exports[`plugin aggressive import without "webpackChunkName" should support comp
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -419,7 +419,7 @@ exports[`plugin aggressive import without "webpackChunkName" should support dest
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -479,7 +479,7 @@ exports[`plugin aggressive import without "webpackChunkName" should support simp
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -535,7 +535,7 @@ exports[`plugin loadable.lib should be transpiled too 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -591,7 +591,7 @@ exports[`plugin simple import in a complex promise should work 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -647,7 +647,7 @@ exports[`plugin simple import should transform path into "chunk-friendly" name 1
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -703,7 +703,7 @@ exports[`plugin simple import should work with * in name 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -759,7 +759,7 @@ exports[`plugin simple import should work with + concatenation 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -815,7 +815,7 @@ exports[`plugin simple import should work with template literal 1`] = `
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -871,7 +871,7 @@ exports[`plugin simple import with "webpackChunkName" comment should use it 1`]
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -927,7 +927,7 @@ exports[`plugin simple import with "webpackChunkName" comment should use it even
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down Expand Up @@ -983,7 +983,7 @@ exports[`plugin simple import without "webpackChunkName" comment should add it 1
isReady(props) {
const key = this.resolve(props);

if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin/src/properties/isReady.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function isReadyProperty({ types: t, template }) {
const statements = template.ast(`
const key=this.resolve(props)
if (this.resolved[key] === false) {
if (this.resolved[key] !== true) {
return false
}

Expand Down
21 changes: 21 additions & 0 deletions packages/component/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"dist/loadable.cjs.js": {
"bundled": 12852,
"minified": 6137,
"gzipped": 2208
},
"dist/loadable.esm.js": {
"bundled": 12469,
"minified": 5828,
"gzipped": 2141,
"treeshaked": {
"rollup": {
"code": 259,
"import_statements": 259
},
"webpack": {
"code": 5061
}
}
}
}
9 changes: 8 additions & 1 deletion packages/component/src/createLoadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react'
import { invariant } from './util'
import Context from './Context'
import {LOADABLE_SHARED} from "./shared";

function resolveConstructor(ctor) {
if (typeof ctor === 'function') {
Expand Down Expand Up @@ -82,7 +83,13 @@ function createLoadable({ resolve = identity, render, onLoad }) {
// If module is already loaded, we use a synchronous loading
// Only perform this synchronous loading if the component has not
// been marked with no SSR, else we risk hydration mismatches
if (options.ssr !== false && ctor.isReady && ctor.isReady(props)) {
if (options.ssr !== false && (
// is ready - was loaded in this session
(ctor.isReady && ctor.isReady(props)) ||
// is ready - was loaded during SSR process
(ctor.chunkName && LOADABLE_SHARED.initialChunks[ctor.chunkName(props)])
)
) {
this.loadSync()
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/component/src/loadableReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-env browser */
import { warn } from './util'
import { getRequiredChunkKey } from './sharedInternals'
import {LOADABLE_SHARED} from "./shared";

const BROWSER = typeof window !== 'undefined'

Expand All @@ -19,7 +20,10 @@ export default function loadableReady(
if (BROWSER) {
const dataElement = document.getElementById(getRequiredChunkKey(namespace))
if (dataElement) {
requiredChunks = JSON.parse(dataElement.textContent)
requiredChunks = JSON.parse(dataElement.textContent);
requiredChunks.forEach(chunk => {
LOADABLE_SHARED.initialChunks[chunk] = true;
});
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/component/src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const LOADABLE_SHARED = {
initialChunks: {}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6531,6 +6531,13 @@ make-dir@^2.0.0, make-dir@^2.1.0:
pify "^4.0.1"
semver "^5.6.0"

make-dir@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
integrity sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==
dependencies:
semver "^6.0.0"

make-fetch-happen@^5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/make-fetch-happen/-/make-fetch-happen-5.0.2.tgz#aa8387104f2687edca01c8687ee45013d02d19bd"
Expand Down