Skip to content

Commit

Permalink
feat: Support .cjs and .mjs configuration extensions (#11586)
Browse files Browse the repository at this point in the history
  • Loading branch information
RianFuro committed Dec 12, 2022
1 parent f00eb82 commit 9d57933
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/cli/resolve-configuration-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ServerlessError = require('../serverless-error');
const fileExists = require('../utils/fs/file-exists');
const resolveInput = require('./resolve-input');

const supportedExtensions = new Set(['yml', 'yaml', 'json', 'js', 'ts']);
const supportedExtensions = new Set(['yml', 'yaml', 'json', 'js', 'mjs', 'cjs', 'ts']);

module.exports = async (options = {}) => {
if (!options) options = {};
Expand Down
23 changes: 23 additions & 0 deletions lib/configuration/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const parseConfigurationFile = async (configurationPath) => {
}
}
// fallthrough
case '.cjs':
case '.js': {
const configurationEventuallyDeferred = (() => {
try {
Expand Down Expand Up @@ -166,6 +167,28 @@ const parseConfigurationFile = async (configurationPath) => {
);
}
}
case '.mjs': {
try {
require.resolve(configurationPath);
} catch {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
try {
// dynamic import might not be available depending on the node version of the user, but we assume it is, since
// the user explicitly declared his use of es-module syntax by using the .mjs extension.
return (await require('../utils/import-esm')(configurationPath)).default;
} catch (error) {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
}`,
'CONFIGURATION_INITIALIZATION_ERROR'
);
}
}
default:
throw new ServerlessError(
`Cannot parse "${path.basename(configurationPath)}": Unsupported file extension`,
Expand Down
12 changes: 12 additions & 0 deletions test/unit/lib/cli/resolve-configuration-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ describe('test/unit/lib/cli/resolve-configuration-path.test.js', () => {
expect(await resolveServerlessConfigPath()).to.equal(configurationPath);
});

it('should recognize "serverless.cjs"', async () => {
configurationPath = path.resolve('serverless.cjs');
await fse.ensureFile(configurationPath);
expect(await resolveServerlessConfigPath()).to.equal(configurationPath);
});

it('should recognize "serverless.mjs"', async () => {
configurationPath = path.resolve('serverless.mjs');
await fse.ensureFile(configurationPath);
expect(await resolveServerlessConfigPath()).to.equal(configurationPath);
});

describe('"--config" param support', () => {
before(async () =>
Promise.all([
Expand Down
20 changes: 20 additions & 0 deletions test/unit/lib/configuration/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ describe('test/unit/lib/configuration/read.test.js', () => {
}
});

it('should read "serverless.cjs"', async () => {
configurationPath = 'serverless.cjs';
const configuration = {
service: 'test-js',
provider: { name: 'aws' },
};
await fsp.writeFile(configurationPath, `module.exports = ${JSON.stringify(configuration)}`);
expect(await readConfiguration(configurationPath)).to.deep.equal(configuration);
});

it('should read "serverless.mjs"', async () => {
configurationPath = 'serverless.mjs';
const configuration = {
service: 'test-js',
provider: { name: 'aws' },
};
await fsp.writeFile(configurationPath, `export default ${JSON.stringify(configuration)}`);
expect(await readConfiguration(configurationPath)).to.deep.equal(configuration);
});

it('should register ts-node only if it is not already registered', async () => {
try {
expect(process[Symbol.for('ts-node.register.instance')]).to.be.undefined;
Expand Down

0 comments on commit 9d57933

Please sign in to comment.