Skip to content

Commit eed934f

Browse files
feat: introduce development and production modes (#363)
1 parent b3ac3f9 commit eed934f

35 files changed

+244
-153
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
},
7575
},
7676
{
77-
files: ['scripts/**/*'],
77+
files: ['scripts/**/*', '*.config.js'],
7878
rules: {
7979
'import/no-commonjs': 'off',
8080
},

babel.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable import/no-commonjs */
1+
const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');
22

33
module.exports = (api) => {
44
const isTest = api.env('test');
@@ -23,12 +23,13 @@ module.exports = (api) => {
2323
],
2424
],
2525
plugins: clean([
26-
!isTest && [
26+
wrapWarningWithDevCheck,
27+
[
2728
'inline-replace-variables',
2829
{
2930
__DEV__: {
3031
type: 'node',
31-
replacement: "process.env.NODE_ENV === 'development'",
32+
replacement: "process.env.NODE_ENV !== 'production'",
3233
},
3334
},
3435
],

bundlesize.config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"files": [
33
{
4-
"path": "packages/autocomplete-core/dist/umd/index.js",
4+
"path": "packages/autocomplete-core/dist/umd/index.production.js",
55
"maxSize": "4.5 kB"
66
},
77
{
8-
"path": "packages/autocomplete-js/dist/umd/index.js",
8+
"path": "packages/autocomplete-js/dist/umd/index.production.js",
99
"maxSize": "8 kB"
1010
},
1111
{
12-
"path": "packages/autocomplete-preset-algolia/dist/umd/index.js",
12+
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
1313
"maxSize": "1.5 kB"
1414
},
1515
{
16-
"path": "packages/autocomplete-plugin-recent-searches/dist/umd/index.js",
16+
"path": "packages/autocomplete-plugin-recent-searches/dist/umd/index.production.js",
1717
"maxSize": "2.5 kB"
1818
},
1919
{
20-
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.js",
20+
"path": "packages/autocomplete-plugin-query-suggestions/dist/umd/index.production.js",
2121
"maxSize": "2.25 kB"
2222
}
2323
]

jest.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable import/no-commonjs */
2-
31
module.exports = {
42
rootDir: process.cwd(),
53
setupFilesAfterEnv: [

packages/autocomplete-core/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"source": "src/index.ts",
1313
"types": "dist/esm/index.d.ts",
1414
"module": "dist/esm/index.js",
15-
"main": "dist/umd/index.js",
16-
"umd:main": "dist/umd/index.js",
17-
"unpkg": "dist/umd/index.js",
18-
"jsdelivr": "dist/umd/index.js",
15+
"main": "dist/umd/index.production.js",
16+
"umd:main": "dist/umd/index.production.js",
17+
"unpkg": "dist/umd/index.production.js",
18+
"jsdelivr": "dist/umd/index.production.js",
1919
"sideEffects": false,
2020
"files": [
2121
"dist/"
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { plugins } from '../../rollup.base.config';
2-
import { getBundleBanner } from '../../scripts/getBundleBanner';
1+
import { createRollupConfigs } from '../../scripts/rollup/config';
32

43
import pkg from './package.json';
54

6-
export default {
7-
input: 'src/index.ts',
8-
output: {
9-
file: 'dist/umd/index.js',
10-
format: 'umd',
11-
sourcemap: true,
12-
name: pkg.name,
13-
banner: getBundleBanner(pkg),
14-
},
15-
plugins,
16-
};
5+
export default createRollupConfigs({ pkg });

packages/autocomplete-js/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"source": "src/index.ts",
1313
"types": "dist/esm/index.d.ts",
1414
"module": "dist/esm/index.js",
15-
"main": "dist/umd/index.js",
16-
"umd:main": "dist/umd/index.js",
17-
"unpkg": "dist/umd/index.js",
18-
"jsdelivr": "dist/umd/index.js",
15+
"main": "dist/umd/index.production.js",
16+
"umd:main": "dist/umd/index.production.js",
17+
"unpkg": "dist/umd/index.production.js",
18+
"jsdelivr": "dist/umd/index.production.js",
1919
"sideEffects": false,
2020
"files": [
2121
"dist/"
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { plugins } from '../../rollup.base.config';
2-
import { getBundleBanner } from '../../scripts/getBundleBanner';
1+
import { createRollupConfigs } from '../../scripts/rollup/config';
32

43
import pkg from './package.json';
54

6-
export default {
7-
input: 'src/index.ts',
8-
output: {
9-
file: 'dist/umd/index.js',
10-
format: 'umd',
11-
sourcemap: true,
12-
name: pkg.name,
13-
banner: getBundleBanner(pkg),
14-
},
15-
plugins,
16-
};
5+
export default createRollupConfigs({ pkg });

packages/autocomplete-plugin-query-suggestions/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"source": "src/index.ts",
1313
"types": "dist/esm/index.d.ts",
1414
"module": "dist/esm/index.js",
15-
"main": "dist/umd/index.js",
16-
"umd:main": "dist/umd/index.js",
17-
"unpkg": "dist/umd/index.js",
18-
"jsdelivr": "dist/umd/index.js",
15+
"main": "dist/umd/index.production.js",
16+
"umd:main": "dist/umd/index.production.js",
17+
"unpkg": "dist/umd/index.production.js",
18+
"jsdelivr": "dist/umd/index.production.js",
1919
"sideEffects": false,
2020
"files": [
2121
"dist/"
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { plugins } from '../../rollup.base.config';
2-
import { getBundleBanner } from '../../scripts/getBundleBanner';
1+
import { createRollupConfigs } from '../../scripts/rollup/config';
32

43
import pkg from './package.json';
54

6-
export default {
7-
input: 'src/index.ts',
8-
output: {
9-
file: 'dist/umd/index.js',
10-
format: 'umd',
11-
sourcemap: true,
12-
name: pkg.name,
13-
banner: getBundleBanner(pkg),
14-
},
15-
plugins,
16-
};
5+
export default createRollupConfigs({ pkg });

packages/autocomplete-plugin-recent-searches/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"source": "src/index.ts",
1313
"types": "dist/esm/index.d.ts",
1414
"module": "dist/esm/index.js",
15-
"main": "dist/umd/index.js",
16-
"umd:main": "dist/umd/index.js",
17-
"unpkg": "dist/umd/index.js",
18-
"jsdelivr": "dist/umd/index.js",
15+
"main": "dist/umd/index.production.js",
16+
"umd:main": "dist/umd/index.production.js",
17+
"unpkg": "dist/umd/index.production.js",
18+
"jsdelivr": "dist/umd/index.production.js",
1919
"sideEffects": false,
2020
"files": [
2121
"dist/"
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { plugins } from '../../rollup.base.config';
2-
import { getBundleBanner } from '../../scripts/getBundleBanner';
1+
import { createRollupConfigs } from '../../scripts/rollup/config';
32

43
import pkg from './package.json';
54

6-
export default {
7-
input: 'src/index.ts',
8-
output: {
9-
file: 'dist/umd/index.js',
10-
format: 'umd',
11-
sourcemap: true,
12-
name: pkg.name,
13-
banner: getBundleBanner(pkg),
14-
},
15-
plugins,
16-
};
5+
export default createRollupConfigs({ pkg });

packages/autocomplete-plugin-recent-searches/src/createRecentSearchesPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function createRecentSearchesPlugin<TItem extends RecentSearchesItem>({
9494
// because we need to resolve the promise before getting the value.
9595
if (!Array.isArray(lastItemsRef.current)) {
9696
warn(
97+
false,
9798
'The `getAlgoliaQuerySuggestionsFacetFilters` function is not supported with storages that return promises in `getAll`.'
9899
);
99100
return params;

packages/autocomplete-preset-algolia/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"source": "src/index.ts",
1717
"types": "dist/esm/index.d.ts",
1818
"module": "dist/esm/index.js",
19-
"main": "dist/umd/index.js",
20-
"umd:main": "dist/umd/index.js",
21-
"unpkg": "dist/umd/index.js",
22-
"jsdelivr": "dist/umd/index.js",
19+
"main": "dist/umd/index.production.js",
20+
"umd:main": "dist/umd/index.production.js",
21+
"unpkg": "dist/umd/index.production.js",
22+
"jsdelivr": "dist/umd/index.production.js",
2323
"scripts": {
2424
"build:clean": "rm -rf ./dist",
2525
"build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm --ignore '**/*/__tests__/'",
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import { plugins } from '../../rollup.base.config';
2-
import { getBundleBanner } from '../../scripts/getBundleBanner';
1+
import { createRollupConfigs } from '../../scripts/rollup/config';
32

43
import pkg from './package.json';
54

6-
export default {
7-
input: 'src/index.ts',
8-
output: {
9-
file: 'dist/umd/index.js',
10-
format: 'umd',
11-
sourcemap: true,
12-
name: pkg.name,
13-
banner: getBundleBanner(pkg),
14-
},
15-
plugins,
16-
};
5+
export default createRollupConfigs({ pkg });

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitHighlight.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('parseAlgoliaHitHighlight', () => {
169169
},
170170
},
171171
});
172-
}).toWarn(
172+
}).toWarnDev(
173173
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
174174
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
175175
);

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseHighlight.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
186186
},
187187
},
188188
});
189-
}).toWarn(
189+
}).toWarnDev(
190190
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
191191
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
192192
);

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseSnippet.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
160160
},
161161
},
162162
});
163-
}).toWarn(
163+
}).toWarnDev(
164164
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
165165
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
166166
);

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitSnippet.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('parseAlgoliaHitSnippet', () => {
160160
},
161161
},
162162
});
163-
}).toWarn(
163+
}).toWarnDev(
164164
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
165165
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
166166
);

