From 2f2e1980915f91060c2d7a33fddba115841d85e4 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 9 Oct 2024 21:40:55 +0200 Subject: [PATCH] build: separate browser and NodeJS versions --- esbuild/build.js | 43 ++++++++++++++++++++++++++++++++++--------- package.json | 30 ++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/esbuild/build.js b/esbuild/build.js index cc3eac1..097b743 100755 --- a/esbuild/build.js +++ b/esbuild/build.js @@ -15,11 +15,8 @@ const common = { }; (async () => { - const runtime = await esbuild.build({ - ...common, + const runtimeCommon = { entryPoints: ['src/runtime/index.ts'], - outfile: 'build/runtime/index.js', - minify: true, loader: { '.svg': 'text', '.ttf': 'file', @@ -27,9 +24,24 @@ const common = { '.woff2': 'file', }, plugins: [inlineScss()], + }; + + const runtime = await esbuild.build({ + ...common, + ...runtimeCommon, + outfile: 'build/runtime/index-node.js', + minify: true, metafile: true, }); + esbuild.build({ + ...common, + ...runtimeCommon, + outfile: 'build/runtime/index.js', + external: ['katex'], + platform: 'neutral', + }); + esbuild.build({ ...common, entryPoints: ['src/react/index.ts'], @@ -38,11 +50,7 @@ const common = { external: ['react'], }); - esbuild.build({ - ...common, - entryPoints: ['src/plugin/index.ts'], - outfile: 'build/plugin/index.js', - platform: 'node', + const pluginCommon = { external: ['markdown-it', 'node:*'], define: { PACKAGE: JSON.stringify(require('../package.json').name), @@ -52,5 +60,22 @@ const common = { .map((file) => file.replace(/^runtime\//, '')), ), }, + }; + + esbuild.build({ + ...common, + ...pluginCommon, + entryPoints: ['src/plugin/index.ts'], + outfile: 'build/plugin/index.js', + platform: 'neutral', + external: [...pluginCommon.external, 'katex'], + }); + + esbuild.build({ + ...common, + ...pluginCommon, + entryPoints: ['src/plugin/index-node.ts'], + outfile: 'build/plugin/index-node.js', + platform: 'node', }); })(); diff --git a/package.json b/package.json index bbb3969..7c592ae 100644 --- a/package.json +++ b/package.json @@ -5,13 +5,35 @@ "main": "build/plugin/index.js", "types": "build/plugin/index.d.ts", "exports": { - ".": "./build/plugin/index.js", - "./plugin": "./build/plugin/index.js", - "./runtime": "./build/runtime/index.js", - "./runtime/styles": "./build/runtime/index.css", + ".": { + "node": "./build/plugin/index-node.js", + "default": "./build/plugin/index.js" + }, + "./plugin": { + "node": "./build/plugin/index-node.js", + "default": "./build/plugin/index.js" + }, + "./runtime": { + "node": "./build/runtime/index-node.js", + "default": "./build/runtime/index.js" + }, + "./runtime/styles": "./build/runtime/index-node.css", "./react": "./build/react/index.js", "./hooks": "./build/react/index.js" }, + "typesVersions": { + "*": { + "index.d.ts": [ + "./build/plugin/index.d.ts" + ], + "plugin": [ + "./build/plugin/index.d.ts" + ], + "runtime": [ + "./build/runtime/index.d.ts" + ] + } + }, "scripts": { "build": "run-p build:*", "build:js": "./esbuild/build.js",