From d799aa26307bed19d8de0d3bbf2167d153a806c8 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Fri, 24 Jun 2022 19:35:11 +0200 Subject: [PATCH] commonjs and esm in paralle/hybrid Signed-off-by: Jan Kowalleck --- .npmignore | 2 + package.json | 23 +++++--- scripts/rename-dist.js | 56 ++++++++++++++++++++ tsconfig.json | 2 +- tsconfig.node.commonjs.json | 10 ++++ tsconfig.node.json => tsconfig.node.esm.json | 4 +- tsconfig.web.json | 5 +- 7 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 scripts/rename-dist.js create mode 100644 tsconfig.node.commonjs.json rename tsconfig.node.json => tsconfig.node.esm.json (63%) diff --git a/.npmignore b/.npmignore index add453b08..00865523b 100644 --- a/.npmignore +++ b/.npmignore @@ -171,5 +171,7 @@ dist /examples/ +/scripts/ + /reports/ /CI_reports/ diff --git a/package.json b/package.json index 1e12df1c9..775ca7c48 100644 --- a/package.json +++ b/package.json @@ -58,14 +58,18 @@ "webpack-cli": "4.10.0", "xmlbuilder2": "^3.0.2" }, - "browser": "./dist.web/lib.js", + "browser": "./dist/web/lib.js", "types": "./src/index.node.ts", - "main": "./dist.node/index.node.js", - "exports": "./dist.node/index.node.js", + "main": "./dist.node.commonjs/index.node.cjs", + "exports": { + ".": { + "import": "./dist.node.esm/index.node.mjs", + "require": "./dist.node.commonjs/index.node.cjs" + } + }, "directories": { "doc": "./docs", "src": "./src", - "lib": "./dist.node", "test": "./tests", "example": "./examples" }, @@ -73,9 +77,14 @@ "prepublish": "npm run build", "prepublishOnly": "npm run build", "lint": "tsc --noEmit", - "build": "run-p --aggregate-output -l build:*", - "prebuild:node": "node -r fs -e 'fs.rmSync(\"dist.node\",{recursive:true,force:true})'", - "build:node": "tsc -b ./tsconfig.node.json", + "build": "run-p --aggregate-output -lc build:*", + "build:node": "run-p --aggregate-output -lc build:node:*", + "prebuild:node:commonjs": "node -r fs -e 'fs.rmSync(\"dist.node.commonjs\",{recursive:true,force:true})'", + "build:node:commonjs": "tsc -b ./tsconfig.node.commonjs.json", + "postbuild:node:commonjs": "node scripts/rename-dist.js dist.node.commonjs cjs", + "prebuild:node:esm": "node -r fs -e 'fs.rmSync(\"dist.node.esm\",{recursive:true,force:true})'", + "build:node:esm": "tsc -b ./tsconfig.node.esm.json", + "postbuild:node:esm": "node scripts/rename-dist.js dist.node.esm mjs", "prebuild:web": "node -r fs -e 'fs.rmSync(\"dist.web\",{recursive:true,force:true})'", "build:web": "webpack build", "cs-fix": "eslint --fix .", diff --git a/scripts/rename-dist.js b/scripts/rename-dist.js new file mode 100644 index 000000000..7d81b945e --- /dev/null +++ b/scripts/rename-dist.js @@ -0,0 +1,56 @@ +'use strict' +/*! +This file is part of CycloneDX JavaScript Library. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +SPDX-License-Identifier: Apache-2.0 +Copyright (c) OWASP Foundation. All Rights Reserved. +*/ + +const { statSync, renameSync, readdirSync } = require('fs') +const { resolve } = require('path') + +/** + * @param {string} dir + */ +function replace (dir) { + // console.log('>> ', dir) + for (const _ of readdirSync(dir)) { + const entry = resolve(dir, _) + const stats = statSync(entry) + if (stats.isDirectory()) { + replace(entry) + } else if (stats.isFile()) { + if (srcExtRE.test(entry)) { + const renamed = entry.replace(srcExtRE, `.${targetExt}`) + renameSync(entry, renamed) + // console.log(entry, ' -> ', renamed) + } + } + } +} + +const srcExt = 'js' +const dir = process.argv[2] +const targetExt = process.argv[3] + +// console.log('dir', dir) +// console.log('targetExt', targetExt) + +if (!dir) { process.exit(1) } +if (!targetExt) { process.exit(2) } + +const srcExtRE = new RegExp(`\\.${srcExt}$`, 'i') + +replace(resolve(process.cwd(), dir)) diff --git a/tsconfig.json b/tsconfig.json index 302d74b59..36fb5038e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,7 +27,7 @@ /* Modules */ /* check compat: https://node.green/ */ - "module": "CommonJS", /* Specify what module code is generated. */ + // "module": "CommonJS", /* Specify what module code is generated. */ "rootDir": "src", /* Specify the root folder within your source files. */ "moduleResolution": "Node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ diff --git a/tsconfig.node.commonjs.json b/tsconfig.node.commonjs.json new file mode 100644 index 000000000..80086e5c4 --- /dev/null +++ b/tsconfig.node.commonjs.json @@ -0,0 +1,10 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "./tsconfig.json", + "files": ["src/index.node.ts"], + "compilerOptions": { + "module": "CommonJS", + "moduleResolution": "Node", + "outDir": "./dist.node.commonjs/" + } +} diff --git a/tsconfig.node.json b/tsconfig.node.esm.json similarity index 63% rename from tsconfig.node.json rename to tsconfig.node.esm.json index 04eb6fc78..c1c95b683 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.esm.json @@ -3,6 +3,8 @@ "extends": "./tsconfig.json", "files": ["src/index.node.ts"], "compilerOptions": { - "outDir": "./dist.node/" + "module": "ES6", + "moduleResolution": "Node", + "outDir": "./dist.node.esm/" } } diff --git a/tsconfig.web.json b/tsconfig.web.json index 417454f8b..7699b259b 100644 --- a/tsconfig.web.json +++ b/tsconfig.web.json @@ -1,5 +1,8 @@ { "$schema": "https://json.schemastore.org/tsconfig", "extends": "./tsconfig.json", - "files": ["src/index.web.ts"] + "files": ["src/index.web.ts"], + "compilerOptions": { + "module": "CommonJS" + } }