Skip to content

Commit

Permalink
share lazy components dependencies with main bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
bstefanescu committed May 17, 2021
1 parent 40cc3e5 commit 2aa9fcc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
18 changes: 2 additions & 16 deletions core/runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,8 @@ Qute.defineMethod = function(name, fn) {
Qute.runAfter = function(cb) { UpdateQueue.runAfter(cb); }
// get a component from its root element
Qute.get = function(elt) { return elt.__qute__; }
// An object which maps package names to the package exported object that were registered in Qute using Qute.export();
// These are used when importing remote (i.e. lazy) components to check if the import is already available.
Qute.exports = {};
Qute.export = function(name, component) {
if (component === void(0)) {
Object.assign(Qute.exports, name); // name is an object
} else {
Qute.exports[name] = component;
}
}
Qute.import = function(component) {
const script = window.document.currentScript;
if (!script) throw new Error('Qute.import must only be called while a component imported trough a script tag is intiializaing.');
const importName = script.getAttribute('data-import-from') || script.src;
return (Qute.exports[importName] = component);
}
// Qute.import is added by @qutejs/importer

// store Qute instance in window - this is important so that imported components use the same Qute instance
window.Qute = Qute;

Expand Down
34 changes: 33 additions & 1 deletion plugins/importer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Qute from '@qutejs/runtime';
var customResolve = null;
var renderError = null;
var renderPending = null;
const exportMap = {};

function resolvePath(base, path) {
base = base.substring(1); // remove the leading /
Expand Down Expand Up @@ -53,7 +54,6 @@ function resolveScript(nameOrUrl) {
}

function insertScript(url, from, onload, onerror) {
const exportMap = Qute.exports;
if (!from) from = url;
if (from in exportMap) {
onload && onload(exportMap[from]);
Expand Down Expand Up @@ -216,9 +216,41 @@ function LazyComponent(location) {
}
}

// ----------- import API -------------

function registerComponent(component) {
const script = window.document.currentScript;
if (!script) throw new Error('Qute.import must only be called while a component imported trough a script tag is intiializaing.');
const importName = script.getAttribute('data-import-from') || script.src;
return (exportMap[importName] = component);
}

function importComponent(componentOrFactory) {
if (arguments.length > 1) {
serialImport(Array.prototype.slice.call(arguments, 1),
result => {
const Comp = componentOrFactory.apply(null, result);
if (Comp == null) {
throw new Error('A Component factory must return a non null object');
}
registerComponent(Comp);
},
error => {
throw error;
});
} else {
registerComponent(componentOrFactory);
}
}

// we augment the Qute api with an import function and exports property
Qute.import = importComponent;

export {
insertScript, importScript,
serialImport, importAll,
setImporterOptions,
importComponent,
exportMap as exports,
LazyComponent
}

0 comments on commit 2aa9fcc

Please sign in to comment.