Skip to content

Commit

Permalink
chore: bundles exports everything and webpack bundles everything
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 7, 2022
1 parent 79ce192 commit ed1e163
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 74 deletions.
9 changes: 4 additions & 5 deletions docs/pages/compiled-code.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand All @@ -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
<!-- You can use the cdn of your choice -->
Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cache/create.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
11 changes: 0 additions & 11 deletions src/index.browser.ts

This file was deleted.

14 changes: 2 additions & 12 deletions src/index.development.ts
Original file line number Diff line number Diff line change
@@ -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'
);
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 2 additions & 1 deletion src/util/cache-predicate.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type KeyGenerator = <R = any, D = any>(
options: CacheRequestConfig<R, D>
) => string;

type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
export type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;

export type CacheUpdater<R, D> =
| 'delete'
Expand Down
2 changes: 1 addition & 1 deletion src/util/update-cache.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down
9 changes: 0 additions & 9 deletions test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion test/storage/is-storage.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion test/util/update-cache.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
11 changes: 0 additions & 11 deletions tsconfig.browser.json

This file was deleted.

8 changes: 5 additions & 3 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
34 changes: 26 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -24,7 +26,8 @@ const config = ({ output, esTarget, minimize, entry }) => ({
filename: output,
library: {
type: 'umd',
name: 'AxiosCacheInterceptor'
name: 'AxiosCacheInterceptor',
...library
},
chunkFormat: 'module'
},
Expand All @@ -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
}
}
}
Expand All @@ -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
}
})
];

0 comments on commit ed1e163

Please sign in to comment.