From ed1e1634726252afc4178ae2bcbadacaf9f055ce Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Fri, 7 Jan 2022 15:45:06 -0300 Subject: [PATCH] chore: bundles exports everything and webpack bundles everything --- docs/pages/compiled-code.md | 9 ++++---- package.json | 9 +++----- src/cache/create.ts | 2 +- src/index.browser.ts | 11 ---------- src/index.development.ts | 14 ++----------- src/index.ts | 7 +++++++ src/util/cache-predicate.ts | 3 ++- src/util/types.ts | 2 +- src/util/update-cache.ts | 2 +- test/bundle.test.ts | 9 -------- test/mocks/axios.ts | 5 +++-- test/storage/is-storage.test.ts | 3 ++- test/util/cache-predicate.test.ts | 2 +- test/util/update-cache.test.ts | 2 +- tsconfig.browser.json | 11 ---------- tsconfig.build.json | 8 +++++--- webpack.config.js | 34 +++++++++++++++++++++++-------- 17 files changed, 59 insertions(+), 74 deletions(-) delete mode 100644 src/index.browser.ts delete mode 100644 tsconfig.browser.json diff --git a/docs/pages/compiled-code.md b/docs/pages/compiled-code.md index 20dd27b9..57238c08 100644 --- a/docs/pages/compiled-code.md +++ b/docs/pages/compiled-code.md @@ -1,9 +1,8 @@ # Compiled code -## NodeJS +## CommonJS -The code is compiled with `tsc` with support to `>= ES6`. See the -[build config](/tsconfig.build.json). +The code is compiled with `webpack` for ES2017+. - `axios-cache-interceptor`: Redirects to `/dist/index.js` - `axios-cache-interceptor/dist/index.js`: The main library file. @@ -12,7 +11,7 @@ The code is compiled with `tsc` with support to `>= ES6`. See the Every browser build is also compatible with CommonsJS because it builds with UMD, so you can use them too. -## Browsers +## UMD > _NOTE_: Axios itself requires [ES6 Promises](https://axios-http.com/docs/notes#promises) @@ -23,7 +22,7 @@ CommonsJS and more) - `axios-cache-interceptor/dist/index.min.js`: Production file for ES6+ - `axios-cache-interceptor/dist/index.es5.min.js`: Production file for ES5+ - `axios-cache-interceptor/dist/index.es2020.min.js`: Production file for ES2020+ -- `axios-cache-interceptor/dist/index.development.js`: Development file +- `axios-cache-interceptor/dist/index.development.js`: Development file (ES2020+) ```html diff --git a/package.json b/package.json index 5572249a..1a6d2ceb 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,16 @@ "name": "axios-cache-interceptor", "version": "0.7.9", "description": "Cache interceptor for axios", + "types": "./dist/index.ts", "main": "./dist/index.js", "browser": "./dist/index.min.js", "jsdelivr": "./dist/index.min.js", "unpkg": "./dist/index.min.js", "runkitExampleFilename": "./examples/runkit.js", "scripts": { - "build": "concurrently 'npm:build:*'", - "build:browser": "webpack", - "build:node": "tsc -p tsconfig.build.json", + "build": "webpack", "test": "jest --coverage", - "escheck": "es-check es5 ./dist/index.es5.min.js && es-check es6 ./dist/index.min.js", + "escheck": "npx es-check es5 ./dist/index.es5.min.js && npx es-check es6 ./dist/index.min.js", "format": "prettier --write .", "lint": "tsc --noEmit && eslint . --ext .ts", "version": "auto-changelog -p && git add CHANGELOG.md" @@ -53,8 +52,6 @@ "@typescript-eslint/parser": "^5.8.1", "auto-changelog": "^2.3.0", "axios": "~0.24.0", - "concurrently": "^7.0.0", - "es-check": "^6.1.1", "eslint": "^8.3.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", diff --git a/src/cache/create.ts b/src/cache/create.ts index 7d909e8c..c2fc432b 100644 --- a/src/cache/create.ts +++ b/src/cache/create.ts @@ -1,8 +1,8 @@ import type { AxiosInstance } from 'axios'; -import { isStorage } from '..'; import { defaultHeaderInterpreter } from '../header/interpreter'; import { CacheRequestInterceptor } from '../interceptors/request'; import { CacheResponseInterceptor } from '../interceptors/response'; +import { isStorage } from '../storage/build'; import { buildMemoryStorage } from '../storage/memory'; import { defaultKeyGenerator } from '../util/key-generator'; import type { AxiosCacheInstance } from './axios'; diff --git a/src/index.browser.ts b/src/index.browser.ts deleted file mode 100644 index 86d241ca..00000000 --- a/src/index.browser.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** Index file for webpack and cdn usage */ - -export { - createCache, - isAxiosCacheInterceptor, - setupCache, - useCache -} from './cache/create'; -export { buildStorage } from './storage/build'; -export { buildMemoryStorage } from './storage/memory'; -export { buildWebStorage } from './storage/web-api'; diff --git a/src/index.development.ts b/src/index.development.ts index 7ad896a0..52e5b8c6 100644 --- a/src/index.development.ts +++ b/src/index.development.ts @@ -1,15 +1,5 @@ -/** Index file for webpack and cdn usage */ - -export { - createCache, - isAxiosCacheInterceptor, - setupCache, - useCache -} from './cache/create'; -export { buildStorage } from './storage/build'; -export { buildMemoryStorage } from './storage/memory'; -export { buildWebStorage } from './storage/web-api'; +export * from './index'; console.warn( - 'You are using a development build. Make sure to use the correct build in production\nhttps://axios-cache-interceptor.js.org/pages/installing' + 'You are using a development build. Make sure to use the correct build in production\nhttps://axios-cache-interceptor.js.org/#/pages/installing' ); diff --git a/src/index.ts b/src/index.ts index 627e5f9a..d16b29e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,18 @@ export * from './cache/axios'; export * from './cache/cache'; export * from './cache/create'; +export * from './header/interpreter'; export * from './header/types'; +export * from './interceptors/request'; +export * from './interceptors/response'; export * from './interceptors/types'; +export * as InterceptorUtil from './interceptors/util'; export * from './storage/build'; export * from './storage/memory'; export * from './storage/types'; export * from './storage/web-api'; +export * from './util/cache-predicate'; export * from './util/headers'; +export * from './util/key-generator'; export * from './util/types'; +export * from './util/update-cache'; diff --git a/src/util/cache-predicate.ts b/src/util/cache-predicate.ts index 25154b29..aaefbb6a 100644 --- a/src/util/cache-predicate.ts +++ b/src/util/cache-predicate.ts @@ -1,4 +1,5 @@ -import type { CacheAxiosResponse, CacheProperties } from '..'; +import type { CacheAxiosResponse } from '../cache/axios'; +import type { CacheProperties } from '../cache/cache'; import type { CachePredicateObject } from './types'; /** Returns true if the response should be cached */ diff --git a/src/util/types.ts b/src/util/types.ts index 2cb97f16..0213bf54 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -31,7 +31,7 @@ export type KeyGenerator = ( options: CacheRequestConfig ) => string; -type MaybePromise = T | Promise | PromiseLike; +export type MaybePromise = T | Promise | PromiseLike; export type CacheUpdater = | 'delete' diff --git a/src/util/update-cache.ts b/src/util/update-cache.ts index f6fed44b..5b66f8f0 100644 --- a/src/util/update-cache.ts +++ b/src/util/update-cache.ts @@ -1,5 +1,5 @@ -import type { AxiosStorage } from '..'; import type { CacheAxiosResponse } from '../cache/axios'; +import type { AxiosStorage } from '../storage/types'; import type { CacheUpdater } from './types'; /** Function to update all caches, from CacheProperties.update, with the new data. */ diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 9701a922..24d6f1b9 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -3,15 +3,6 @@ import { buildMemoryStorage } from '../src/storage/memory'; import { buildWebStorage } from '../src/storage/web-api'; describe('test bundle imports', () => { - it('tests browser ', async () => { - const bundle = await import('../src/index.browser'); - - expect(bundle.setupCache).toBe(setupCache); - expect(bundle.isAxiosCacheInterceptor).toBe(isAxiosCacheInterceptor); - expect(bundle.buildMemoryStorage).toBe(buildMemoryStorage); - expect(bundle.buildWebStorage).toBe(buildWebStorage); - }); - it('test development bundle imports', async () => { const oldWarn = console.warn; console.warn = jest.fn(); diff --git a/test/mocks/axios.ts b/test/mocks/axios.ts index ca1fa8e1..693e0f6a 100644 --- a/test/mocks/axios.ts +++ b/test/mocks/axios.ts @@ -1,6 +1,7 @@ import Axios from 'axios'; -import { AxiosCacheInstance, CacheProperties, setupCache } from '../../src'; -import type { CacheInstance } from '../../src/cache/cache'; +import type { AxiosCacheInstance } from '../../src/cache/axios'; +import type { CacheInstance, CacheProperties } from '../../src/cache/cache'; +import { setupCache } from '../../src/cache/create'; import { Header } from '../../src/util/headers'; export const XMockRandom = 'x-mock-random'; diff --git a/test/storage/is-storage.test.ts b/test/storage/is-storage.test.ts index 9a2eb6c0..d8a5b846 100644 --- a/test/storage/is-storage.test.ts +++ b/test/storage/is-storage.test.ts @@ -1,5 +1,6 @@ import { Axios } from 'axios'; -import { buildMemoryStorage, isStorage } from '../../src'; +import { isStorage } from '../../src/storage/build'; +import { buildMemoryStorage } from '../../src/storage/memory'; import { mockAxios } from '../mocks/axios'; it('tests isStorage function', () => { diff --git a/test/util/cache-predicate.test.ts b/test/util/cache-predicate.test.ts index d9cb2e75..82f10bae 100644 --- a/test/util/cache-predicate.test.ts +++ b/test/util/cache-predicate.test.ts @@ -1,4 +1,4 @@ -import type { CachedStorageValue } from '../../src'; +import type { CachedStorageValue } from '../../src/storage/types'; import { isCachePredicateValid } from '../../src/util/cache-predicate'; import { mockAxios } from '../mocks/axios'; import { createResponse } from '../utils'; diff --git a/test/util/update-cache.test.ts b/test/util/update-cache.test.ts index a4d22d1a..d4fb0ede 100644 --- a/test/util/update-cache.test.ts +++ b/test/util/update-cache.test.ts @@ -1,4 +1,4 @@ -import type { CachedStorageValue } from '../../src'; +import type { CachedStorageValue } from '../../src/storage/types'; import { defaultKeyGenerator } from '../../src/util/key-generator'; import { mockAxios } from '../mocks/axios'; diff --git a/tsconfig.browser.json b/tsconfig.browser.json deleted file mode 100644 index da7049bb..00000000 --- a/tsconfig.browser.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "noEmit": false, - "target": "ES6", - "declaration": false, - "declarationMap": false, - "module": "ESNext" - }, - "include": ["src/index.*.ts"] -} diff --git a/tsconfig.build.json b/tsconfig.build.json index 026c9cee..3b5266bc 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -2,8 +2,10 @@ "extends": "./tsconfig.json", "compilerOptions": { "noEmit": false, - "target": "ES6" + "target": "ESNext", + "module": "ESNext", + "declaration": false, + "declarationMap": false }, - "include": ["src"], - "exclude": ["src/index.*.ts"] + "include": ["src"] } diff --git a/webpack.config.js b/webpack.config.js index cb0568de..b4459a72 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -10,10 +10,12 @@ const TerserWebpackPlugin = require('terser-webpack-plugin'); * entry: string; * esTarget: string; * minimize: boolean; + * override?: import('typescript').CompilerOptions; + * library?: import('webpack').LibraryOptions; * }} options * @returns {import('webpack').Configuration} */ -const config = ({ output, esTarget, minimize, entry }) => ({ +const config = ({ output, esTarget, minimize, entry, override = {}, library }) => ({ mode: 'production', entry: path.resolve(__dirname, 'src', entry), @@ -24,7 +26,8 @@ const config = ({ output, esTarget, minimize, entry }) => ({ filename: output, library: { type: 'umd', - name: 'AxiosCacheInterceptor' + name: 'AxiosCacheInterceptor', + ...library }, chunkFormat: 'module' }, @@ -44,9 +47,10 @@ const config = ({ output, esTarget, minimize, entry }) => ({ test: /\.(ts|js)$/, loader: 'ts-loader', options: { - configFile: path.resolve(__dirname, 'tsconfig.browser.json'), + configFile: path.resolve(__dirname, 'tsconfig.build.json'), compilerOptions: { - target: esTarget + target: esTarget, + ...override } } } @@ -67,21 +71,35 @@ module.exports = [ minimize: false }), config({ - esTarget: 'es2015', - entry: 'index.browser.ts', + esTarget: 'es2015', // ES6 + entry: 'index.ts', output: 'index.min.js', minimize: true }), config({ esTarget: 'es5', - entry: 'index.browser.ts', + entry: 'index.ts', output: 'index.es5.min.js', minimize: true }), config({ esTarget: 'es2020', - entry: 'index.browser.ts', + entry: 'index.ts', output: 'index.es2020.min.js', minimize: true + }), + config({ + esTarget: 'es2017', + entry: 'index.ts', + output: 'index.js', + minimize: true, + library: { + type: 'commonjs', + name: undefined + }, + override: { + declaration: true, + declarationMap: true + } }) ];