Merge pull request #21194 from strapi/chore/strapi-server-resolve

enhancement: resolve strapi-server with exports map too
This commit is contained in:
Alexandre BODIN 2024-09-09 17:45:19 +02:00 committed by GitHub
commit d7ca1a1259
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 84 additions and 99 deletions

View File

@ -10,9 +10,9 @@
"default": "./admin/src/index.js"
},
"./strapi-server": {
"source": "./strapi-server.js",
"require": "./strapi-server.js",
"default": "./strapi-server.js"
"source": "./server/index.js",
"require": "./server/index.js",
"default": "./server/index.js"
},
"./package.json": "./package.json"
},

View File

@ -0,0 +1,11 @@
'use strict';
const config = require('./config');
const register = require('./register');
module.exports = () => {
return {
register,
config,
};
};

View File

@ -1,10 +0,0 @@
'use strict';
const config = require('./server/config');
const register = require('./server/register');
module.exports = () => {
return {
register,
config,
};
};

View File

@ -63,4 +63,4 @@ const getAdminEE = () => {
return eeAdmin;
};
export default getAdminEE();
export default getAdminEE;

View File

@ -1,14 +1,11 @@
import type { Core } from '@strapi/types';
export const enableFeatureMiddleware: Core.MiddlewareFactory =
(featureName: string) => (ctx, next) => {
export const enableFeatureMiddleware =
(featureName: string): Core.MiddlewareHandler =>
(ctx, next) => {
if (strapi.ee.features.isEnabled(featureName)) {
return next();
}
ctx.status = 404;
};
export default {
enableFeatureMiddleware,
};

View File

@ -51,8 +51,9 @@
"./strapi-server": {
"types": "./dist/server/src/index.d.ts",
"source": "./server/src/index.js",
"require": "./strapi-server.js",
"default": "./strapi-server.js"
"import": "./dist/server/index.mjs",
"require": "./dist/server/index.js",
"default": "./dist/server/index.js"
},
"./package.json": "./package.json"
},

View File

