From f058c8ac829c8a85587f7c10ad3ecb6662c79d58 Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Fri, 8 Oct 2021 02:32:02 +0300 Subject: [PATCH] build: complete build system --- .eslintignore | 2 +- .gitignore | 4 ++-- MIGRATION.md | 8 ++++---- package.json | 2 +- rollup.config.js | 2 +- scripts/build.js | 13 +++++++++++-- scripts/libraries.js | 12 ++++++++++++ scripts/source.package.js | 7 ++++--- tsconfig.build.json | 4 ++-- 9 files changed, 38 insertions(+), 16 deletions(-) diff --git a/.eslintignore b/.eslintignore index 85d76c82..a95854f8 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,3 @@ *.test.ts integration -dist +dist/** diff --git a/.gitignore b/.gitignore index 789896a8..27aeab20 100644 --- a/.gitignore +++ b/.gitignore @@ -124,7 +124,7 @@ typings/ .nuxt # rollup.config.js default build output -dist/ +node_modules/dist/ # Uncomment the public line if your project uses Gatsby # https://nextjs.org/blog/next-9-1#public-directory-support @@ -179,5 +179,5 @@ $RECYCLE.BIN/ # End of https://www.gitignore.io/api/node,windows,linux,macos -dist .idea +dist diff --git a/MIGRATION.md b/MIGRATION.md index f278dc40..56b862fe 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -92,7 +92,7 @@ const received = splitMap({ cases: { firstName: (string) => string.split(' ')[0], // string | undefined lastName: (string) => string.split(' ')[1], // string | undefined -}); + }); ``` - First argument should be `source` @@ -340,10 +340,10 @@ const source = spread({ ``` 1. If you have two arguments: - - First argument should be `source` in object - - Second argument should be `targets` + - First argument should be `source` in object + - Second argument should be `targets` 1. If only one argument: - - Wrap it to object and assign to `targets` + - Wrap it to object and assign to `targets` https://github.com/sergeysova/patronum/pull/61 diff --git a/package.json b/package.json index cb5770ce..82c85cec 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "cleanup": "rm -rf dist && mkdir dist", "build:indexes": "node scripts/build.js", "build:types": "tsc --project ./tsconfig.build.json", - "build:commonjs": "yarn babel --out-dir ./dist src -x .ts --ignore '**/*.test.ts','**/*.d.ts'", + "build:commonjs": "yarn babel --verbose --out-dir dist src -x .ts --ignore '**/*.test.ts','**/*.d.ts'", "build:mjs": "ESMODULES=true yarn build:commonjs --out-file-extension .mjs", "build:bundles": "rollup -c", "build": "run-s cleanup build:indexes build:types build:commonjs build:mjs build:bundles", diff --git a/rollup.config.js b/rollup.config.js index 5488f3c9..fb8eba7d 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,7 +5,7 @@ const commonjs = require('@rollup/plugin-commonjs'); const plugins = [ nodeResolve({ jsnext: true, skip: ['effector'], extensions: ['.js', '.mjs'] }), commonjs({ extensions: ['.js', '.mjs'] }), - // terser({}), + terser({}), ]; const input = 'dist/index.js'; diff --git a/scripts/build.js b/scripts/build.js index ed95e9d0..16b9ea03 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -8,6 +8,7 @@ const { createDistribution, writeFile, createMjsIndex, + createExportsMap, } = require('./libraries'); const packageJson = require('./source.package.js'); @@ -20,14 +21,22 @@ async function main() { const directory = await createDistribution('./dist'); await directory.copyList('./src', staticFiles); - - pkg.files.push(...staticFiles); + await directory.copyList('./', ['README.md', 'MIGRATION.md', 'LICENSE']); const found = await globby(`./src/*/${packageMarker}`); const names = found.map((name) => name.replace(`/${packageMarker}`, '').replace('./src/', ''), ); + pkg.exports = { + '.': { + require: './index.js', + import: './index.mjs', + default: './index.mjs', + }, + ...createExportsMap(names), + }; + await directory.write('index.js', createCommonJsIndex(names)); await directory.write('index.mjs', createMjsIndex(names)); await directory.write('index.d.ts', createTypingsIndex(names)); diff --git a/scripts/libraries.js b/scripts/libraries.js index 65d87680..6575a0bd 100644 --- a/scripts/libraries.js +++ b/scripts/libraries.js @@ -41,6 +41,17 @@ function createFactoriesJson(library, names) { return { factories, mapping }; } +function createExportsMap(names) { + const object = {}; + names.forEach((name) => { + object[`./${name}`] = { + require: `./${name}/index.js`, + import: `./${name}/index.mjs`, + }; + }); + return object; +} + async function createDistribution(dir) { return { write: (path, content) => writeFile(`${dir}/${path}`, content), @@ -56,6 +67,7 @@ module.exports = { createCommonJsIndex, createMjsIndex, createTypingsIndex, + createExportsMap, createFactoriesJson, createDistribution, }; diff --git a/scripts/source.package.js b/scripts/source.package.js index 30190876..04d3eeae 100644 --- a/scripts/source.package.js +++ b/scripts/source.package.js @@ -2,10 +2,11 @@ module.exports = () => ({ name: 'patronum', version: '1.2.0-next.2', description: '☄️ Effector utility library delivering modularity and convenience', - main: 'dist/patronum.cjs.js', + type: 'module', + main: 'patronum.cjs.js', types: 'index.d.ts', - browser: 'dist/patronum.umd.js', - files: ['*/*.d.ts', '*/*.js', '*/*.mjs', 'dist'], + module: 'patronum.mjs', + browser: 'patronum.umd.js', repository: { type: 'git', url: 'git+https://github.com/effector/patronum.git', diff --git a/tsconfig.build.json b/tsconfig.build.json index e3696f29..20077fd2 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -2,10 +2,10 @@ "extends": "./tsconfig.json", "compilerOptions": { "emitDeclarationOnly": true, - "declarationDir": "./dist", + "declarationDir": "./node_modules/dist", "declaration": true, "rootDir": "./src", - "outDir": "./dist", + "outDir": "./node_modules/dist", "allowJs": false }, "exclude": [