From: Jérôme Benoit Date: Fri, 3 Jun 2022 20:06:37 +0000 (+0200) Subject: Add initial support for ESM build X-Git-Tag: v1.1.61~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=44a95b7fc8487ae8116f89820ca03cb108dfada8;p=e-mobility-charging-stations-simulator.git Add initial support for ESM build Signed-off-by: Jérôme Benoit --- diff --git a/docker/Dockerfile b/docker/Dockerfile index 113b4251..c5ac91aa 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -30,5 +30,5 @@ COPY README.md LICENSE ./ COPY docker/autoconfig.sh /autoconfig.sh RUN chmod +x /autoconfig.sh -#CMD ["node", "dist/start.js"] -CMD /autoconfig.sh && node -r source-map-support/register dist/start.js +#CMD ["node", "dist/start.cjs"] +CMD /autoconfig.sh && node -r source-map-support/register dist/start.cjs diff --git a/manifest-cf-template.yml b/manifest-cf-template.yml index d7c5d6df..74b5dfb9 100644 --- a/manifest-cf-template.yml +++ b/manifest-cf-template.yml @@ -9,8 +9,8 @@ applications: no-route: true health-check-type: process health-check-invocation-timeout: 10 - command: node -r source-map-support/register dist/start.js - # command: node -r source-map-support/register dist/httpd/ui/start.js + command: node -r source-map-support/register dist/start.cjs + # command: node -r source-map-support/register dist/httpd/ui/start.cjs env: # OPTIMIZE_MEMORY: true NODE_OPTIONS: --stack-trace-limit=1024 --max-old-space-size=768 diff --git a/package.json b/package.json index 87ebe4ec..ff1c06e9 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "url": "http://www.apache.org/licenses/LICENSE-2.0" } ], - "main": "./dist/start.js", + "main": "./dist/start.cjs", "lint-staged": { "src/**/*.{js,ts}": [ "prettier --write", @@ -45,16 +45,17 @@ "scripts": { "prepare": "node prepare.js", "prestart": "npm run build", - "start": "cross-env NODE_ENV=production node -r source-map-support/register dist/start.js", - "start:server": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/ui/httpd/start.js", - "start:debug": "cross-env NODE_ENV=production node -r source-map-support/register --inspect dist/start.js", - "start:dev": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.js", - "start:dev:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.js", - "start:prof": "cross-env NODE_ENV=production node -r source-map-support/register --prof dist/start.js", - "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.js", - "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.js", - "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.js", - "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.js", + "start": "cross-env NODE_ENV=production node -r source-map-support/register dist/start.cjs", + "start:esm": "cross-env NODE_ENV=production node -r source-map-support/register dist/start.mjs", + "start:server": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/ui/httpd/start.cjs", + "start:debug": "cross-env NODE_ENV=production node -r source-map-support/register --inspect dist/start.cjs", + "start:dev": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.cjs", + "start:dev:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.cjs", + "start:prof": "cross-env NODE_ENV=production node -r source-map-support/register --prof dist/start.cjs", + "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.cjs", + "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.cjs", + "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.cjs", + "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.cjs", "rollup": "rollup --config", "build": "npm run rollup", "build:dev": "npm run rollup -- --environment BUILD:development", diff --git a/rollup.config.mjs b/rollup.config.mjs index 7f0e5774..bdc96d66 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -10,15 +10,28 @@ const isDevelopmentBuild = process.env.BUILD === 'development'; export default { input: ['src/start.ts', 'src/ui/httpd/start.ts', 'src/charging-station/ChargingStationWorker.ts'], - output: { - dir: 'dist', - format: 'cjs', - exports: 'auto', - sourcemap: true, - preserveModules: true, - preserveModulesRoot: 'src', - ...(!isDevelopmentBuild && { plugins: [terser({ numWorkers: 2 })] }), - }, + output: [ + { + dir: 'dist', + format: 'es', + exports: 'auto', + sourcemap: true, + preserveModules: true, + preserveModulesRoot: 'src', + entryFileNames: '[name].mjs', + ...(!isDevelopmentBuild && { plugins: [terser({ numWorkers: 2 })] }), + }, + { + dir: 'dist', + format: 'cjs', + exports: 'auto', + sourcemap: true, + preserveModules: true, + preserveModulesRoot: 'src', + entryFileNames: '[name].cjs', + ...(!isDevelopmentBuild && { plugins: [terser({ numWorkers: 2 })] }), + }, + ], external: [ 'basic-ftp', 'chalk', diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 1ce68ba2..82aef5cd 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -41,7 +41,7 @@ export default class Bootstrap { this.workerScript = path.join( path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'charging-station', - 'ChargingStationWorker.js' + 'ChargingStationWorker' + path.extname(fileURLToPath(import.meta.url)) ); this.initialize(); this.initWorkerImplementation(); diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index bfb79336..1dd064fb 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,6 +1,7 @@ import { WorkerData, WorkerOptions } from '../types/Worker'; import WorkerConstants from './WorkerConstants'; +import fs from 'fs'; export default abstract class WorkerAbstract { protected readonly workerScript: string; @@ -28,6 +29,12 @@ export default abstract class WorkerAbstract { }, } ) { + if (!workerScript) { + throw new Error('Worker script is not defined'); + } + if (!fs.existsSync(workerScript)) { + throw new Error('Worker script file does not exist'); + } this.workerScript = workerScript; this.workerOptions = workerOptions; } diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index b297f42b..d95f4d32 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -19,7 +19,7 @@ export default class WorkerFactory { workerOptions?: WorkerOptions ): WorkerAbstract | null { if (!isMainThread) { - throw new Error('Trying to get a worker implementation outside the main thread'); + throw new Error('Cannot get a worker implementation outside the main thread'); } workerOptions = workerOptions ?? ({} as WorkerOptions); workerOptions.workerStartDelay =