packages/autocomplete-preset-algolia/src/highlight/parseAlgoliaHitHighlight.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({
1616

1717
if (typeof highlightedValue !== 'string') {
1818
warn(
19+
false,
1920
`The attribute ${JSON.stringify(
2021
path
2122
)} does not exist on the hit. Did you set it in \`attributesToHighlight\`?` +

packages/autocomplete-preset-algolia/src/highlight/parseAlgoliaHitSnippet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function parseAlgoliaHitSnippet<THit extends Hit<{}>>({
1616

1717
if (typeof highlightedValue !== 'string') {
1818
warn(
19+
false,
1920
`The attribute ${JSON.stringify(
2021
path
2122
)} does not exist on the hit. Did you set it in \`attributesToSnippet\`?` +
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const __DEV__: boolean;

packages/autocomplete-shared/src/__tests__/warn.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@
33
import { warn } from '../warn';
44

55
describe('warn', () => {
6+
test('logs when the condition is unmet', () => {
7+
expect(() => {
8+
warn(false, 'warning');
9+
}).toWarnDev('[Autocomplete] warning');
10+
});
11+
12+
test('does not log when the condition is unmet', () => {
13+
expect(() => {
14+
warn(true, 'warning');
15+
}).not.toWarnDev();
16+
});
17+
618
test('trims the message', () => {
719
expect(() => {
8-
warn('\nwarning! ');
9-
}).toWarn('[Autocomplete] warning!');
20+
warn(false, '\nwarning! ');
21+
}).toWarnDev('[Autocomplete] warning!');
1022
});
1123

1224
test('warns a message a single time', () => {
1325
const originalConsoleWarn = console.warn;
1426
console.warn = jest.fn();
1527

16-
warn('warning1');
17-
warn('warning1');
18-
warn('warning2');
28+
warn(false, 'warning1');
29+
warn(false, 'warning1');
30+
warn(false, 'warning2');
1931

2032
expect(console.warn).toHaveBeenCalledTimes(2);
2133
expect(console.warn).toHaveBeenNthCalledWith(1, '[Autocomplete] warning1');

packages/autocomplete-shared/src/warn.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ export const warnCache = {
22
current: {},
33
};
44

5-
export function warn(message: string) {
5+
/**
6+
* Logs a warning if the condition is not met.
7+
* This is used to log issues in development environment only.
8+
*/
9+
export function warn(condition: boolean, message: string) {
10+
if (!__DEV__) {
11+
return;
12+
}
13+
14+
if (condition) {
15+
return;
16+
}
17+
618
const sanitizedMessage = message.trim();
719
const hasAlreadyPrinted = warnCache.current[sanitizedMessage];
820

packages/website/docusaurus.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable import/no-commonjs */
2-
31
module.exports = {
42
title: 'Autocomplete',
53
tagline: 'JavaScript library for building autocomplete search experiences.',

rollup.base.config.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)