| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const webpack = require('webpack'); | 
					
						
							| 
									
										
										
										
											2022-03-25 10:04:46 +01:00
										 |  |  | const { isObject } = require('lodash'); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | // eslint-disable-next-line import/no-extraneous-dependencies
 | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  | const SpeedMeasurePlugin = require('speed-measure-webpack-plugin'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | const webpackConfig = require('../webpack.config'); | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  | const getPluginsPath = require('../utils/get-plugins-path'); | 
					
						
							| 
									
										
										
										
											2021-11-18 12:59:05 +01:00
										 |  |  | const { | 
					
						
							|  |  |  |   getCorePluginsPath, | 
					
						
							|  |  |  |   getPluginToInstallPath, | 
					
						
							|  |  |  |   createPluginsFile, | 
					
						
							|  |  |  | } = require('./create-plugins-file'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:33:31 +01:00
										 |  |  | const PLUGINS_TO_INSTALL = ['i18n', 'users-permissions']; | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  | // Wrapper that outputs the webpack speed
 | 
					
						
							|  |  |  | const smp = new SpeedMeasurePlugin(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | const buildAdmin = async () => { | 
					
						
							|  |  |  |   const entry = path.join(__dirname, '..', 'admin', 'src'); | 
					
						
							|  |  |  |   const dest = path.join(__dirname, '..', 'build'); | 
					
						
							| 
									
										
										
										
											2022-04-14 10:59:42 +02:00
										 |  |  |   const tsConfigFilePath = path.join(__dirname, '..', 'admin', 'src', 'tsconfig.json'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 12:59:05 +01:00
										 |  |  |   const corePlugins = getCorePluginsPath(); | 
					
						
							|  |  |  |   const plugins = getPluginToInstallPath(PLUGINS_TO_INSTALL); | 
					
						
							|  |  |  |   const allPlugins = { ...corePlugins, ...plugins }; | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  |   const pluginsPath = getPluginsPath(); | 
					
						
							| 
									
										
										
										
											2021-11-18 12:59:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await createPluginsFile(allPlugins); | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const args = { | 
					
						
							|  |  |  |     entry, | 
					
						
							|  |  |  |     dest, | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  |     cacheDir: path.join(__dirname, '..'), | 
					
						
							|  |  |  |     pluginsPath, | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  |     env: 'production', | 
					
						
							|  |  |  |     optimize: true, | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       backend: 'http://localhost:1337', | 
					
						
							|  |  |  |       adminPath: '/admin/', | 
					
						
							| 
									
										
										
										
											2022-07-27 23:32:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /** | 
					
						
							|  |  |  |        * Ideally this would take more scenarios into account, such | 
					
						
							|  |  |  |        * as the `telemetryDisabled` property in the package.json | 
					
						
							|  |  |  |        * of the users project. For builds based on an app we are | 
					
						
							|  |  |  |        * passing this information throgh, but here we do not have access | 
					
						
							|  |  |  |        * to the app's package.json. By using at least an environment variable | 
					
						
							|  |  |  |        * we can make sure developers can actually test this functionality. | 
					
						
							|  |  |  |        */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       telemetryDisabled: process.env.STRAPI_TELEMETRY_DISABLED === 'true' ?? false, | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-04-14 10:59:42 +02:00
										 |  |  |     tsConfigFilePath, | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  |   const config = | 
					
						
							| 
									
										
										
										
											2022-06-07 16:07:39 +02:00
										 |  |  |     process.env.MEASURE_BUILD_SPEED === 'true' | 
					
						
							|  |  |  |       ? smp.wrap(webpackConfig(args)) | 
					
						
							|  |  |  |       : webpackConfig(args); | 
					
						
							| 
									
										
										
										
											2022-03-18 16:13:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const compiler = webpack(config); | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   console.log('Building the admin panel'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |     compiler.run((err, stats) => { | 
					
						
							|  |  |  |       let messages; | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         if (!err.message) { | 
					
						
							|  |  |  |           return reject(err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         messages = { | 
					
						
							|  |  |  |           errors: [err.message], | 
					
						
							|  |  |  |           warnings: [], | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         messages = stats.toJson({ all: false, warnings: true, errors: true }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (messages.errors.length) { | 
					
						
							|  |  |  |         // Only keep the first error. Others are often indicative
 | 
					
						
							|  |  |  |         // of the same problem, but confuse the reader with noise.
 | 
					
						
							|  |  |  |         if (messages.errors.length > 1) { | 
					
						
							|  |  |  |           messages.errors.length = 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-25 10:04:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return reject( | 
					
						
							|  |  |  |           new Error( | 
					
						
							|  |  |  |             messages.errors.reduce((acc, error) => { | 
					
						
							|  |  |  |               if (isObject(error)) { | 
					
						
							| 
									
										
										
										
											2022-09-05 16:15:21 +02:00
										 |  |  |                 return acc + error.message; | 
					
						
							| 
									
										
										
										
											2022-03-25 10:04:46 +01:00
										 |  |  |               } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 16:15:21 +02:00
										 |  |  |               return acc + error.join('\n\n'); | 
					
						
							| 
									
										
										
										
											2022-03-25 10:04:46 +01:00
										 |  |  |             }, '') | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return resolve({ | 
					
						
							|  |  |  |         stats, | 
					
						
							|  |  |  |         warnings: messages.warnings, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | buildAdmin() | 
					
						
							|  |  |  |   .then(() => { | 
					
						
							|  |  |  |     process.exit(); | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |   .catch((err) => { | 
					
						
							| 
									
										
										
										
											2021-06-28 09:51:41 +02:00
										 |  |  |     console.error(err); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   }); |