Skip to content

Commit

Permalink
chore: use esm for server code by default (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin authored Sep 27, 2024
1 parent c4702ad commit fc7192a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-turkeys-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@strapi/sdk-plugin": patch
---

chore: use esm for server code by default
63 changes: 37 additions & 26 deletions src/cli/commands/plugin/init/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,6 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => {

return {
prompts: [
definePackageOption({
name: 'repo',
type: 'text',
message: 'git url',
validate(val: unknown) {
if (!val) {
return true;
}

try {
const result = gitUrlParse(val as any);

repo = { source: result.source, owner: result.owner, name: result.name };

return true;
} catch (err) {
return 'invalid git url';
}
},
}),
definePackageOption({
name: 'pkgName',
type: 'text',
Expand Down Expand Up @@ -227,6 +207,26 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => {
message: 'plugin author email',
initial: gitConfig?.user?.email,
}),
definePackageOption({
name: 'repo',
type: 'text',
message: 'git url',
validate(val: unknown) {
if (!val) {
return true;
}

try {
const result = gitUrlParse(val as any);

repo = { source: result.source, owner: result.owner, name: result.name };

return true;
} catch (err) {
return 'invalid git url';
}
},
}),
definePackageOption({
name: 'license',
type: 'text',
Expand Down Expand Up @@ -450,13 +450,24 @@ const getPluginTemplate = ({ suggestedPackageName }: PluginTemplateOptions) => {
'@strapi/typescript-utils': '*',
typescript: '*',
};
} else if (isRecord(pkgJson.exports['./strapi-admin'])) {
// If the plugin is not typescript, we need to add a jsconfig.json file
// to the frontend code. This configuration ensures we have no
// build errors for the frontend javascript code.
const { adminJsConfigFile } = await import('./files/javascript');
} else {
if (isRecord(pkgJson.exports['./strapi-admin'])) {
// If the plugin is not typescript, we need to add a jsconfig.json file
// to the frontend code. This configuration ensures we have no
// build errors for the frontend javascript code.
const { adminJsConfigFile } = await import('./files/javascript');

files.push(adminJsConfigFile);
files.push(adminJsConfigFile);
}

if (isRecord(pkgJson.exports['./strapi-server'])) {
// If the plugin is not typescript, we need to add a jsconfig.json file
// to the frontend code. This configuration ensures we have no
// build errors for the frontend javascript code.
const { serverJsConfigFile } = await import('./files/javascript');

files.push(serverJsConfigFile);
}
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/cli/commands/plugin/init/files/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ const ADMIN: TemplateFile = {
`,
};

export { ADMIN as adminJsConfigFile };
const SERVER: TemplateFile = {
name: 'server/jsconfig.json',
contents: outdent`
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": [
"./src/**/*.js",
],
"exclude": ["node_modules"]
}
`,
};

export { ADMIN as adminJsConfigFile, SERVER as serverJsConfigFile };
78 changes: 26 additions & 52 deletions src/cli/commands/plugin/init/files/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,25 @@ const JAVASCRIPT = (pluginName: string): TemplateFile[] => [
{
name: 'server/src/index.js',
contents: outdent`
'use strict';
/**
* Application methods
*/
const bootstrap = require('./bootstrap');
const destroy = require('./destroy');
const register = require('./register');
import bootstrap from './bootstrap';
import destroy from './destroy';
import register from './register';
/**
* Plugin server methods
*/
const config = require('./config');
const contentTypes = require('./content-types');
const controllers = require('./controllers');
const middlewares = require('./middlewares');
const policies = require('./policies');
const routes = require('./routes');
const services = require('./services');
module.exports = {
import config from './config';
import contentTypes from './content-types';
import controllers from './controllers';
import middlewares from './middlewares';
import policies from './policies';
import routes from './routes';
import services from './services';
export default {
bootstrap,
destroy,
register,
Expand All @@ -213,45 +211,37 @@ const JAVASCRIPT = (pluginName: string): TemplateFile[] => [
{
name: 'server/src/bootstrap.js',
contents: outdent`
'use strict';
const bootstrap = ({ strapi }) => {
// bootstrap phase
};
module.exports = bootstrap;
export default bootstrap;
`,
},
{
name: 'server/src/destroy.js',
contents: outdent`
'use strict';
const destroy = ({ strapi }) => {
// destroy phase
};
module.exports = destroy;
export default destroy;
`,
},
{
name: 'server/src/register.js',
contents: outdent`
'use strict';
const register = ({ strapi }) => {
// register phase
};
module.exports = register;
export default register;
`,
},
{
name: 'server/src/config/index.js',
contents: outdent`
'use strict';
module.exports = {
export default {
default: {},
validator() {},
};
Expand All @@ -260,28 +250,22 @@ const JAVASCRIPT = (pluginName: string): TemplateFile[] => [
{
name: 'server/src/content-types/index.js',
contents: outdent`
'use strict';
module.exports = {};
export default {};
`,
},
{
name: 'server/src/controllers/index.js',
contents: outdent`
'use strict';
const controller = require('./controller');
import controller from './controller';
module.exports = {
export default {
controller,
};
`,
},
{
name: 'server/src/controllers/controller.js',
contents: outdent`
'use strict';
const controller = ({ strapi }) => ({
index(ctx) {
ctx.body = strapi
Expand All @@ -292,31 +276,25 @@ const JAVASCRIPT = (pluginName: string): TemplateFile[] => [
},
});
module.exports = controller
export default controller
`,
},
{
name: 'server/src/middlewares/index.js',
contents: outdent`
'use strict';
module.exports = {};
export default {};
`,
},
{
name: 'server/src/policies/index.js',
contents: outdent`
'use strict';
module.exports = {};
export default {};
`,
},
{
name: 'server/src/routes/index.js',
contents: outdent`
'use strict';
module.exports = [
export default [
{
method: 'GET',
path: '/',
Expand All @@ -332,27 +310,23 @@ const JAVASCRIPT = (pluginName: string): TemplateFile[] => [
{
name: 'server/src/services/index.js',
contents: outdent`
'use strict';
const service = require('./service');
import service from './service';
module.exports = {
export default {
service,
};
`,
},
{
name: 'server/src/services/service.js',
contents: outdent`
'use strict';
const service = ({ strapi }) => ({
getWelcomeMessage() {
return 'Welcome to Strapi 🚀';
},
});
module.exports = service
export default service
`,
},
];
Expand Down

0 comments on commit fc7192a

Please sign in to comment.