GeezerVison is showing github.com — :construction: lazy files · nullstack/nullstack@8c15083
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import LazyComponent from "../shared/lazyComponent" |
| 2 | + |
| 3 | +export default function lazy(_hash, importer) { |
| 4 | + return class extends LazyComponent { |
| 5 | + importer = importer |
| 6 | + } |
| 7 | +} |
Collapse file
Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +module.exports = function (source, map) { |
| 2 | + if (this.resourcePath.includes('Application.njs')) { |
| 3 | + console.log("DEBUG") |
| 4 | + require('fs').writeFileSync('debug.njs', source) |
| 5 | + } |
| 6 | + this.callback(null, source, map) |
| 7 | +} |
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import LazyComponent from "../shared/lazyComponent" |
| 2 | + |
| 3 | +const queue = {} |
| 4 | + |
| 5 | +export default function lazy(hash, importer) { |
| 6 | + queue[hash] = importer |
| 7 | + return class extends LazyComponent { |
| 8 | + importer = importer |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +export async function load(hash) { |
| 13 | + const fileHash = module.hot ? hash.split('___')[0] : hash.slice(0, 8) |
| 14 | + if (!queue[fileHash]) return |
| 15 | + const importer = queue[fileHash] |
| 16 | + delete queue[fileHash] |
| 17 | + return importer() |
| 18 | +} |
Collapse file
Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import element from './element' |
| 2 | + |
| 3 | +class LazyComponent { |
| 4 | + |
| 5 | + constructor(scope) { |
| 6 | + this.server = scope.context.server |
| 7 | + } |
| 8 | + |
| 9 | + async initiate() { |
| 10 | + this.component = (await this.importer()).default |
| 11 | + } |
| 12 | + |
| 13 | + render() { |
| 14 | + if (!this.component) return false |
| 15 | + const { key, ...attributes } = this._attributes |
| 16 | + return element(this.component, attributes) |
| 17 | + } |
| 18 | + |
| 19 | + toJSON() { |
| 20 | + return null |
| 21 | + } |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +export default LazyComponent |