@ -17,12 +17,6 @@ const config: Config = defineConfig({
tsconfig: './server/tsconfig.build.json',
runtime: 'node',
},
{
source: './ee/server/src/index.ts',
import: './dist/ee/server/index.mjs',
require: './dist/ee/server/index.js',
runtime: 'node',
},
],
dist: './dist',
/**

View File

@ -1,11 +1,37 @@
export { default as bootstrap } from './bootstrap';
export { default as register } from './register';
export { default as destroy } from './destroy';
import _ from 'lodash';
export { default as config } from './config';
export { default as policies } from './policies';
export { default as routes } from './routes';
export { default as services } from './services';
export { default as controllers } from './controllers';
export { default as contentTypes } from './content-types';
export { default as middlewares } from './middlewares';
import bootstrap from './bootstrap';
import register from './register';
import destroy from './destroy';
import config from './config';
import policies from './policies';
import routes from './routes';
import services from './services';
import controllers from './controllers';
import contentTypes from './content-types';
import middlewares from './middlewares';
import getEEAdmin from '../../ee/server/src';
// eslint-disable-next-line import/no-mutable-exports
let admin = {
bootstrap,
register,
destroy,
config,
policies,
routes,
services,
controllers,
contentTypes,
middlewares,
};
const mergeRoutes = (a: any, b: any, key: string) => {
return _.isArray(a) && _.isArray(b) && key === 'routes' ? a.concat(b) : undefined;
};
if (strapi.EE) {
admin = _.mergeWith({}, admin, getEEAdmin(), mergeRoutes);
}
export default admin;

View File

@ -1,18 +0,0 @@
'use strict';
const _ = require('lodash');
const admin = require('./dist/server');
const mergeRoutes = (a, b, key) => {
return _.isArray(a) && _.isArray(b) && key === 'routes' ? a.concat(b) : undefined;
};
if (strapi.EE) {
const eeAdmin = require('./dist/ee/server');
// module.exports = admin;
// TODO: change to avoid issue with lodash merging frozen objects
module.exports = _.mergeWith({}, admin, eeAdmin, mergeRoutes);
} else {
module.exports = admin;
}

View File

@ -37,8 +37,8 @@
"./strapi-server": {
"types": "./dist/server/src/index.d.ts",
"source": "./server/src/index.ts",
"require": "./strapi-server.js",
"default": "./strapi-server.js"
"require": "./dist/server/index.js",
"default": "./dist/server/index.js"
},
"./package.json": "./package.json"
},

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -99,6 +99,7 @@
"package-json": "7.0.0",
"pkg-up": "3.1.0",
"qs": "6.11.1",
"resolve.exports": "2.0.2",
"semver": "7.5.4",
"statuses": "2.0.1",
"typescript": "5.2.2",

View File

@ -11,6 +11,7 @@ interface PluginMeta {
enabled: boolean;
pathToPlugin?: string;
info: Record<string, unknown>;
packageInfo?: Record<string, unknown>;
}
type PluginMetas = Record<string, PluginMeta>;
@ -102,6 +103,7 @@ export const getEnabledPlugins = async (strapi: Core.Strapi, { client } = { clie
internalPlugins[packageInfo.strapi.name] = {
...toDetailedDeclaration({ enabled: true, resolve: packagePath, isModule: client }),
info: packageInfo.strapi,
packageInfo,
};
}
@ -125,6 +127,7 @@ export const getEnabledPlugins = async (strapi: Core.Strapi, { client } = { clie
...packageInfo.strapi,
packageName: packageInfo.name,
},
packageInfo,
};
}
}
@ -149,6 +152,7 @@ export const getEnabledPlugins = async (strapi: Core.Strapi, { client } = { clie
if (isStrapiPlugin(packageInfo)) {
declaredPlugins[pluginName].info = packageInfo.strapi || {};
declaredPlugins[pluginName].packageInfo = packageInfo;
}
}
});

View File

@ -1,6 +1,8 @@
import { join } from 'path';
import fse from 'fs-extra';
import { defaultsDeep, defaults, getOr, get } from 'lodash/fp';
import * as resolve from 'resolve.exports';
import { env } from '@strapi/utils';
import type { Core, Plugin, Struct } from '@strapi/types';
import { loadConfigFile } from '../../utils/load-config-file';
@ -94,9 +96,20 @@ export default async function loadPlugins(strapi: Core.Strapi) {
const enabledPlugin = enabledPlugins[pluginName];
let serverEntrypointPath;
let resolvedExport = './strapi-server';
try {
serverEntrypointPath = join(enabledPlugin.pathToPlugin, 'strapi-server.js');
resolvedExport = (
resolve.exports(enabledPlugin.packageInfo, 'strapi-server', {
require: true,
}) ?? './strapi-server'
).toString();
} catch (e) {
// key missing in exports map or no export map -> let's try the legacy way
}
try {
serverEntrypointPath = join(enabledPlugin.pathToPlugin, resolvedExport);
} catch (e) {
throw new Error(
`Error loading the plugin ${pluginName} because ${pluginName} is not installed. Please either install the plugin or remove it's configuration.`

View File

@ -3,6 +3,7 @@ import loadAdmin from '../loaders/admin';
export default defineProvider({
init(strapi) {
// eslint-disable-next-line node/no-missing-require
strapi.add('admin', () => require('@strapi/admin/strapi-server'));
},

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./dist/server');

View File

@ -27,9 +27,9 @@
"default": "./dist/admin/index.js"
},
"./strapi-server": {
"source": "./strapi-server.js",
"require": "./strapi-server.js",
"default": "./strapi-server.js"
"source": "./server/index.js",
"require": "./server/index.js",
"default": "./server/index.js"
},
"./package.json": "./package.json"
},

View File

@ -1,3 +0,0 @@
'use strict';
module.exports = require('./server');

View File

@ -8757,6 +8757,7 @@ __metadata:
package-json: "npm:7.0.0"
pkg-up: "npm:3.1.0"
qs: "npm:6.11.1"
resolve.exports: "npm:2.0.2"
semver: "npm:7.5.4"
statuses: "npm:2.0.1"
supertest: "npm:6.3.3"
@ -28113,7 +28114,7 @@ __metadata:
languageName: node
linkType: hard
"resolve.exports@npm:^2.0.0":
"resolve.exports@npm:2.0.2, resolve.exports@npm:^2.0.0":
version: 2.0.2
resolution: "resolve.exports@npm:2.0.2"
checksum: 10c0/cc4cffdc25447cf34730f388dca5021156ba9302a3bad3d7f168e790dc74b2827dff603f1bc6ad3d299bac269828dca96dd77e036dc9fba6a2a1807c47ab5c98