feat: initial work at bundling ESM and CommonJS
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Mar 2023 15:29:45 +0000 (16:29 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Mar 2023 15:29:45 +0000 (16:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package.json
rollup.config.mjs
src/pools/abstract-pool.ts

index 65e343654521112adb6a4386aa12177734890a94..c06db4c53652dc88c4f842e4e161ed30329adc88 100644 (file)
@@ -3,7 +3,10 @@
   "version": "2.3.8",
   "description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation",
   "license": "MIT",
-  "main": "lib/index.js",
+  "exports": [
+    "lib/index.js",
+    "lib/index.mjs"
+  ],
   "scripts": {
     "prepare": "node prepare.js",
     "build": "rollup --config --environment BUILD:development",
index e1bdac21fe03b621fa5b4e81d2c73b3e2ef5be52..8997445e8f193906422514a30d0c97a837adefa5 100644 (file)
@@ -10,14 +10,30 @@ const isDocumentation = process.env.DOCUMENTATION
 
 export default {
   input: 'src/index.ts',
-  output: {
-    ...(isDevelopmentBuild ? { dir: 'lib' } : { file: 'lib/index.js' }),
-    format: 'cjs',
-    sourcemap: !!isDevelopmentBuild,
-    ...(isDevelopmentBuild && { preserveModules: true }),
-    ...(isDevelopmentBuild && { preserveModulesRoot: 'src' }),
-    ...(!isDevelopmentBuild && { plugins: [terser({ maxWorkers: 2 })] })
-  },
+  output: [
+    {
+      ...(isDevelopmentBuild ? { dir: 'lib' } : { file: 'lib/index.js' }),
+      format: 'cjs',
+      sourcemap: !!isDevelopmentBuild,
+      ...(isDevelopmentBuild && {
+        preserveModules: true,
+        preserveModulesRoot: 'src'
+      }),
+      ...(!isDevelopmentBuild && { plugins: [terser({ maxWorkers: 2 })] })
+    },
+    {
+      ...(isDevelopmentBuild ? { dir: 'lib' } : { file: 'lib/index.mjs' }),
+      format: 'esm',
+      sourcemap: !!isDevelopmentBuild,
+
+      ...(isDevelopmentBuild && {
+        entryFileNames: '[name].mjs',
+        preserveModules: true,
+        preserveModulesRoot: 'src'
+      }),
+      ...(!isDevelopmentBuild && { plugins: [terser({ maxWorkers: 2 })] })
+    }
+  ],
   external: ['async_hooks', 'cluster', 'events', 'os', 'worker_threads'],
   plugins: [
     typescript({
index 695c43b30d3f6697a5407a60ff3ceeca05248350..32b4d983859c3f7fd0ce9354116ed9fcb5d05c53 100644 (file)
@@ -3,7 +3,7 @@ import type {
   PromiseWorkerResponseWrapper
 } from '../utility-types'
 import { EMPTY_FUNCTION, EMPTY_OBJECT_LITERAL } from '../utils'
-import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
+import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
 import type { PoolOptions } from './pool'
 import { PoolEmitter } from './pool'
 import type { IPoolInternal, TasksUsage } from './pool-internal'