From e7b4390af64dd91bb2a0dcda3fb126a11ff97d44 Mon Sep 17 00:00:00 2001 From: Mark Silverwood Date: Fri, 27 Jan 2023 13:55:01 +0000 Subject: [PATCH] add cjs build versions --- .size-limit.js | 7 ++++++- index.cjs | 4 ++-- package.json | 12 ++++++------ rollup.config.js | 29 +++++++++++++++++------------ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/.size-limit.js b/.size-limit.js index 13b467424..d7d93853d 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -1,9 +1,14 @@ // eslint-env node module.exports = [ + { + name: 'CJS', + path: 'dist/lightweight-charts.production.cjs', + limit: '44.1 KB', + }, { name: 'ESM', - path: 'dist/lightweight-charts.esm.production.js', + path: 'dist/lightweight-charts.production.mjs', limit: '44.0 KB', }, { diff --git a/index.cjs b/index.cjs index 797206bac..7e397576c 100644 --- a/index.cjs +++ b/index.cjs @@ -1,7 +1,7 @@ 'use strict'; if (process.env.NODE_ENV === 'production') { - module.exports = require('./dist/lightweight-charts.esm.production.js'); + module.exports = require('./dist/lightweight-charts.production.cjs'); } else { - module.exports = require('./dist/lightweight-charts.esm.development.js'); + module.exports = require('./dist/lightweight-charts.development.cjs'); } diff --git a/package.json b/package.json index c4b8f3a94..842a906d7 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "type": "git", "url": "https://github.com/tradingview/lightweight-charts.git" }, - "module": "dist/lightweight-charts.esm.production.js", + "module": "dist/lightweight-charts.production.mjs", "main": "index.cjs", "typings": "dist/typings.d.ts", "exports": { @@ -20,17 +20,17 @@ ".": { "development": { "types": "./dist/typings.d.ts", - "import": "./dist/lightweight-charts.esm.development.js", - "require": "./dist/lightweight-charts.esm.development.js" + "import": "./dist/lightweight-charts.development.mjs", + "require": "./dist/lightweight-charts.development.cjs" }, "production": { "types": "./dist/typings.d.ts", - "import": "./dist/lightweight-charts.esm.production.js", - "require": "./dist/lightweight-charts.esm.production.js" + "import": "./dist/lightweight-charts.production.mjs", + "require": "./dist/lightweight-charts.production.cjs" }, "default": { "types": "./dist/typings.d.ts", - "import": "./dist/lightweight-charts.esm.production.js", + "import": "./dist/lightweight-charts.production.mjs", "require": "./index.cjs" } } diff --git a/rollup.config.js b/rollup.config.js index 91d57a10b..37a518b71 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -15,16 +15,19 @@ function getCurrentVersion() { const currentVersion = getCurrentVersion(); -function getConfig(inputFile, type, isProd) { - const isModular = type === 'module'; - const suffix = isModular ? 'esm' : 'standalone'; +function getConfig(inputFile, format, isProd) { + const isStandalone = format === 'iife'; const mode = isProd ? 'production' : 'development'; - + const extension = { + cjs: 'cjs', + esm: 'mjs', + iife: 'js', + }[format]; const config = { input: inputFile, output: { - format: isModular ? 'esm' : 'iife', - file: `./dist/lightweight-charts.${suffix}.${mode}.js`, + format, + file: `./dist/lightweight-charts${isStandalone ? '.standalone' : ''}.${mode}.${extension}`, banner: ` /*! * @license @@ -50,28 +53,30 @@ function getConfig(inputFile, type, isProd) { inline_script: true, }, mangle: { - module: (type === 'module'), + module: (format === 'esm' || format === 'cjs'), properties: { regex: /^_(private|internal)_/, }, }, }), ], - external: id => isModular && /^fancy-canvas(\/.+)?$/.test(id), + external: id => !isStandalone && /^fancy-canvas(\/.+)?$/.test(id), }; return config; } const configs = [ - getConfig('./lib/prod/src/index.js', 'module', false), - getConfig('./lib/prod/src/standalone.js', 'standalone', false), + getConfig('./lib/prod/src/index.js', 'esm', false), + getConfig('./lib/prod/src/index.js', 'cjs', false), + getConfig('./lib/prod/src/standalone.js', 'iife', false), ]; if (process.env.NODE_ENV === 'production') { configs.push( - getConfig('./lib/prod/src/index.js', 'module', true), - getConfig('./lib/prod/src/standalone.js', 'standalone', true) + getConfig('./lib/prod/src/index.js', 'esm', true), + getConfig('./lib/prod/src/index.js', 'cjs', true), + getConfig('./lib/prod/src/standalone.js', 'iife', true) ); }