Skip to content

Commit

Permalink
Refactor build process and add ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoofsKelvin committed Oct 30, 2021
1 parent 64afbbf commit 664cc35
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 13 deletions.
File renamed without changes.
Binary file modified .yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts",
"mainEntryPointFilePath": "<projectFolder>/lib/cjs/index.d.ts",
"apiReport": {
"enabled": true,
"reportFolder": "<projectFolder>/api"
Expand Down
8 changes: 4 additions & 4 deletions jest.config.js → jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @typedef {import('ts-jest/dist/types')} */
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['src'],
};
preset: "ts-jest",
testEnvironment: "node",
roots: ["src"],
};
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "ip-matching",
"version": "2.1.2",
"type": "commonjs",
"main": "lib/index.js",
"types": "api/index.d.ts",
"type": "module",
"main": "./lib/cjs/index.js",
"exports": "./lib/cjs/index.js",
"module": "./lib/esm/index.mjs",
"types": "./api/index.d.ts",
"description": "Utilities for IPv4/IPv6 ranges/subnets/masks parsing/matching, string representations, ...",
"devDependencies": {
"@microsoft/api-extractor": "^7.18.1",
Expand All @@ -22,15 +24,15 @@
"scripts": {
"compile": "tsc",
"compile:watch": "tsc -w",
"compile:esm": "tsc -p tsconfig.esm.json",
"test": "jest",
"test:watch": "jest --watch",
"api": "api-extractor run",
"api:local": "api-extractor run -l",
"api:watch": "nodemon -w api-extractor.json -w lib -e d.ts --exec yarn api:local",
"lint": "eslint . --ext .ts --format stylish",
"lint:watch": "nodemon -w .eslintignore -w .eslintrc.js -w src -e ts --exec yarn eslint . --ext .ts --format stylish",
"clean": "rimraf lib",
"prepack": "yarn clean && yarn compile && yarn lint && yarn test && yarn api"
"prepack": "yarn node ./prepack.cjs"
},
"author": {
"name": "Kelvin Schoofs",
Expand Down Expand Up @@ -58,8 +60,8 @@
],
"files": [
"api/",
"lib/*.js",
"!lib/*.test.js",
"lib/**/*.js",
"!lib/**/*.test.js",
"README.md",
"CHANGELOG.md"
],
Expand Down
59 changes: 59 additions & 0 deletions prepack.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const rimraf = require("rimraf");
const child_process = require("child_process");
const readline = require("readline");

async function spawn(prefix, cmd, args) {
console.log(`[${prefix}] Process started`);
const cp = child_process.spawn(cmd, args, {
shell: true,
stdio: "pipe",
env: { ...process.env, FORCE_COLOR: "1" },
});
readline.createInterface({ input: cp.stdout }).on("line", (line) => {
console.log(`[${prefix}] ${line}`);
});
readline.createInterface({ input: cp.stderr }).on("line", (line) => {
console.error(`[${prefix}] ${line}`);
});
return new Promise((resolve, reject) => {
cp.once("error", reject);
cp.once("close", (code) => {
console.log(`[${prefix}] Process ended with status code ${code}`);
if (!code) return resolve();
reject(new Error(`Process ${prefix} exited with code ${code}`));
});
});
}

function clean() {
return new Promise((resolve, reject) =>
rimraf("lib", (err) => {
if (!err) return resolve();
reject(new Error(`Error during clean: ${err.stack || err}`));
})
);
}

async function build() {
// Start cleaning and linting
const cleanPromise = clean();
const lintPromise = spawn("Lint", "yarn", ["lint"]);

// Start compiling CJS and EMS after cleaning is done
const cjsPromise = cleanPromise.then(() => spawn("CJS", "yarn", ["compile"]));
const esmPromise = cleanPromise.then(() => spawn("ESM", "yarn", ["compile:esm"]));

// Start API after CJS is done (which also outputs types)
const apiPromise = cjsPromise.then(() => spawn("API", "yarn", ["api"]));

// Start testing after CJS and EMS are done
const testPromise = Promise.all([cjsPromise, esmPromise]).then(() => spawn("Test", "yarn", ["test"]));

// Wait for all unlinked promises to finish
await Promise.all([lintPromise, apiPromise, testPromise]);
}

build().catch((err) => {
console.error(err);
process.exit(1);
});
11 changes: 11 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"declaration": false,
"declarationMap": false,
"sourceMap": true,
"outDir": "lib/esm",
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "lib",
"outDir": "lib/cjs",
"rootDir": "src",
"strict": true,
"moduleResolution": "node"
Expand Down

0 comments on commit 664cc35

Please sign in to comment.