From 862984327a9af1c621700b9661ba9ee19d4751b5 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Fri, 25 Dec 2020 17:25:51 +0100 Subject: [PATCH] manual build via rollup (#109) --- .eslintignore | 1 + .prettierignore | 1 + package.json | 52 ++++++++++++++++++---- post-build.js | 29 ++++++++++++ rollup.config.js | 99 +++++++++++++++++++++++++++++++++++++++++ src/HandledError.ts | 3 ++ src/core/buildThunks.ts | 5 +-- src/core/module.ts | 3 +- src/createApi.ts | 3 +- src/retry.ts | 2 +- src/utils/index.ts | 1 - src/utils/isDev.ts | 1 - tsconfig.json | 5 ++- typesversions.js | 23 ---------- yarn.lock | 63 +++++++++++++++++++++++--- 15 files changed, 240 insertions(+), 51 deletions(-) create mode 100644 .eslintignore create mode 100644 .prettierignore create mode 100644 post-build.js create mode 100644 rollup.config.js create mode 100644 src/HandledError.ts delete mode 100644 src/utils/isDev.ts delete mode 100644 typesversions.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..3e221292 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +/dist \ No newline at end of file diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..3e221292 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +/dist \ No newline at end of file diff --git a/package.json b/package.json index 6722989a..8b35f74a 100644 --- a/package.json +++ b/package.json @@ -6,16 +6,16 @@ "public": true, "author": "Lenz Weber", "main": "dist/index.js", - "module": "dist/rtk-query.esm.js", + "module": "dist/esm/index.js", "sideEffects": false, - "types": "dist/index.d.ts", + "types": "dist/ts/index.d.ts", "typesVersions": { ">=4.1": { "dist": [ - "dist/min-4.1/index.d.ts" + "dist/ts-4.1/index.d.ts" ], - "dist/*": [ - "dist/min-4.1/*" + "dist/ts/*": [ + "dist/ts-4.1/*" ] } }, @@ -31,7 +31,7 @@ }, "scripts": { "start": "tsdx watch", - "build": "tsdx build && node typesversions.js", + "build": "rollup -c && node post-build.js", "test": "tsdx test", "lint": "tsdx lint", "prepare": "yarn build", @@ -62,15 +62,43 @@ }, "size-limit": [ { - "path": "dist/rtk-query.cjs.production.min.js", - "limit": "25 KB" + "name": "ESM full", + "path": "dist/esm/index.js", + "limit": "15 KB" + }, + { + "name": "createPureApi + setupListeners", + "path": "dist/esm/index.js", + "import": "{ createPureApi, setupListeners }" + }, + { + "name": "createApi + setupListeners", + "path": "dist/esm/index.js", + "import": "{ createApi, setupListeners }" + }, + { + "name": "fetchBaseQuery", + "path": "dist/esm/index.js", + "import": "{ fetchBaseQuery }" + }, + { + "name": "retry", + "path": "dist/esm/index.js", + "import": "{ retry }" + }, + { + "name": "ApiProvider", + "path": "dist/esm/index.js", + "import": "{ ApiProvider }" }, { - "path": "dist/rtk-query.esm.js", + "name": "CJS minfied", + "path": "dist/index.cjs.production.min.js", "limit": "25 KB" } ], "dependencies": { + "@babel/runtime": "^7.12.5", "immer": ">=8.0.0" }, "peerDependencies": { @@ -96,7 +124,11 @@ } }, "devDependencies": { + "@babel/plugin-transform-runtime": "^7.12.10", "@reduxjs/toolkit": "^1.5.0", + "@rollup/plugin-babel": "^5.2.2", + "@rollup/plugin-replace": "^2.3.4", + "@rollup/plugin-typescript": "^8.0.0", "@size-limit/preset-small-lib": "^4.6.0", "@testing-library/react": "^11.1.0", "@testing-library/react-hooks": "^3.4.2", @@ -113,6 +145,8 @@ "react-dom": "^16.14.0 || 17.0.0", "react-redux": "^7.2.1", "react-test-renderer": "^17.0.1", + "rollup": "^2.34.2", + "rollup-plugin-terser": "^7.0.2", "shelljs": "^0.8.4", "size-limit": "^4.6.0", "ts-node": "^9.0.0", diff --git a/post-build.js b/post-build.js new file mode 100644 index 00000000..566fc01b --- /dev/null +++ b/post-build.js @@ -0,0 +1,29 @@ +const fs = require('fs'); +const { cp, rm } = require('shelljs'); + +if (fs.existsSync('dist/ts-4.1')) { + rm('-r', 'dist/ts-4.1'); +} + +cp('-r', 'dist/ts/', 'dist/ts-4.1'); + +const stubTs41Types = ` +import { EndpointDefinitions } from './endpointDefinitions'; +export declare type TS41Hooks = unknown; +export {}; +`; + +fs.writeFileSync('dist/ts/ts41Types.d.ts', stubTs41Types); + +fs.writeFileSync( + 'dist/index.js', + ` +'use strict' + +if (process.env.NODE_ENV === 'production') { + module.exports = require('./index.cjs.production.min.js') +} else { + module.exports = require('./index.cjs.development.js') +} +` +); diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..a9253b07 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,99 @@ +import babel from '@rollup/plugin-babel'; +import typescript from '@rollup/plugin-typescript'; +import { terser } from 'rollup-plugin-terser'; +import replace from '@rollup/plugin-replace'; + +/** @type {import("rollup").RollupOptions} */ +const defaultConfig = { + input: 'src/index.ts', + external: [/@babel\/runtime/, /@reduxjs\/toolkit/, /react$/, /react-redux/, /immer/, /tslib/], + treeshake: { + propertyReadSideEffects: false, + }, +}; + +const defaultTerserOptions = { + output: { comments: false }, + compress: { + keep_infinity: true, + pure_getters: true, + passes: 10, + }, + ecma: 5, + warnings: true, +}; + +const defaultTsConfig = { declaration: false, declarationMap: false, target: 'ESNext', module: 'esnext' }; + +/** @type {import("rollup").RollupOptions} */ +const configs = [ + // ESM + { + ...defaultConfig, + output: [ + { + dir: 'dist/esm', + format: 'es', + sourcemap: true, + preserveModules: true, + }, + ], + plugins: [ + typescript(defaultTsConfig), + babel({ + exclude: 'node_modules/**', + extensions: ['.js', '.ts'], + babelHelpers: 'runtime', + presets: [ + [ + '@babel/preset-env', + { + targets: { node: true, browsers: ['defaults', 'not IE 11', 'maintained node versions'] }, + bugfixes: true, + loose: true, + }, + ], + ], + plugins: [['@babel/plugin-transform-runtime', { useESModules: true }]], + }), + ], + }, + // CJS: + ...withMinify((minified) => ({ + ...defaultConfig, + output: [ + { + dir: 'dist', + format: 'cjs', + sourcemap: true, + entryFileNames: minified ? '[name].cjs.production.min.js' : '[name].cjs.development.js', + }, + ], + plugins: [ + typescript( + minified + ? defaultTsConfig + : { ...defaultTsConfig, declarationDir: 'dist/ts', declaration: true, declarationMap: true } + ), + replace({ + values: { + 'process.env.NODE_ENV': JSON.stringify(minified ? 'production' : 'development'), + }, + }), + babel({ + exclude: 'node_modules/**', + extensions: ['.js', '.ts'], + babelHelpers: 'runtime', + presets: [['@babel/preset-env', { targets: { node: true, browsers: ['defaults'] } }]], + plugins: [['@babel/plugin-transform-runtime', { useESModules: false }]], + }), + ...(minified ? [terser({ ...defaultTerserOptions, toplevel: true })] : []), + ], + })), +]; + +function withMinify(build) { + return [build(false), build(true)]; +} + +export default configs; diff --git a/src/HandledError.ts b/src/HandledError.ts new file mode 100644 index 00000000..a52304de --- /dev/null +++ b/src/HandledError.ts @@ -0,0 +1,3 @@ +export class HandledError { + constructor(public readonly value: any) {} +} diff --git a/src/core/buildThunks.ts b/src/core/buildThunks.ts index 7e36c4c7..22fd4f03 100644 --- a/src/core/buildThunks.ts +++ b/src/core/buildThunks.ts @@ -17,6 +17,7 @@ import { Patch, isDraftable, produceWithPatches, enablePatches } from 'immer'; import { AnyAction, createAsyncThunk, ThunkAction, ThunkDispatch, AsyncThunk } from '@reduxjs/toolkit'; import { PrefetchOptions } from '../react-hooks/buildHooks'; +import { HandledError } from '../HandledError'; import { ApiEndpointQuery } from './module'; @@ -125,10 +126,6 @@ export type UpdateQueryResultThunk = { assertCast>(serializeQueryArgs); const assertEntityType: AssertEntityTypes = (entity) => { - if (IS_DEV()) { + if (typeof process !== 'undefined' && process.env.NODE_ENV === 'development') { if (!entityTypes.includes(entity.type as any)) { console.error(`Entity type '${entity.type}' was used, but not specified in \`entityTypes\`!`); } diff --git a/src/createApi.ts b/src/createApi.ts index 19e77043..4b916151 100644 --- a/src/createApi.ts +++ b/src/createApi.ts @@ -2,7 +2,6 @@ import type { Api, Module, ModuleName } from './apiTypes'; import type { BaseQueryArg, BaseQueryFn } from './baseQueryTypes'; import { defaultSerializeQueryArgs, SerializeQueryArgs } from './defaultSerializeQueryArgs'; import { DefinitionType, EndpointBuilder, EndpointDefinitions } from './endpointDefinitions'; -import { IS_DEV } from './utils'; export interface CreateApiOptions< BaseQuery extends BaseQueryFn, @@ -62,7 +61,7 @@ export function buildCreateApi, ...Module[]]>( }); for (const [endpoint, definition] of Object.entries(evaluatedEndpoints)) { - if (IS_DEV()) { + if (typeof process !== 'undefined' && process.env.NODE_ENV === 'development') { if (!inject.overrideExisting && endpoint in context.endpointDefinitions) { console.error( `called \`injectEndpoints\` to override already-existing endpoint ${endpoint} without specifying \`overrideExisting: true\`` diff --git a/src/retry.ts b/src/retry.ts index ac14430e..5a422519 100644 --- a/src/retry.ts +++ b/src/retry.ts @@ -1,5 +1,5 @@ import { BaseQueryEnhancer } from './baseQueryTypes'; -import { HandledError } from './core/buildThunks'; +import { HandledError } from './HandledError'; async function defaultBackoff(attempt: number = 0, maxRetries: number = 5) { const attempts = Math.min(attempt, maxRetries); diff --git a/src/utils/index.ts b/src/utils/index.ts index 2eedc837..99a40dc8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,7 +5,6 @@ export * from './flatten'; export * from './shallowEqual'; export * from './useShallowStableValue'; export * from './capitalize'; -export * from './isDev'; export * from './isOnline'; export * from './isDocumentVisible'; export * from './copyWithStructuralSharing'; diff --git a/src/utils/isDev.ts b/src/utils/isDev.ts deleted file mode 100644 index 16e49bb8..00000000 --- a/src/utils/isDev.ts +++ /dev/null @@ -1 +0,0 @@ -export const IS_DEV = () => typeof process !== 'undefined' && process.env.NODE_ENV === 'development'; diff --git a/tsconfig.json b/tsconfig.json index 796d4e81..ea6c948a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,8 @@ { // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs - "include": ["src", "test/flatten.test.ts"], + "include": ["src"], "compilerOptions": { + "target": "ESnext", "module": "esnext", "lib": ["dom", "esnext"], "importHelpers": true, @@ -31,6 +32,6 @@ "forceConsistentCasingInFileNames": true, // `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc` "noEmit": true, - "downlevelIteration": true + "downlevelIteration": false } } diff --git a/typesversions.js b/typesversions.js deleted file mode 100644 index 6d0ec818..00000000 --- a/typesversions.js +++ /dev/null @@ -1,23 +0,0 @@ -const fs = require('fs'); -const glob = require('glob'); -const { cp, mkdir, rm } = require('shelljs'); - -rm('-r', 'dist/min-4.1'); -mkdir('-p', 'dist/min-4.1'); - -const fourOneFiles = glob.sync('dist/*.d.ts'); -fourOneFiles.push('dist/utils'); -fourOneFiles.push('dist/core'); -fourOneFiles.push('dist/react-hooks'); - -for (let file of fourOneFiles) { - cp('-r', file, 'dist/min-4.1/'); -} - -const stubTs41Types = ` -import { EndpointDefinitions } from './endpointDefinitions'; -export declare type TS41Hooks = unknown; -export {}; -`; - -fs.writeFileSync('dist/ts41Types.d.ts', stubTs41Types); diff --git a/yarn.lock b/yarn.lock index db1de694..d51240a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -150,7 +150,7 @@ dependencies: "@babel/types" "^7.12.1" -"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.10.4", "@babel/helper-module-imports@^7.12.1": +"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.10.4", "@babel/helper-module-imports@^7.12.1", "@babel/helper-module-imports@^7.12.5": version "7.12.5" resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.12.5.tgz#1bfc0229f794988f76ed0a4d4e90860850b54dfb" integrity sha512-SR713Ogqg6++uexFRORf/+nPXMmWIn80TALu0uaFb+iQIUoR7bOC7zBWyzBs5b3tBBJXuyD0cRu1F15GyzjOWA== @@ -682,6 +682,15 @@ dependencies: "@babel/helper-plugin-utils" "^7.10.4" +"@babel/plugin-transform-runtime@^7.12.10": + version "7.12.10" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.12.10.tgz#af0fded4e846c4b37078e8e5d06deac6cd848562" + integrity sha512-xOrUfzPxw7+WDm9igMgQCbO3cJKymX7dFdsgRr1eu9n3KjjyU4pptIXbXPseQDquw+W+RuJEJMHKHNsPNNm3CA== + dependencies: + "@babel/helper-module-imports" "^7.12.5" + "@babel/helper-plugin-utils" "^7.10.4" + semver "^5.5.1" + "@babel/plugin-transform-shorthand-properties@^7.12.1": version "7.12.1" resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.12.1.tgz#0bf9cac5550fce0cfdf043420f661d645fdc75e3" @@ -1173,6 +1182,14 @@ "@babel/helper-module-imports" "^7.10.4" "@rollup/pluginutils" "^3.1.0" +"@rollup/plugin-babel@^5.2.2": + version "5.2.2" + resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.2.2.tgz#e5623a01dd8e37e004ba87f2de218c611727d9b2" + integrity sha512-MjmH7GvFT4TW8xFdIeFS3wqIX646y5tACdxkTO+khbHvS3ZcVJL6vkAHLw2wqPmkhwCfWHoNsp15VYNwW6JEJA== + dependencies: + "@babel/helper-module-imports" "^7.10.4" + "@rollup/pluginutils" "^3.1.0" + "@rollup/plugin-commonjs@^11.0.0": version "11.1.0" resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz#60636c7a722f54b41e419e1709df05c7234557ef" @@ -1205,7 +1222,7 @@ is-module "^1.0.0" resolve "^1.17.0" -"@rollup/plugin-replace@^2.2.1": +"@rollup/plugin-replace@^2.2.1", "@rollup/plugin-replace@^2.3.4": version "2.3.4" resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.4.tgz#7dd84c17755d62b509577f2db37eb524d7ca88ca" integrity sha512-waBhMzyAtjCL1GwZes2jaE9MjuQ/DQF2BatH3fRivUF3z0JBFrU0U6iBNC/4WR+2rLKhaAhPWDNPYp4mI6RqdQ== @@ -1213,6 +1230,14 @@ "@rollup/pluginutils" "^3.1.0" magic-string "^0.25.7" +"@rollup/plugin-typescript@^8.0.0": + version "8.0.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-8.0.0.tgz#d621d9e268ef591c9e439650f550c5ffc53545c1" + integrity sha512-2L/kKvM5U4VOm+yVMvPIBF3yMZtQUyopf4YIT+KQbqZBZ8Fsdm7X6yeezy92PMyvvHQG1Pa322MVwxPojQvukA== + dependencies: + "@rollup/pluginutils" "^3.1.0" + resolve "^1.17.0" + "@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.0.9", "@rollup/pluginutils@^3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b" @@ -5577,7 +5602,7 @@ jest-worker@^24.9.0: merge-stream "^2.0.0" supports-color "^6.1.0" -jest-worker@^26.6.2: +jest-worker@^26.2.1, jest-worker@^26.6.2: version "26.6.2" resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-26.6.2.tgz#7f72cbc4d643c365e27b9fd775f9d0eaa9c7a8ed" integrity sha512-KWYVV1c4i+jbMpaBC+U++4Va0cp8OisU185o73T1vo99hqi7w8tSJfUXYswwqqrjzwxa6KpRK54WhPvwf5w6PQ== @@ -7812,6 +7837,16 @@ rollup-plugin-terser@^5.1.2: serialize-javascript "^4.0.0" terser "^4.6.2" +rollup-plugin-terser@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/rollup-plugin-terser/-/rollup-plugin-terser-7.0.2.tgz#e8fbba4869981b2dc35ae7e8a502d5c6c04d324d" + integrity sha512-w3iIaU4OxcF52UUXiZNsNeuXIMDvFrr+ZXK6bFZ0Q60qyVfq4uLptoS4bbq3paG3x216eQllFZX7zt6TIImguQ== + dependencies: + "@babel/code-frame" "^7.10.4" + jest-worker "^26.2.1" + serialize-javascript "^4.0.0" + terser "^5.0.0" + rollup-plugin-typescript2@^0.27.3: version "0.27.3" resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.27.3.tgz#cd9455ac026d325b20c5728d2cc54a08a771b68b" @@ -7839,6 +7874,13 @@ rollup@^1.32.1: "@types/node" "*" acorn "^7.1.0" +rollup@^2.34.2: + version "2.34.2" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.34.2.tgz#fa73e05c64df587e9ed4dc80d7d4e7d4a43f8908" + integrity sha512-mvtQLqu3cNeoctS+kZ09iOPxrc1P1/Bt1z15enuQ5feyKOdM3MJAVFjjsygurDpSWn530xB4AlA83TWIzRstXA== + optionalDependencies: + fsevents "~2.1.2" + rsvp@^4.8.4: version "4.8.5" resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734" @@ -7960,7 +8002,7 @@ semver-regex@^2.0.0: resolved "https://registry.yarnpkg.com/semver-regex/-/semver-regex-2.0.0.tgz#a93c2c5844539a770233379107b38c7b4ac9d338" integrity sha512-mUdIBBvdn0PLOeP3TEkMH7HHeUP3GjsXCwKarjv/kGmUFOYg1VqEemKhoQpWMu6X2I8kHeuVdGibLGkVK+/5Qw== -"semver@2 || 3 || 4 || 5", semver@^5.4.1, semver@^5.5.0, semver@^5.6.0: +"semver@2 || 3 || 4 || 5", semver@^5.4.1, semver@^5.5.0, semver@^5.5.1, semver@^5.6.0: version "5.7.1" resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== @@ -8194,7 +8236,7 @@ source-map-resolve@^0.6.0: atob "^2.1.2" decode-uri-component "^0.2.0" -source-map-support@^0.5.17, source-map-support@^0.5.6, source-map-support@~0.5.12: +source-map-support@^0.5.17, source-map-support@^0.5.6, source-map-support@~0.5.12, source-map-support@~0.5.19: version "0.5.19" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== @@ -8217,7 +8259,7 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== -source-map@^0.7.3: +source-map@^0.7.3, source-map@~0.7.2: version "0.7.3" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383" integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ== @@ -8611,6 +8653,15 @@ terser@^4.1.2, terser@^4.6.2: source-map "~0.6.1" source-map-support "~0.5.12" +terser@^5.0.0: + version "5.5.1" + resolved "https://registry.yarnpkg.com/terser/-/terser-5.5.1.tgz#540caa25139d6f496fdea056e414284886fb2289" + integrity sha512-6VGWZNVP2KTUcltUQJ25TtNjx/XgdDsBDKGt8nN0MpydU36LmbPPcMBd2kmtZNNGVVDLg44k7GKeHHj+4zPIBQ== + dependencies: + commander "^2.20.0" + source-map "~0.7.2" + source-map-support "~0.5.19" + test-exclude@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"