File size: 1,395 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * sections-helper
 *
 * In days past, the preloader was part of sections.js. To preload a module you would import sections
 * and call preload directly. However, all of the require.ensure calls live in sections.js. This makes
 * webpack think that imported sections was also dependant on every other chunk. The cyclic dependencies
 * ballooned compile times and made module analysis very difficult.
 *
 * To break the dependency cycle, we introduced `sections-helper` which does not import sections.js
 */

import { find } from 'lodash';

let sections = null;
export function receiveSections( s ) {
	sections = s;
}

export function getSections() {
	if ( ! sections ) {
		throw new Error( 'sections-helper has not been initialized yet' );
	}
	return sections;
}

export function preload( sectionName ) {
	const section = find( sections, { name: sectionName } );

	if ( section ) {
		section.load();
	}
}

export function load( sectionName, moduleName ) {
	const section = find( sections, { name: sectionName, module: moduleName } );

	if ( ! section ) {
		return Promise.reject(
			`Attempting to load non-existent section: ${ sectionName } (module=${ moduleName })`
		);
	}

	// section.load() loads the module synchronously (using require()) in environments without
	// code splitting. The return value must be explicitly resolved to Promise.
	return Promise.resolve( section.load() );
}