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

fixing async loader for v3 #952

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 25 additions & 30 deletions packages/async-loader/async.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import { h, Component } from 'preact';

export default function(req) {
function Async() {
Component.call(this);

let b, old;
this.componentWillMount = () => {
b = this.base = this.nextBase || this.__b; // short circuits 1st render
req(m => {
this.setState({ child: m.default || m });
});
};
const PENDING = {};

this.shouldComponentUpdate = (_, nxt) => {
nxt = nxt.child === void 0;
if (nxt && old === void 0 && b) {
// Node.TEXT_NODE
if (b.nodeType === 3) {
old = b.data;
} else {
old = h(b.nodeName, {
dangerouslySetInnerHTML: { __html: b.innerHTML },
});
}
} else {
old = ''; // dump it
}
return !nxt;
};
function Pending() {
throw PENDING;
}

this.render = (p, s) => (s.child ? h(s.child, p) : old);
export default function async(load) {
let component;
function AsyncComponent() {
Component.call(this);
if (!component) {
(this.componentWillMount = () => {
load(mod => {
component = (mod && mod.default) || mod;
this.componentDidCatch = null;
this.setState({});
});
}),
(this.componentDidCatch = err => {
if (err !== PENDING) throw err;
});
this.shouldComponentUpdate = () => component != null;
}
this.render = props => h(component || Pending, props);
}
(Async.prototype = new Component()).constructor = Async;
return Async;
AsyncComponent.preload = load;
(AsyncComponent.prototype = new Component()).constructor = AsyncComponent;
return AsyncComponent;
}
7 changes: 6 additions & 1 deletion packages/async-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const { getOptions, stringifyRequest } = require('loader-utils');

exports.pitch = function(req) {
Expand All @@ -16,8 +17,12 @@ exports.pitch = function(req) {
name = query.formatName(this.resourcePath);
}

// import Async from '${path.relative(process.cwd(), path.resolve(__dirname, 'async-component.js'))}';
return `
import Async from '@preact/async-loader/async';
import Async from ${stringifyRequest(
this,
path.resolve(__dirname, 'async.js')
)};

function load(cb) {
require.ensure([], function (require) {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/lib/lib/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global __webpack_public_path__ */

import { h, render } from 'preact';
import { h, render, hydrate } from 'preact';

const interopDefault = m => (m && m.default ? m.default : m);

Expand Down Expand Up @@ -48,7 +48,11 @@ if (typeof app === 'function') {
* to send other data like at some point in time.
*/
const CLI_DATA = { preRenderData };
root = render(h(app, { CLI_DATA }), document.body, root);
const doRender =
process.env.NODE_ENV !== 'production' || root.tagName !== 'script'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if we need to recommend folks use id="app" here. We could have L35 do:

let root = document.getElementById('preact_root') || document.body.firstElementChild;

Thoughts? It would help us avoid browser extensions potentially changing the value of firstElementChild on us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! I really really want to do this. This stream lines everything for us

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally agree with it. :) Always wanted to do this one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

? render
: hydrate;
root = doRender(h(app, { CLI_DATA }), document.body, root);
};

if (module.hot) module.hot.accept('preact-cli-entrypoint', init);
Expand Down