refactor: allow ES import for cloud string if package type is module (#7560)
* allow module import for Parse Cloud * Update .babelrc * catch esm error * Update ParseServer.js * add tests * Update CHANGELOG.md * Update CloudCode.spec.js Co-authored-by: Manuel <5673677+mtrezza@users.noreply.github.com>
This commit is contained in:
3
.babelrc
3
.babelrc
@@ -7,7 +7,8 @@
|
|||||||
["@babel/preset-env", {
|
["@babel/preset-env", {
|
||||||
"targets": {
|
"targets": {
|
||||||
"node": "12"
|
"node": "12"
|
||||||
}
|
},
|
||||||
|
"exclude": ["proposal-dynamic-import"]
|
||||||
}]
|
}]
|
||||||
],
|
],
|
||||||
"sourceMaps": "inline"
|
"sourceMaps": "inline"
|
||||||
|
|||||||
@@ -151,6 +151,7 @@ ___
|
|||||||
- Refactor: uniform issue templates across repos (Manuel Trezza) [#7528](https://github.com/parse-community/parse-server/pull/7528)
|
- Refactor: uniform issue templates across repos (Manuel Trezza) [#7528](https://github.com/parse-community/parse-server/pull/7528)
|
||||||
- ci: bump ci environment (Manuel Trezza) [#7539](https://github.com/parse-community/parse-server/pull/7539)
|
- ci: bump ci environment (Manuel Trezza) [#7539](https://github.com/parse-community/parse-server/pull/7539)
|
||||||
- CI now pushes docker images to Docker Hub (Corey Baker) [#7548](https://github.com/parse-community/parse-server/pull/7548)
|
- CI now pushes docker images to Docker Hub (Corey Baker) [#7548](https://github.com/parse-community/parse-server/pull/7548)
|
||||||
|
- Allow cloud string for ES modules (Daniel Blyth) [#7560](https://github.com/parse-community/parse-server/pull/7560)
|
||||||
- docs: Introduce deprecation ID for reference in comments and online search (Manuel Trezza) [#7562](https://github.com/parse-community/parse-server/pull/7562)
|
- docs: Introduce deprecation ID for reference in comments and online search (Manuel Trezza) [#7562](https://github.com/parse-community/parse-server/pull/7562)
|
||||||
|
|
||||||
## 4.10.3
|
## 4.10.3
|
||||||
|
|||||||
@@ -39,6 +39,14 @@ describe('Cloud Code', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can load cloud code as a module', async () => {
|
||||||
|
process.env.npm_package_type = 'module';
|
||||||
|
await reconfigureServer({ cloud: './spec/cloud/cloudCodeModuleFile.js' });
|
||||||
|
const result = await Parse.Cloud.run('cloudCodeInFile');
|
||||||
|
expect(result).toEqual('It is possible to define cloud code in a file.');
|
||||||
|
delete process.env.npm_package_type;
|
||||||
|
});
|
||||||
|
|
||||||
it('can create functions', done => {
|
it('can create functions', done => {
|
||||||
Parse.Cloud.define('hello', () => {
|
Parse.Cloud.define('hello', () => {
|
||||||
return 'Hello world!';
|
return 'Hello world!';
|
||||||
|
|||||||
3
spec/cloud/cloudCodeModuleFile.js
Normal file
3
spec/cloud/cloudCodeModuleFile.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Parse.Cloud.define('cloudCodeInFile', () => {
|
||||||
|
return 'It is possible to define cloud code in a file.';
|
||||||
|
});
|
||||||
@@ -103,7 +103,11 @@ class ParseServer {
|
|||||||
if (typeof cloud === 'function') {
|
if (typeof cloud === 'function') {
|
||||||
cloud(Parse);
|
cloud(Parse);
|
||||||
} else if (typeof cloud === 'string') {
|
} else if (typeof cloud === 'string') {
|
||||||
require(path.resolve(process.cwd(), cloud));
|
if (process.env.npm_package_type === 'module') {
|
||||||
|
import(path.resolve(process.cwd(), cloud));
|
||||||
|
} else {
|
||||||
|
require(path.resolve(process.cwd(), cloud));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw "argument 'cloud' must either be a string or a function";
|
throw "argument 'cloud' must either be a string or a function";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user