Debian Codegraph @

extensions/VisualEditor/lib/ve/build/moduleUtils.js

raw symbols SCIP
1 /*!
2 * Utility methods for interpreting the modules.json manifest.
3 *
4 * Code shared with the OOjs UI project
5 */
6
7 'use strict';
8
9 const self = module.exports = {
10 /**
11 * Expand an array of file paths and variant-objects into
12 * a flattened list by variant.
13 *
14 * input = [
15 * 'foo.js',
16 * 'bar.js',
17 * { default: 'baz-fallback.js', svg: 'baz-svg.js', }.
18 * 'quux.js'
19 * ]
20 *
21 * output = {
22 * default: [
23 * 'foo.js',
24 * 'bar.js'
25 * 'baz-fallback.js'
26 * 'quux.js'
27 * ],
28 * svg: [
29 * 'foo.js',
30 * 'bar.js'
31 * 'baz-svg.js'
32 * 'quux.js'
33 * ]
34 * ]
35 *
36 * @param {Array} resources List of expandable resources
37 * @return {Object.<string,string[]>}
38 */
39 expandResources: function ( resources ) {
40 // Figure out what the different css targets will be,
41 // we need this to be shared between the recess task
42 // (which will compile the less code) and the concat task
43 // (which will prepend intro.css without it being stripped
44 // like recess would).
45 const targets = { default: [] };
46 resources.forEach( ( filepath ) => {
47 if ( typeof filepath !== 'object' ) {
48 filepath = { default: filepath };
49 }
50 // Fetch copy of buffer before filepath/variant loop, otherwise
51 // it can incorrectly include the default file in a non-default variant.
52 const buffer = targets.default.slice();
53 for ( const variant in filepath ) {
54 if ( !targets[ variant ] ) {
55 targets[ variant ] = buffer.slice();
56 }
57 targets[ variant ].push( filepath[ variant ] );
58 }
59 } );
60 return targets;
61 },
62
63 /**
64 * Create a build list
65 *
66 * @param {Array} modules List of modules and their dependencies
67 * @param {string[]} targets List of target modules to load including any dependencies
68 * @return {Object} An object containing arrays of the scripts and styles
69 */
70 makeBuildList: function ( modules, targets ) {
71 /**
72 * Given a list of modules and targets, returns an object splitting the scripts
73 * and styles.
74 *
75 * @param {string[]} buildlist List of targets to work through
76 * @param {Object|null} filelist Object to extend
77 * @return {Object} Object of two arrays listing the file paths
78 */
79 function expandBuildList( buildlist, filelist ) {
80 filelist = filelist || {};
81 filelist.scripts = filelist.scripts || [];
82 filelist.styles = filelist.styles || [];
83
84 for ( const build in buildlist ) {
85 const moduleName = buildlist[ build ];
86
87 for ( const script in modules[ moduleName ].scripts ) {
88 if ( !modules[ moduleName ].scripts[ script ].debug ) {
89 filelist.scripts.push( modules[ moduleName ].scripts[ script ] );
90 }
91 }
92
93 for ( const style in modules[ moduleName ].styles ) {
94 if ( !modules[ moduleName ].styles[ style ].debug ) {
95 filelist.styles.push( modules[ moduleName ].styles[ style ] );
96 }
97 }
98 }
99 return filelist;
100 }
101
102 return expandBuildList( self.buildDependencyList( modules, targets ) );
103 },
104
105 /**
106 * Expands an array of arrays of file paths with dependencies into an ordered
107 * lit of dependencies stemming from one or more given top-level modules.
108 *
109 * @param {Array} modules List of modules and their dependencies
110 * @param {string[]} load List of targets to return and their dependencies
111 * @param {string[]|null} list Extant flat list of file paths to extend
112 * @return {string[]} Flat list of file paths
113 * @throws {Error} Dependency not found
114 */
115 buildDependencyList: function ( modules, load, list ) {
116 list = list || [];
117
118 for ( let i = 0; i < load.length; i++ ) {
119 const module = load[ i ];
120
121 if ( !Object.prototype.hasOwnProperty.call( modules, module ) ) {
122 throw new Error( 'Dependency ' + module + ' not found' );
123 }
124
125 // Add in any dependencies
126 if ( modules[ module ].dependencies ) {
127 self.buildDependencyList( modules, modules[ module ].dependencies, list );
128 }
129
130 // Append target load module to the end of the current list
131 if ( !list.includes( module ) ) {
132 list.push( module );
133 }
134 }
135
136 return list;
137 }
138 };