| | const { getOptions } = require( 'loader-utils' ); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function addModuleImportToSections( sections, { useRequire, onlyIsomorphic } = {} ) { |
| | sections.forEach( ( section ) => { |
| | if ( onlyIsomorphic && ! section.isomorphic ) { |
| | |
| | |
| | return; |
| | } |
| |
|
| | section.load = useRequire |
| | ? `() => require( '${ section.module }' )` |
| | : `() => import( /* webpackChunkName: '${ section.name }' */ '${ section.module }' )`; |
| | } ); |
| |
|
| | |
| | const sectionStringsWithImportFns = JSON.stringify( sections, null, '\t' ).replace( |
| | /load": "(.*)"/g, |
| | 'load": $1' |
| | ); |
| |
|
| | const sectionsFile = `export default ${ sectionStringsWithImportFns }`; |
| | return sectionsFile; |
| | } |
| |
|
| | function printSectionsAndPaths( sections ) { |
| | let lastSection = null; |
| | const sortedSections = [ ...sections ]; |
| | sortedSections.sort( ( a, b ) => { |
| | return a.name.localeCompare( b.name ); |
| | } ); |
| | for ( const section of sortedSections ) { |
| | if ( lastSection !== section.name ) { |
| | console.log( `\t${ section.name }:` ); |
| | lastSection = section.name; |
| | } |
| | for ( const p of section.paths ) { |
| | console.log( `\t\t${ p }` ); |
| | } |
| | } |
| | } |
| |
|
| | function filterSectionsInDevelopment( sections, { forceAll, activeSections, enableByDefault } ) { |
| | if ( forceAll ) { |
| | return sections; |
| | } |
| |
|
| | return sections.filter( ( section ) => { |
| | if ( activeSections && typeof activeSections[ section.name ] !== 'undefined' ) { |
| | return activeSections[ section.name ]; |
| | } |
| | return enableByDefault; |
| | } ); |
| | } |
| |
|
| | const loader = function () { |
| | const options = getOptions( this ) || {}; |
| | const { onlyIsomorphic, forceAll, activeSections, enableByDefault } = options; |
| | |
| | const useRequire = options.useRequire || options.forceRequire; |
| | let { include } = options; |
| |
|
| | let sections = filterSectionsInDevelopment( require( this.resourcePath ), { |
| | forceAll, |
| | activeSections, |
| | enableByDefault, |
| | } ); |
| |
|
| | if ( include ) { |
| | if ( ! Array.isArray( include ) ) { |
| | include = include.split( ',' ); |
| | } |
| | console.log( `[sections-loader] Limiting build to ${ include.join( ', ' ) } sections` ); |
| | const allSections = sections; |
| | sections = allSections.filter( ( section ) => include.includes( section.name ) ); |
| | if ( ! sections.length ) { |
| | |
| | console.warn( `[sections-loader] No sections matched ${ include.join( ',' ) }` ); |
| | console.warn( `[sections-loader] Available sections are:` ); |
| | printSectionsAndPaths( allSections ); |
| | } |
| | } |
| |
|
| | return addModuleImportToSections( sections, { useRequire, onlyIsomorphic } ); |
| | }; |
| |
|
| | loader.addModuleImportToSections = addModuleImportToSections; |
| |
|
| | module.exports = loader; |
| |
|