From 6b420429f4c947bba86a4ef7480db9425bd9c756 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Tue, 2 Jan 2024 16:31:15 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7(packaged)=20Move=20build=20chain?= =?UTF-8?q?=20to=20ESM=20(#4588)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The build chain of `@fast-check/packaged` has been CommonJS-based since day 1. With ESM moving forward in the ecosystem, it's time to move ourselves to the new standard and adapt our build chains to ESM. Unfortunately it may have some subtle impacts on our users as our package will not be a CJS one offering a ESM fallback anymore. I will rather be the opposite: an ESM package with a fallback to CJS. It implies that we moved ESM related files closer to the root of the package (we could have kept them in esm/) and moved the CJS ones further in the file structure (we had to move them). Another subtle impact is that it would impose our users to run at least Node ≥12.17.0. As such we consider it as a breaking change. --- .yarn/versions/5da63084.yml | 2 + packages/packaged/bin/packaged.js | 60 ++++++++++--------- packages/packaged/package.json | 8 +-- packages/packaged/test-types/empty.ts | 2 + .../packaged/test-types/{main.ts => main.mts} | 0 5 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 .yarn/versions/5da63084.yml create mode 100644 packages/packaged/test-types/empty.ts rename packages/packaged/test-types/{main.ts => main.mts} (100%) diff --git a/.yarn/versions/5da63084.yml b/.yarn/versions/5da63084.yml new file mode 100644 index 00000000..14576c16 --- /dev/null +++ b/.yarn/versions/5da63084.yml @@ -0,0 +1,2 @@ +releases: + "@fast-check/packaged": minor diff --git a/packages/packaged/bin/packaged.js b/packages/packaged/bin/packaged.js index 411414d1..28f808b1 100755 --- a/packages/packaged/bin/packaged.js +++ b/packages/packaged/bin/packaged.js @@ -1,31 +1,33 @@ -/* global process, console, require */ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { removeNonPublishedFiles } = require('../lib/packaged.js'); +/* global process, console */ +import { removeNonPublishedFiles } from '../lib/packaged.js'; -const args = process.argv.slice(2); -const help = args.includes('--help') || args.includes('-h'); -if (help) { - console.log('Usages:'); - console.log('- packaged'); - console.log(' Drop any file in the current directory that will not be part of the package'); - console.log(' if published to npm registry'); - console.log('- packaged --dry-run'); - console.log(' No removal, just printing'); - console.log('- packaged --keep-node-modules'); - console.log(' Keep root level node_modules if any'); - return; -} -const dryRun = args.includes('--dry-run'); -const keepNodeModules = args.includes('--keep-node-modules'); -removeNonPublishedFiles('.', { dryRun, keepNodeModules }).then((out) => { - if (dryRun) { - console.log('Those files would have been kept:'); - for (const k of out.kept) { - console.log(`- ${k}`); - } - console.log('Those files would have been removed:'); - for (const r of out.removed) { - console.log(`- ${r}`); - } +function run(args) { + const help = args.includes('--help') || args.includes('-h'); + if (help) { + console.log('Usages:'); + console.log('- packaged'); + console.log(' Drop any file in the current directory that will not be part of the package'); + console.log(' if published to npm registry'); + console.log('- packaged --dry-run'); + console.log(' No removal, just printing'); + console.log('- packaged --keep-node-modules'); + console.log(' Keep root level node_modules if any'); + return; } -}); + const dryRun = args.includes('--dry-run'); + const keepNodeModules = args.includes('--keep-node-modules'); + removeNonPublishedFiles('.', { dryRun, keepNodeModules }).then((out) => { + if (dryRun) { + console.log('Those files would have been kept:'); + for (const k of out.kept) { + console.log(`- ${k}`); + } + console.log('Those files would have been removed:'); + for (const r of out.removed) { + console.log(`- ${r}`); + } + } + }); +} + +run(process.argv.slice(2)); diff --git a/packages/packaged/package.json b/packages/packaged/package.json index f88e1f1c..770b085e 100644 --- a/packages/packaged/package.json +++ b/packages/packaged/package.json @@ -2,7 +2,7 @@ "name": "@fast-check/packaged", "description": "Utility package removing any files that will not be part of the final bundle", "version": "0.2.0", - "type": "commonjs", + "type": "module", "main": "lib/packaged.js", "types": "lib/packaged.d.ts", "bin": "./bin/packaged.js", @@ -12,11 +12,11 @@ ], "sideEffects": false, "scripts": { - "build": "yarn build:publish-cjs && yarn build:publish-types", + "build": "yarn build:publish-esm && yarn build:publish-types", "build-ci": "yarn build", "build:publish-types": "tsc -p tsconfig.publish.types.json", - "build:publish-cjs": "tsc -p tsconfig.publish.json", - "test": "jest --verbose", + "build:publish-esm": "tsc -p tsconfig.publish.json --module es2015 --moduleResolution node", + "test": "yarn node --experimental-vm-modules $(yarn bin jest) --verbose", "typecheck": "tsc --noEmit" }, "repository": { diff --git a/packages/packaged/test-types/empty.ts b/packages/packaged/test-types/empty.ts new file mode 100644 index 00000000..a617e52d --- /dev/null +++ b/packages/packaged/test-types/empty.ts @@ -0,0 +1,2 @@ +// Just to avoid empty mv from complaining in CI. +// Alternatively we could have muted the complaint by passsing some flags to mv but it would have hidden a potentially legit error. diff --git a/packages/packaged/test-types/main.ts b/packages/packaged/test-types/main.mts similarity index 100% rename from packages/packaged/test-types/main.ts rename to packages/packaged/test-types/main.mts -- 2.51.2