From 5e562377ddec27b7eb2db530939b34e841bdd94b Mon Sep 17 00:00:00 2001 From: maxeljkin Date: Tue, 29 Aug 2023 08:58:31 +0300 Subject: [PATCH] feat: add dual packaging --- .eslintrc | 2 +- package.json | 2 +- packages/classname/index.cjs | 7 +++++++ packages/classname/index.js | 7 ------- packages/classname/package.json | 14 +++++++++++++- packages/classnames/index.cjs | 7 +++++++ packages/classnames/index.js | 7 ------- packages/classnames/package.json | 14 +++++++++++++- packages/core/index.cjs | 7 +++++++ packages/core/index.js | 7 ------- packages/core/package.json | 14 +++++++++++++- packages/di/{index.js => index.cjs} | 0 packages/di/package.json | 14 +++++++++++++- scripts/rollup/build.js | 20 ++++++++++++++++---- 14 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 packages/classname/index.cjs delete mode 100644 packages/classname/index.js create mode 100644 packages/classnames/index.cjs delete mode 100644 packages/classnames/index.js create mode 100644 packages/core/index.cjs delete mode 100644 packages/core/index.js rename packages/di/{index.js => index.cjs} (100%) diff --git a/.eslintrc b/.eslintrc index 92538229..526aaf5c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -210,7 +210,7 @@ } }, { - "files": ["*.js"], + "files": ["*.{js,cjs}"], "rules": { "strict": 0 } diff --git a/package.json b/package.json index 27f13851..315af101 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "scripts": { "bootstrap": "lerna bootstrap --no-ci", "build": "lerna run build --concurrency=1", - "lint": "eslint --ext .js,.ts,.tsx .", + "lint": "eslint --ext .js,.cjs,.ts,.tsx .", "postinstall": "npm run bootstrap", "publish:next": "lerna publish --canary --preid dev --npm-tag next --no-git-tag-version", "unit:coverage": "npm run unit -- --coverage", diff --git a/packages/classname/index.cjs b/packages/classname/index.cjs new file mode 100644 index 00000000..6c5763dc --- /dev/null +++ b/packages/classname/index.cjs @@ -0,0 +1,7 @@ +'use strict' + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./build/classname.production.min.cjs') +} else { + module.exports = require('./build/classname.development.cjs') +} diff --git a/packages/classname/index.js b/packages/classname/index.js deleted file mode 100644 index 8d12de5f..00000000 --- a/packages/classname/index.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -if (process.env.NODE_ENV === 'production') { - module.exports = require('./build/classname.production.min.js') -} else { - module.exports = require('./build/classname.development.js') -} diff --git a/packages/classname/package.json b/packages/classname/package.json index 80807630..b3b4600a 100644 --- a/packages/classname/package.json +++ b/packages/classname/package.json @@ -5,8 +5,20 @@ "homepage": "https://github.com/bem/bem-react/tree/master/packages/classname", "repository": "https://github.com/bem/bem-react", "keywords": ["bem", "naming", "classes", "notation", "core"], - "main": "index.js", + "main": "index.cjs", "typings": "classname.d.ts", + "exports": { + "development": { + "require": "./build/classname.development.cjs", + "module": "./build/classname.development.mjs" + }, + "production": { + "require": "./build/classname.production.min.cjs", + "module": "./build/classname.production.min.mjs" + }, + "require": "./index.cjs", + "default": "./build/classname.production.min.mjs" + }, "publishConfig": { "access": "public" }, diff --git a/packages/classnames/index.cjs b/packages/classnames/index.cjs new file mode 100644 index 00000000..fbc0d395 --- /dev/null +++ b/packages/classnames/index.cjs @@ -0,0 +1,7 @@ +'use strict' + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./build/classnames.production.min.cjs') +} else { + module.exports = require('./build/classnames.development.cjs') +} diff --git a/packages/classnames/index.js b/packages/classnames/index.js deleted file mode 100644 index 5c2d37c3..00000000 --- a/packages/classnames/index.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -if (process.env.NODE_ENV === 'production') { - module.exports = require('./build/classnames.production.min.js') -} else { - module.exports = require('./build/classnames.development.js') -} diff --git a/packages/classnames/package.json b/packages/classnames/package.json index fb75bc67..65c96bf9 100644 --- a/packages/classnames/package.json +++ b/packages/classnames/package.json @@ -5,8 +5,20 @@ "homepage": "https://github.com/bem/bem-react/tree/master/packages/classnames", "repository": "https://github.com/bem/bem-react", "keywords": ["classes", "merge"], - "main": "index.js", + "main": "index.cjs", "typings": "classnames.d.ts", + "exports": { + "development": { + "require": "./build/classnames.development.cjs", + "module": "./build/classnames.development.mjs" + }, + "production": { + "require": "./build/classnames.production.min.cjs", + "module": "./build/classnames.production.min.mjs" + }, + "require": "./index.cjs", + "default": "./build/classnames.production.min.mjs" + }, "publishConfig": { "access": "public" }, diff --git a/packages/core/index.cjs b/packages/core/index.cjs new file mode 100644 index 00000000..6f939f93 --- /dev/null +++ b/packages/core/index.cjs @@ -0,0 +1,7 @@ +'use strict' + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./build/core.production.min.cjs') +} else { + module.exports = require('./build/core.development.cjs') +} diff --git a/packages/core/index.js b/packages/core/index.js deleted file mode 100644 index 6c403fb0..00000000 --- a/packages/core/index.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -if (process.env.NODE_ENV === 'production') { - module.exports = require('./build/core.production.min.js') -} else { - module.exports = require('./build/core.development.js') -} diff --git a/packages/core/package.json b/packages/core/package.json index ab40ca58..d96206d4 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -5,8 +5,20 @@ "homepage": "https://github.com/bem/bem-react/tree/master/packages/core", "repository": "https://github.com/bem/bem-react", "keywords": ["bem", "modifier", "withBemMod", "core"], - "main": "index.js", + "main": "index.cjs", "typings": "core.d.ts", + "exports": { + "development": { + "require": "./build/core.development.cjs", + "module": "./build/core.development.mjs" + }, + "production": { + "require": "./build/core.production.min.cjs", + "module": "./build/core.production.min.mjs" + }, + "require": "./index.cjs", + "default": "./build/core.production.min.mjs" + }, "publishConfig": { "access": "public" }, diff --git a/packages/di/index.js b/packages/di/index.cjs similarity index 100% rename from packages/di/index.js rename to packages/di/index.cjs diff --git a/packages/di/package.json b/packages/di/package.json index b90c28c6..63bca4fb 100644 --- a/packages/di/package.json +++ b/packages/di/package.json @@ -5,8 +5,20 @@ "homepage": "https://github.com/bem/bem-react/tree/master/packages/di", "repository": "https://github.com/bem/bem-react", "keywords": ["bem", "level", "dependency", "di", "dependency injection", "react"], - "main": "index.js", + "main": "index.cjs", "typings": "di.d.ts", + "exports": { + "development": { + "require": "./build/di.development.cjs", + "module": "./build/di.development.mjs" + }, + "production": { + "require": "./build/di.production.min.cjs", + "module": "./build/di.production.min.mjs" + }, + "require": "./index.cjs", + "default": "./build/di.production.min.mjs" + }, "publishConfig": { "access": "public" }, diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index c4d102e0..3c27cacd 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -78,19 +78,31 @@ function getPackageData(packagePath) { inputFile, outputs: [ { - outputFile: resolve(buildPath, `${packageName}.production.min.js`), + outputFile: resolve(buildPath, `${packageName}.production.min.cjs`), isProduction: true, + isESM: false, }, { - outputFile: resolve(buildPath, `${packageName}.development.js`), + outputFile: resolve(buildPath, `${packageName}.production.min.mjs`), + isProduction: true, + isESM: true, + }, + { + outputFile: resolve(buildPath, `${packageName}.development.cjs`), + isProduction: false, + isESM: false, + }, + { + outputFile: resolve(buildPath, `${packageName}.development.mjs`), isProduction: false, + isESM: true, }, ], } } function build({ packageName, tsConfigPath, externalDependencies, inputFile, outputs }) { - outputs.forEach(async ({ outputFile, isProduction }) => { + outputs.forEach(async ({ outputFile, isProduction, isESM }) => { const inputConfig = { input: inputFile, plugins: getPlugins({ isProduction, tsConfigPath }), @@ -99,7 +111,7 @@ function build({ packageName, tsConfigPath, externalDependencies, inputFile, out const outputConfig = { file: outputFile, - format: 'cjs', + format: isESM ? 'es' : 'cjs', interop: false, }