diff --git a/.gitignore b/.gitignore index 2ccbe465..b0a5c349 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules/ +/dist/ diff --git a/npm-scripts.js b/npm-scripts.js index 7414f889..0ff5de2f 100644 --- a/npm-scripts.js +++ b/npm-scripts.js @@ -1,8 +1,9 @@ +const esbuild = require("esbuild"); const fs = require('fs'); const path = require('path'); const process = require('process'); const { execSync } = require('child_process'); -const { version } = require('./package.json'); +const pkg = require('./package.json'); const task = process.argv.slice(2).join(' '); @@ -11,42 +12,49 @@ const ESLINT_PATHS = [ 'src', 'test' ].join(' '); // eslint-disable-next-line no-console console.log(`npm-scripts.js [INFO] running task "${task}"`); -switch (task) -{ - case 'grammar': { - grammar(); - - break; - } - - case 'lint': { - lint(); - - break; - } - - case 'test': { - test(); - - break; - } - - case 'release': { - lint(); - test(); - executeCmd(`git commit -am '${version}'`); - executeCmd(`git tag -a ${version} -m '${version}'`); - executeCmd('git push origin master && git push origin --tags'); - executeCmd('npm publish'); - - // eslint-disable-next-line no-console - console.log('update tryit-jssip and JsSIP website'); - - break; - } +void run(); - default: { - throw new TypeError(`unknown task "${task}"`); +async function run() { + switch (task) + { + case 'grammar': { + grammar(); + break; + } + + case 'lint': { + lint(); + break; + } + + case 'test': { + test(); + break; + } + + case 'build': { + await build(true /* minify */); + await build(false /* minify */); + + break; + } + + case 'release': { + lint(); + test(); + executeCmd(`git commit -am '${pkg.version}'`); + executeCmd(`git tag -a ${pkg.version} -m '${pkg.version}'`); + executeCmd('git push origin master && git push origin --tags'); + executeCmd('npm publish'); + + // eslint-disable-next-line no-console + console.log('update tryit-jssip and JsSIP website'); + break; + } + + default: { + throw new TypeError(`unknown task "${task}"`); + } } } @@ -93,6 +101,41 @@ function grammar() logInfo('grammar done'); } +// Build sources into a file for publishing. +async function build(minify = true) { + const entry = path.resolve("src/JsSIP.js"); + const outfile = path.resolve("./dist", `jssip${minify ? '.min' : ''}.js`); + const banner = ` + /* + * JsSIP ${pkg.version} + * ${pkg.description} + * Copyright: 2012-${new Date().getFullYear()} ${pkg.contributors.join(' ')} + * Homepage: ${pkg.homepage} + * License: ${pkg.license} + */`; + + await esbuild.build({ + entryPoints: [entry], + outfile, + bundle: true, + minify, + sourcemap: false, + // https://esbuild.github.io/api/#global-name. + format: "iife", + globalName: "JsSIP", + platform: "browser", + target: ["es2015"], + // Make the generated output a single line. + supported: { + "template-literal": false, + }, + // Add banner. + banner: { + js: banner, + }, + }); +} + function executeCmd(command) { // eslint-disable-next-line no-console diff --git a/package.json b/package.json index e7c6c147..798e9ca7 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "@eslint/js": "^9.39.2", "@types/debug": "^4.1.12", "@types/events": "^3.0.3", + "esbuild": "^0.27.2", "eslint": "^9.39.1", "eslint-plugin-jest": "^29.12.1", "globals": "^17.0.0", @@ -45,6 +46,7 @@ "scripts": { "lint": "node npm-scripts.js lint", "test": "node npm-scripts.js test", + "build": "node npm-scripts.js build", "release": "node npm-scripts.js release" } }