2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
2021-09-08 17:25:24 +02:00
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const webpack = require('webpack');
|
2019-09-23 19:27:40 +02:00
|
|
|
const WebpackDevServer = require('webpack-dev-server');
|
2022-04-14 14:55:57 +02:00
|
|
|
const { isUsingTypeScript } = require('@strapi/typescript-utils');
|
2019-09-24 14:10:57 +02:00
|
|
|
const chalk = require('chalk');
|
2022-04-14 10:59:42 +02:00
|
|
|
|
2022-03-09 15:13:51 +01:00
|
|
|
const {
|
|
|
|
createCacheDir,
|
|
|
|
getCustomWebpackConfig,
|
|
|
|
shouldBuildAdmin,
|
|
|
|
watchAdminFiles,
|
|
|
|
} = require('./utils');
|
2020-04-15 12:56:51 +02:00
|
|
|
|
2022-04-14 10:59:42 +02:00
|
|
|
async function build({ appDir, buildDestDir, env, forceBuild, optimize, options, plugins }) {
|
|
|
|
const buildAdmin = await shouldBuildAdmin({ appDir, plugins });
|
|
|
|
|
2022-04-14 14:55:57 +02:00
|
|
|
const useTypeScript = await isUsingTypeScript(
|
2022-04-14 10:59:42 +02:00
|
|
|
path.join(appDir, 'src', 'admin'),
|
|
|
|
'tsconfig.json'
|
|
|
|
);
|
2021-12-20 14:56:06 +01:00
|
|
|
|
2021-12-20 15:51:19 +01:00
|
|
|
if (!buildAdmin && !forceBuild) {
|
2021-11-29 16:13:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:56:30 +02:00
|
|
|
// Create the cache dir containing the front-end files.
|
2022-04-14 10:59:42 +02:00
|
|
|
await createCacheDir({ appDir, plugins });
|
2020-04-15 11:56:30 +02:00
|
|
|
|
2022-03-09 17:18:27 +01:00
|
|
|
const cacheDir = path.resolve(appDir, '.cache');
|
2021-05-05 15:21:53 +02:00
|
|
|
const entry = path.resolve(cacheDir, 'admin', 'src');
|
2022-03-09 17:18:27 +01:00
|
|
|
const dest = path.resolve(buildDestDir, 'build');
|
2021-07-01 14:19:50 +02:00
|
|
|
|
|
|
|
// Roots for the @strapi/babel-plugin-switch-ee-ce
|
|
|
|
const roots = {
|
|
|
|
eeRoot: path.resolve(cacheDir, 'ee', 'admin'),
|
|
|
|
ceRoot: path.resolve(cacheDir, 'admin', 'src'),
|
|
|
|
};
|
|
|
|
|
2022-04-12 16:57:00 +02:00
|
|
|
const pluginsPath = Object.keys(plugins).map(pluginName => plugins[pluginName].pathToPlugin);
|
2021-09-09 10:56:57 +02:00
|
|
|
|
2022-04-14 10:59:42 +02:00
|
|
|
// Either use the tsconfig file from the generated app or the one inside the .cache folder
|
|
|
|
// so we can develop plugins in TS while being in a JS app
|
|
|
|
const tsConfigFilePath = useTypeScript
|
|
|
|
? path.join(appDir, 'src', 'admin', 'tsconfig.json')
|
|
|
|
: path.resolve(entry, 'tsconfig.json');
|
|
|
|
|
2022-03-09 17:18:27 +01:00
|
|
|
const config = getCustomWebpackConfig(appDir, {
|
|
|
|
appDir,
|
2021-09-09 10:56:57 +02:00
|
|
|
cacheDir,
|
|
|
|
dest,
|
2022-03-09 17:18:27 +01:00
|
|
|
entry,
|
2021-09-09 10:56:57 +02:00
|
|
|
env,
|
|
|
|
optimize,
|
2022-03-09 17:18:27 +01:00
|
|
|
options,
|
|
|
|
pluginsPath,
|
2021-09-09 10:56:57 +02:00
|
|
|
roots,
|
2022-04-14 10:59:42 +02:00
|
|
|
tsConfigFilePath,
|
2021-09-09 10:56:57 +02:00
|
|
|
});
|
2020-04-15 11:56:30 +02:00
|
|
|
|
|
|
|
const compiler = webpack(config);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) {
|
2021-07-01 14:19:50 +02:00
|
|
|
console.error(err.stack || err);
|
|
|
|
|
|
|
|
if (err.details) {
|
|
|
|
console.error(err.details);
|
2020-04-15 11:56:30 +02:00
|
|
|
}
|
2021-07-01 14:19:50 +02:00
|
|
|
return reject(err);
|
2020-04-15 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 14:19:50 +02:00
|
|
|
const info = stats.toJson();
|
|
|
|
|
|
|
|
if (stats.hasErrors()) {
|
|
|
|
console.error(info.errors);
|
2020-04-15 11:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolve({
|
|
|
|
stats,
|
2021-07-01 14:19:50 +02:00
|
|
|
|
|
|
|
warnings: info.warnings,
|
2020-04-15 11:56:30 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-09 17:18:27 +01:00
|
|
|
async function clean({ appDir, buildDestDir }) {
|
|
|
|
// FIXME rename admin build dir and path to build dir
|
|
|
|
const buildDir = path.join(buildDestDir, 'build');
|
|
|
|
// .cache dir is always located at the root of the app
|
|
|
|
const cacheDir = path.join(appDir, '.cache');
|
2020-04-14 15:24:14 +02:00
|
|
|
|
|
|
|
fs.removeSync(buildDir);
|
|
|
|
fs.removeSync(cacheDir);
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:59:42 +02:00
|
|
|
async function watchAdmin({ appDir, browser, buildDestDir, host, options, plugins, port }) {
|
2022-04-14 14:55:57 +02:00
|
|
|
const useTypeScript = await isUsingTypeScript(
|
2022-04-14 10:59:42 +02:00
|
|
|
path.join(appDir, 'src', 'admin'),
|
|
|
|
'tsconfig.json'
|
|
|
|
);
|
2019-09-26 11:52:02 +02:00
|
|
|
// Create the cache dir containing the front-end files.
|
2022-03-09 18:26:17 +01:00
|
|
|
const cacheDir = path.join(appDir, '.cache');
|
2022-04-14 10:59:42 +02:00
|
|
|
await createCacheDir({ appDir, plugins });
|
2019-09-26 11:52:02 +02:00
|
|
|
|
2021-09-09 10:56:57 +02:00
|
|
|
const entry = path.join(cacheDir, 'admin', 'src');
|
2022-03-09 18:26:17 +01:00
|
|
|
const dest = path.join(buildDestDir, 'build');
|
2019-09-23 19:27:40 +02:00
|
|
|
const env = 'development';
|
|
|
|
|
2021-07-05 11:24:24 +02:00
|
|
|
// Roots for the @strapi/babel-plugin-switch-ee-ce
|
|
|
|
const roots = {
|
|
|
|
eeRoot: path.resolve(cacheDir, 'ee', 'admin'),
|
|
|
|
ceRoot: path.resolve(cacheDir, 'admin', 'src'),
|
|
|
|
};
|
|
|
|
|
2022-04-12 16:57:00 +02:00
|
|
|
const pluginsPath = Object.keys(plugins).map(pluginName => plugins[pluginName].pathToPlugin);
|
2021-09-09 10:56:57 +02:00
|
|
|
|
2022-04-14 10:59:42 +02:00
|
|
|
// Either use the tsconfig file from the generated app or the one inside the .cache folder
|
|
|
|
// so we can develop plugins in TS while being in a JS app
|
|
|
|
const tsConfigFilePath = useTypeScript
|
|
|
|
? path.join(appDir, 'src', 'admin', 'tsconfig.json')
|
|
|
|
: path.resolve(entry, 'tsconfig.json');
|
|
|
|
|
2019-09-23 19:27:40 +02:00
|
|
|
const args = {
|
2022-03-09 18:26:17 +01:00
|
|
|
appDir,
|
2021-09-09 10:56:57 +02:00
|
|
|
cacheDir,
|
2019-09-23 19:27:40 +02:00
|
|
|
dest,
|
2022-03-09 18:26:17 +01:00
|
|
|
entry,
|
2019-09-23 19:27:40 +02:00
|
|
|
env,
|
|
|
|
options,
|
2022-03-09 18:26:17 +01:00
|
|
|
pluginsPath,
|
2021-07-05 11:24:24 +02:00
|
|
|
roots,
|
2022-01-17 12:05:45 +01:00
|
|
|
devServer: {
|
|
|
|
port,
|
|
|
|
client: {
|
|
|
|
logging: 'none',
|
|
|
|
overlay: {
|
|
|
|
errors: true,
|
|
|
|
warnings: false,
|
|
|
|
},
|
2021-11-09 15:55:30 +01:00
|
|
|
},
|
2022-01-17 12:05:45 +01:00
|
|
|
open: browser === 'true' ? true : browser,
|
|
|
|
devMiddleware: {
|
|
|
|
publicPath: options.adminPath,
|
|
|
|
},
|
|
|
|
historyApiFallback: {
|
|
|
|
index: options.adminPath,
|
|
|
|
disableDotRule: true,
|
|
|
|
},
|
2019-09-23 19:27:40 +02:00
|
|
|
},
|
2022-04-14 10:59:42 +02:00
|
|
|
tsConfigFilePath,
|
2019-09-23 19:27:40 +02:00
|
|
|
};
|
|
|
|
|
2022-03-09 18:26:17 +01:00
|
|
|
const webpackConfig = getCustomWebpackConfig(appDir, args);
|
2022-01-17 12:05:45 +01:00
|
|
|
|
|
|
|
const compiler = webpack(webpackConfig);
|
2019-09-23 19:27:40 +02:00
|
|
|
|
2022-02-25 23:20:08 +01:00
|
|
|
const devServerArgs = {
|
|
|
|
...args.devServer,
|
2022-03-02 10:08:55 +01:00
|
|
|
...webpackConfig.devServer,
|
|
|
|
};
|
2022-02-25 23:20:08 +01:00
|
|
|
|
|
|
|
const server = new WebpackDevServer(devServerArgs, compiler);
|
2019-09-24 07:10:02 +02:00
|
|
|
|
2022-01-17 12:05:45 +01:00
|
|
|
const runServer = async () => {
|
2019-09-24 14:10:57 +02:00
|
|
|
console.log(chalk.green('Starting the development server...'));
|
|
|
|
console.log();
|
2021-11-09 15:55:30 +01:00
|
|
|
console.log(chalk.green(`Admin development at http://${host}:${port}${options.adminPath}`));
|
2022-01-17 12:05:45 +01:00
|
|
|
|
|
|
|
await server.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
runServer();
|
2021-07-27 09:26:37 +02:00
|
|
|
|
2022-03-09 18:26:17 +01:00
|
|
|
watchAdminFiles(appDir, useTypeScript);
|
2021-11-29 16:13:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 15:48:16 +02:00
|
|
|
module.exports = {
|
2020-04-14 15:24:14 +02:00
|
|
|
clean,
|
2019-04-29 15:48:16 +02:00
|
|
|
build,
|
2019-09-26 11:52:02 +02:00
|
|
|
watchAdmin,
|
2019-04-29 15:48:16 +02:00
|
|
|
};
|