Debian Codegraph @

extensions/VisualEditor/lib/ve/build/tasks/buildloader.js

raw symbols SCIP
1 /*!
2 * Build a static loader file from a template
3 */
4
5 'use strict';
6
7 module.exports = function ( grunt ) {
8
9 grunt.registerMultiTask( 'buildloader', function () {
10 const styles = [],
11 scripts = [],
12 loadedModules = [],
13 targetFile = this.data.targetFile,
14 pathPrefix = this.data.pathPrefix || '',
15 i18n = this.data.i18n || [],
16 demoPages = this.data.demoPages,
17 indent = this.data.indent || '',
18 modules = this.data.modules,
19 load = this.data.load,
20 run = this.data.run || [],
21 env = this.data.env || {},
22 placeholders = this.data.placeholders || {},
23 dir = this.data.dir,
24 langList = this.data.langList !== undefined ? this.data.langList : true,
25 done = this.async(),
26 moduleUtils = require( '../moduleUtils' ),
27 stringifyObject = require( 'stringify-object' );
28 let text = grunt.file.read( this.data.template );
29
30 /**
31 * Build a script tag
32 *
33 * @param {string} src The script URL
34 * @return {string} Script tag, indented as appropriate
35 */
36 function scriptTag( src ) {
37 return indent + '<script src="' + pathPrefix + src.file + '"></script>';
38 }
39
40 /**
41 * Build a stylesheet link tag
42 *
43 * @param {string} [group] Group for the stylesheet
44 * @param {string} src The stylesheet URL
45 * @return {string} Stylesheet link tag, indented as appropriate
46 */
47 function styleTag( group, src ) {
48 const rtlFilepath = src.file.replace( /\.css$/, '.rtl.css' );
49
50 const rel = src.file.endsWith( '.less' ) ? 'stylesheet/less' : 'stylesheet';
51
52 if ( rtlFilepath !== src.file && grunt.file.exists( rtlFilepath ) ) {
53 if ( !dir ) {
54 return indent + '<link rel="' + rel + '" href="' + pathPrefix + src.file + '" class="stylesheet-ltr' +
55 ( group ? ' stylesheet-' + group : '' ) + '">\n' +
56 indent + '<link rel="' + rel + '" href="' + pathPrefix + rtlFilepath + '" class="stylesheet-rtl' +
57 ( group ? ' stylesheet-' + group : '' ) + '">';
58 } else if ( dir === 'rtl' ) {
59 return indent + '<link rel="' + rel + '" href="' + pathPrefix + rtlFilepath + '"' +
60 ( group ? ' class="stylesheet-' + group + '"' : '' ) + '>';
61 }
62 }
63 return indent + '<link rel="' + rel + '" href="' + pathPrefix + src.file + '"' +
64 ( group ? ' class="stylesheet-' + group + '"' : '' ) + '>';
65 }
66
67 /**
68 * Expand string file path into a file description object { file: src }
69 *
70 * Do nothing if already expanded.
71 *
72 * @param {string|Object} src File path, or file description object
73 * @return {Object} File description object
74 */
75 function expand( src ) {
76 return typeof src === 'string' ? { file: src } : src;
77 }
78
79 /**
80 * Filter out debug files, depending on env.test / env.debug
81 *
82 * @param {string} [type] The type of the file
83 * @param {Object} src File description object
84 * @return {boolean} Whether to include the file
85 */
86 function filter( type, src ) {
87 if ( src.debug && !env.debug ) {
88 return false;
89 }
90 if ( type === 'styles' && env.test && !src.test ) {
91 return false;
92 }
93
94 return true;
95 }
96
97 /**
98 * Substitute placeholder text
99 *
100 * @param {string} input The input text, maybe containing placeholders
101 * @param {string} id The id to replace
102 * @param {string|Function} replacement Replacement string, or function returning one
103 * @param {Function} callback Will be called with the substituted output
104 */
105 function placeholder( input, id, replacement, callback ) {
106 // eslint-disable-next-line security/detect-non-literal-regexp
107 const rComment = new RegExp( '<!-- ' + id + ' -->', 'm' );
108 if ( typeof replacement === 'function' ) {
109 replacement( ( response ) => {
110 const output = input.replace( rComment, response );
111 callback( output );
112 } );
113 } else {
114 const output = input.replace( rComment, replacement );
115 callback( output );
116 }
117 }
118
119 /**
120 * Push modules into loadedModules
121 *
122 * @param {string[]} l Modules specified in the buildLoader config (see Gruntfile.js)
123 */
124 function addModules( l ) {
125 const dependencies = moduleUtils.buildDependencyList( modules, l );
126 for ( const dependency in dependencies ) {
127 const module = dependencies[ dependency ];
128 if ( loadedModules.includes( module ) ) {
129 continue;
130 }
131 loadedModules.push( module );
132 if ( modules[ module ].scripts ) {
133 const moduleScripts = modules[ module ].scripts
134 .map( expand ).filter( filter.bind( this, 'scripts' ) ).map( scriptTag )
135 .join( '\n' );
136 if ( moduleScripts ) {
137 scripts.push( indent + '<!-- ' + module + ' -->\n' + moduleScripts );
138 }
139 }
140 if ( modules[ module ].styles ) {
141 const moduleStyles = modules[ module ].styles
142 .map( expand ).filter( filter.bind( this, 'styles' ) ).map( styleTag.bind( styleTag, modules[ module ].styleGroup ) )
143 .join( '\n' );
144 if ( moduleStyles ) {
145 styles.push( indent + '<!-- ' + module + ' -->\n' + moduleStyles );
146 }
147 }
148 }
149 }
150
151 addModules( load );
152
153 if ( i18n.length || demoPages ) {
154 let configScript = indent + '<script>\n';
155
156 if ( i18n.length ) {
157 configScript +=
158 indent + '\tve.messagePaths = ' +
159 stringifyObject(
160 i18n.map( ( path ) => pathPrefix + path )
161 ).replace( /\n/g, '\n\t' + indent ) + ';\n';
162
163 if ( langList ) {
164 configScript += indent + '\tve.availableLanguages = ' +
165 stringifyObject(
166 Array.from( new Set(
167 grunt.file.expand(
168 i18n.map( ( path ) => path + '*.json' )
169 ).map( ( file ) => file.split( '/' ).pop().slice( 0, -5 ) )
170 ) ).sort()
171 ).replace( /\n\t*/g, ' ' ) +
172 ';\n';
173 }
174 }
175 if ( demoPages ) {
176 configScript += indent + '\tve.demoPages = ' + stringifyObject( demoPages ).replace( /\n/g, '\n\t' + indent ) + ';\n';
177 }
178
179 configScript +=
180 indent + '</script>';
181 scripts.push( configScript );
182 }
183
184 addModules( run );
185
186 placeholders.styles = styles.join( '\n\n' );
187 placeholders.scripts = scripts.join( '\n\n' );
188 placeholders.dir = dir;
189
190 grunt.util.async.forEachSeries(
191 Object.keys( placeholders ),
192 ( id, next ) => {
193 placeholder( text, id.toUpperCase(), placeholders[ id ], ( newText ) => {
194 text = newText;
195 next();
196 } );
197 },
198 () => {
199 grunt.file.write( targetFile, text );
200 grunt.log.ok( 'File "' + targetFile + '" written.' );
201
202 done();
203 }
204 );
205
206 } );
207
208 };