mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-03 19:36:20 +00:00 
			
		
		
		
	Use enabled plugins to build admin
This commit is contained in:
		
							parent
							
								
									76d1ac7ada
								
							
						
					
					
						commit
						eb522e73e2
					
				@ -34,9 +34,9 @@ function getCustomWebpackConfig(dir, config) {
 | 
			
		||||
  return webpackConfig;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function build({ dir, env, options, optimize }) {
 | 
			
		||||
async function build({ plugins, dir, env, options, optimize }) {
 | 
			
		||||
  // Create the cache dir containing the front-end files.
 | 
			
		||||
  await createCacheDir(dir);
 | 
			
		||||
  await createCacheDir({ dir, plugins });
 | 
			
		||||
 | 
			
		||||
  const cacheDir = path.resolve(dir, '.cache');
 | 
			
		||||
  const entry = path.resolve(cacheDir, 'admin', 'src');
 | 
			
		||||
@ -78,34 +78,24 @@ async function build({ dir, env, options, optimize }) {
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function createPluginsJs(plugins, localPlugins, dest) {
 | 
			
		||||
  const createPluginsArray = plugins =>
 | 
			
		||||
    plugins.map(name => {
 | 
			
		||||
      const shortName = _.camelCase(name.replace(/^@strapi\/plugin-/i, ''));
 | 
			
		||||
 | 
			
		||||
async function createPluginsJs(plugins, dest) {
 | 
			
		||||
  const pluginsArray = plugins.map(({ name }) => {
 | 
			
		||||
    const shortName = _.camelCase(name);
 | 
			
		||||
    return { name, shortName };
 | 
			
		||||
  });
 | 
			
		||||
  const appPluginsArray = createPluginsArray(plugins);
 | 
			
		||||
  const localPluginsArray = createPluginsArray(localPlugins);
 | 
			
		||||
 | 
			
		||||
  const content = `
 | 
			
		||||
${appPluginsArray
 | 
			
		||||
${pluginsArray
 | 
			
		||||
  .map(({ name, shortName }) => {
 | 
			
		||||
    const req = `'../../plugins/${name}/admin/src'`;
 | 
			
		||||
 | 
			
		||||
    return `import ${shortName} from ${req};`;
 | 
			
		||||
  })
 | 
			
		||||
  .join('\n')}
 | 
			
		||||
${localPluginsArray
 | 
			
		||||
  .map(({ name, shortName }) => {
 | 
			
		||||
    const req = `'../../../plugins/${name}/admin/src'`;
 | 
			
		||||
 | 
			
		||||
    return `import ${shortName} from ${req};`;
 | 
			
		||||
  })
 | 
			
		||||
  .join('\n')}
 | 
			
		||||
 | 
			
		||||
const plugins = {
 | 
			
		||||
${[...appPluginsArray, ...localPluginsArray]
 | 
			
		||||
${[...pluginsArray]
 | 
			
		||||
  .map(({ name, shortName }) => {
 | 
			
		||||
    return `  '${name}': ${shortName},`;
 | 
			
		||||
  })
 | 
			
		||||
@ -126,8 +116,8 @@ async function clean({ dir }) {
 | 
			
		||||
  fs.removeSync(cacheDir);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function copyPlugin(name, dest) {
 | 
			
		||||
  const pkgFilePath = getPkgPath(name);
 | 
			
		||||
async function copyPlugin({ name, pathToPlugin }, dest) {
 | 
			
		||||
  const pkgFilePath = getPkgPath(pathToPlugin);
 | 
			
		||||
 | 
			
		||||
  const resolveDepPath = (...args) => path.resolve(pkgFilePath, ...args);
 | 
			
		||||
  const resolveDest = (...args) => path.resolve(dest, 'plugins', name, ...args);
 | 
			
		||||
@ -158,25 +148,16 @@ async function copyAdmin(dest) {
 | 
			
		||||
  await fs.copy(path.resolve(adminPath, 'package.json'), path.resolve(dest, 'package.json'));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function createCacheDir(dir) {
 | 
			
		||||
async function createCacheDir({ dir, plugins }) {
 | 
			
		||||
  const cacheDir = path.resolve(dir, '.cache');
 | 
			
		||||
 | 
			
		||||
  const pkgJSON = require(path.join(dir, 'package.json'));
 | 
			
		||||
 | 
			
		||||
  const pluginsToCopy = Object.keys(pkgJSON.dependencies).filter(
 | 
			
		||||
    dep =>
 | 
			
		||||
      dep.startsWith('@strapi/plugin') &&
 | 
			
		||||
      fs.existsSync(path.resolve(getPkgPath(dep), 'admin', 'src', 'index.js'))
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  let localPluginsToCopy = [];
 | 
			
		||||
  if (fs.existsSync(path.join(dir, 'plugins'))) {
 | 
			
		||||
    localPluginsToCopy = fs
 | 
			
		||||
      .readdirSync(path.join(dir, 'plugins'))
 | 
			
		||||
      .filter(plugin =>
 | 
			
		||||
        fs.existsSync(path.resolve(dir, 'plugins', plugin, 'admin', 'src', 'index.js'))
 | 
			
		||||
      );
 | 
			
		||||
  }
 | 
			
		||||
  const pluginsToCopy = Object.keys(plugins)
 | 
			
		||||
    .filter(pluginName => {
 | 
			
		||||
      const pluginInfo = plugins[pluginName];
 | 
			
		||||
      // TODO: use strapi-admin
 | 
			
		||||
      return fs.existsSync(path.resolve(pluginInfo.pathToPlugin, 'admin', 'src', 'index.js'));
 | 
			
		||||
    })
 | 
			
		||||
    .map(name => ({ name, ...plugins[name] }));
 | 
			
		||||
 | 
			
		||||
  // create .cache dir
 | 
			
		||||
  await fs.emptyDir(cacheDir);
 | 
			
		||||
@ -185,7 +166,7 @@ async function createCacheDir(dir) {
 | 
			
		||||
  await copyAdmin(cacheDir);
 | 
			
		||||
 | 
			
		||||
  // copy plugins code
 | 
			
		||||
  await Promise.all(pluginsToCopy.map(name => copyPlugin(name, cacheDir)));
 | 
			
		||||
  await Promise.all(pluginsToCopy.map(plugin => copyPlugin(plugin, cacheDir)));
 | 
			
		||||
 | 
			
		||||
  // Copy app.js
 | 
			
		||||
  const customAdminConfigFilePath = path.join(dir, 'admin', 'app.js');
 | 
			
		||||
@ -202,12 +183,12 @@ async function createCacheDir(dir) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // create plugins.js with plugins requires
 | 
			
		||||
  await createPluginsJs(pluginsToCopy, localPluginsToCopy, cacheDir);
 | 
			
		||||
  await createPluginsJs(pluginsToCopy, cacheDir);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function watchAdmin({ dir, host, port, browser, options }) {
 | 
			
		||||
async function watchAdmin({ plugins, dir, host, port, browser, options }) {
 | 
			
		||||
  // Create the cache dir containing the front-end files.
 | 
			
		||||
  await createCacheDir(dir);
 | 
			
		||||
  await createCacheDir({ dir, plugins });
 | 
			
		||||
 | 
			
		||||
  const entry = path.join(dir, '.cache', 'admin', 'src');
 | 
			
		||||
  const dest = path.join(dir, 'build');
 | 
			
		||||
 | 
			
		||||
@ -1,23 +1,32 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
const { green } = require('chalk');
 | 
			
		||||
 | 
			
		||||
// eslint-disable-next-line node/no-extraneous-require
 | 
			
		||||
const strapiAdmin = require('@strapi/admin');
 | 
			
		||||
const { getConfigUrls } = require('@strapi/utils');
 | 
			
		||||
const loadConfiguration = require('../core/app-configuration');
 | 
			
		||||
const ee = require('../utils/ee');
 | 
			
		||||
 | 
			
		||||
const ee = require('../utils/ee');
 | 
			
		||||
const addSlash = require('../utils/addSlash');
 | 
			
		||||
const strapi = require('../index');
 | 
			
		||||
const getEnabledPlugins = require('../core/loaders/plugins/get-enabled-plugins');
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * `$ strapi build`
 | 
			
		||||
 */
 | 
			
		||||
module.exports = async ({ clean, optimization }) => {
 | 
			
		||||
  const dir = process.cwd();
 | 
			
		||||
  const config = loadConfiguration(dir);
 | 
			
		||||
 | 
			
		||||
  const { serverUrl, adminPath } = getConfigUrls(config.server, true);
 | 
			
		||||
  const strapiInstance = strapi({
 | 
			
		||||
    dir,
 | 
			
		||||
    autoReload: true,
 | 
			
		||||
    serveAdminPanel: false,
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  console.log(`Building your admin UI with ${green(config.environment)} configuration ...`);
 | 
			
		||||
  const plugins = await getEnabledPlugins(strapiInstance);
 | 
			
		||||
 | 
			
		||||
  const env = strapiInstance.config.get('environment');
 | 
			
		||||
  const { serverUrl, adminPath } = getConfigUrls(strapiInstance.config.get('server'), true);
 | 
			
		||||
 | 
			
		||||
  console.log(`Building your admin UI with ${green(env)} configuration ...`);
 | 
			
		||||
 | 
			
		||||
  if (clean) {
 | 
			
		||||
    await strapiAdmin.clean({ dir });
 | 
			
		||||
@ -28,6 +37,7 @@ module.exports = async ({ clean, optimization }) => {
 | 
			
		||||
  return strapiAdmin
 | 
			
		||||
    .build({
 | 
			
		||||
      dir,
 | 
			
		||||
      plugins,
 | 
			
		||||
      // front end build env is always production for now
 | 
			
		||||
      env: 'production',
 | 
			
		||||
      optimize: optimization,
 | 
			
		||||
 | 
			
		||||
@ -2,32 +2,42 @@
 | 
			
		||||
 | 
			
		||||
// eslint-disable-next-line node/no-extraneous-require
 | 
			
		||||
const strapiAdmin = require('@strapi/admin');
 | 
			
		||||
const { getOr } = require('lodash/fp');
 | 
			
		||||
const { getConfigUrls, getAbsoluteServerUrl } = require('@strapi/utils');
 | 
			
		||||
const loadConfiguration = require('../core/app-configuration');
 | 
			
		||||
 | 
			
		||||
const ee = require('../utils/ee');
 | 
			
		||||
const addSlash = require('../utils/addSlash');
 | 
			
		||||
const strapi = require('../index');
 | 
			
		||||
const getEnabledPlugins = require('../core/loaders/plugins/get-enabled-plugins');
 | 
			
		||||
 | 
			
		||||
module.exports = async function({ browser }) {
 | 
			
		||||
  const dir = process.cwd();
 | 
			
		||||
 | 
			
		||||
  const config = loadConfiguration(dir);
 | 
			
		||||
  const strapiInstance = strapi({
 | 
			
		||||
    dir,
 | 
			
		||||
    autoReload: true,
 | 
			
		||||
    serveAdminPanel: false,
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  const { adminPath } = getConfigUrls(config.server, true);
 | 
			
		||||
  const plugins = await getEnabledPlugins(strapiInstance);
 | 
			
		||||
 | 
			
		||||
  const adminPort = getOr(8000, 'server.admin.port')(config);
 | 
			
		||||
  const adminHost = getOr('localhost', 'server.admin.host')(config);
 | 
			
		||||
  const adminWatchIgnoreFiles = getOr([], 'server.admin.watchIgnoreFiles')(config);
 | 
			
		||||
  const { adminPath } = getConfigUrls(strapiInstance.config.get('server'), true);
 | 
			
		||||
 | 
			
		||||
  const adminPort = strapiInstance.config.get('server.admin.port', 8000);
 | 
			
		||||
  const adminHost = strapiInstance.config.get('server.admin.host', 'localhost');
 | 
			
		||||
  const adminWatchIgnoreFiles = strapiInstance.config.get('server.admin.watchIgnoreFiles', []);
 | 
			
		||||
 | 
			
		||||
  const backendURL = getAbsoluteServerUrl(strapiInstance.config, true);
 | 
			
		||||
 | 
			
		||||
  ee({ dir });
 | 
			
		||||
 | 
			
		||||
  strapiAdmin.watchAdmin({
 | 
			
		||||
    dir,
 | 
			
		||||
    plugins,
 | 
			
		||||
    port: adminPort,
 | 
			
		||||
    host: adminHost,
 | 
			
		||||
    browser,
 | 
			
		||||
    options: {
 | 
			
		||||
      backend: getAbsoluteServerUrl(config, true),
 | 
			
		||||
      backend: backendURL,
 | 
			
		||||
      adminPath: addSlash(adminPath),
 | 
			
		||||
      watchIgnoreFiles: adminWatchIgnoreFiles,
 | 
			
		||||
      features: ee.isEE ? ee.features.getEnabled() : [],
 | 
			
		||||
 | 
			
		||||
@ -50,8 +50,6 @@ module.exports = (projectDirectory, cliArguments) => {
 | 
			
		||||
    installDependencies: true,
 | 
			
		||||
    strapiDependencies: [
 | 
			
		||||
      '@strapi/strapi',
 | 
			
		||||
      '@strapi/admin',
 | 
			
		||||
      '@strapi/utils',
 | 
			
		||||
      '@strapi/plugin-users-permissions',
 | 
			
		||||
      '@strapi/plugin-i18n',
 | 
			
		||||
    ],
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user