Skip to content

Commit

Permalink
feat: Unitech#4540 support ES import
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Jan 13, 2020
1 parent e96cda7 commit dc21450
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 5 deletions.
9 changes: 9 additions & 0 deletions examples/import/circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const PI = 3.14159265359;

export function area(radius) {
return (radius ** 2) * PI;
}

export function circumference(radius) {
return 2 * radius * PI;
}
7 changes: 7 additions & 0 deletions examples/import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { area, circumference } from './circle.js';

const r = 3;

console.log(`Circle with radius ${r} has
area: ${area(r)};
circunference: ${circumference(r)}`);
12 changes: 12 additions & 0 deletions examples/import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "import",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type":"module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
8 changes: 6 additions & 2 deletions lib/ProcessContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var p = require('path');
var cst = require('../constants');
var Utility = require('./Utility.js');
var ProcessUtils = require('./ProcessUtils');

// Load all env-vars from master.
var pm2_env = JSON.parse(process.env.pm2_env);
Expand All @@ -30,7 +31,7 @@ delete process.env.pm2_env;
(function ProcessContainer() {
var fs = require('fs');

require('./ProcessUtils').injectModules();
ProcessUtils.injectModules()

var stdFile = pm2_env.pm_log_path;
var outFile = pm2_env.pm_out_log_path;
Expand Down Expand Up @@ -294,7 +295,10 @@ function exec(script, stds) {
// Change dir to fix process.cwd
process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script));

require('module')._load(script, null, true);
if (ProcessUtils.isESModule(script) === true)
import(process.env.pm_exec_path);
else
require('module')._load(script, null, true);

function logError(types, error){
try {
Expand Down
12 changes: 9 additions & 3 deletions lib/ProcessContainerFork.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* can be found in the LICENSE file.
*/
// Inject custom modules
require('./ProcessUtils').injectModules();
var ProcessUtils = require('./ProcessUtils')
ProcessUtils.injectModules()

if (typeof(process.env.source_map_support) != "undefined" &&
process.env.source_map_support !== "false") {
Expand All @@ -23,8 +24,13 @@ if (process.connected &&
});

// Require the real application
if (process.env.pm_exec_path)
require('module')._load(process.env.pm_exec_path, null, true);
if (process.env.pm_exec_path) {
if (ProcessUtils.isESModule(process.env.pm_exec_path) === true) {
import(process.env.pm_exec_path);
}
else
require('module')._load(process.env.pm_exec_path, null, true);
}
else
throw new Error('Could not _load() the script');

Expand Down
21 changes: 21 additions & 0 deletions lib/ProcessUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,26 @@ module.exports = {
tracing: process.env.trace === 'true' || false
}, conf))
}
},
isESModule(exec_path) {
var fs = require('fs')
var path = require('path')
var semver = require('semver')

if (semver.satisfies(process.version, '< 13.3.0'))
return false

if (path.extname(exec_path) === '.mjs')
return true

try {
var data = JSON.parse(fs.readFileSync(path.join(path.dirname(exec_path), 'package.json')))
if (data.type === 'module')
return true
else
return false
} catch(e) {
return false
}
}
};
2 changes: 2 additions & 0 deletions test/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ if [ $SUPV = '<6' ]; then
exit
fi

runTest ./test/e2e/esmodule.sh

runTest ./test/e2e/cli/monit.sh
runTest ./test/e2e/cli/cli-actions-1.sh
runTest ./test/e2e/cli/cli-actions-2.sh
Expand Down
65 changes: 65 additions & 0 deletions test/e2e/esmodule.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"

# Bootstrap one app
cd $file_path/esmodules/mjs

#### FORK MODE

$pm2 delete all

$pm2 start index.mjs
sleep 2
should 'should have detected es module via .mjs file extension and started 1 app' 'online' 1
should 'should have application in stable state' 'restart_time: 0' 1

$pm2 restart index
sleep 2
should 'should have detected es module via .mjs file extension and started 1 app' 'online' 1
should 'should have application in stable state' 'restart_time: 1' 1

$pm2 delete all

cd $file_path/esmodules/packagemodule

$pm2 start index.js
sleep 2
should 'should have detected es module via .mjs file extension and started 1 app' 'online' 1
should 'should have application in stable state' 'restart_time: 0' 1

$pm2 restart index
sleep 2
should 'should have detected es module via .mjs file extension and started 1 app' 'online' 1
should 'should have application in stable state' 'restart_time: 1' 1

#### CLUSTER MODE

$pm2 delete all

$pm2 start index.mjs -i 4
sleep 2
should 'should have detected es module via .mjs file extension and started 4 apps' 'online' 4
should 'should have application in stable state' 'restart_time: 0' 4

$pm2 restart index
sleep 2
should 'should have detected es module via .mjs file extension and started 4 app' 'online' 4
should 'should have application in stable state' 'restart_time: 1' 4

$pm2 delete all

cd $file_path/esmodules/packagemodule

$pm2 start index.js -i 4
sleep 2
should 'should have detected es module via .mjs file extension and started 4 apps' 'online' 4
should 'should have application in stable state' 'restart_time: 0' 4

$pm2 restart index
sleep 2
should 'should have detected es module via .mjs file extension and started 4 app' 'online' 4
should 'should have application in stable state' 'restart_time: 1' 4

$pm2 delete all
9 changes: 9 additions & 0 deletions test/fixtures/esmodules/mjs/circle.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const PI = 3.14159265359;

export function area(radius) {
return (radius ** 2) * PI;
}

export function circumference(radius) {
return 2 * radius * PI;
}
10 changes: 10 additions & 0 deletions test/fixtures/esmodules/mjs/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { area, circumference } from './circle.mjs';

const r = 3;

console.log(`Circle with radius ${r} has
area: ${area(r)};
circunference: ${circumference(r)}`);

setInterval(() => {
}, 1000)
9 changes: 9 additions & 0 deletions test/fixtures/esmodules/packagemodule/circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const PI = 3.14159265359;

export function area(radius) {
return (radius ** 2) * PI;
}

export function circumference(radius) {
return 2 * radius * PI;
}
10 changes: 10 additions & 0 deletions test/fixtures/esmodules/packagemodule/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { area, circumference } from './circle.js';

const r = 3;

console.log(`Circle with radius ${r} has
area: ${area(r)};
circunference: ${circumference(r)}`);

setInterval(() => {
}, 1000)
12 changes: 12 additions & 0 deletions test/fixtures/esmodules/packagemodule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "import",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type":"module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

0 comments on commit dc21450

Please sign in to comment.