Skip to content

Commit

Permalink
feat: introduce development and production modes
Browse files Browse the repository at this point in the history
This adopts a similar strategy to InstantSearch (algolia/instantsearch#3260) for our bundles as well as our development warnings (algolia/instantsearch#3574).
  • Loading branch information
francoischalifour committed Nov 9, 2020
1 parent b3ac3f9 commit c60f441
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
},
},
{
files: ['scripts/**/*'],
files: ['scripts/**/*', '*.config.js'],
rules: {
'import/no-commonjs': 'off',
},
Expand Down
7 changes: 4 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable import/no-commonjs */
const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');

module.exports = (api) => {
const isTest = api.env('test');
Expand All @@ -23,12 +23,13 @@ module.exports = (api) => {
],
],
plugins: clean([
!isTest && [
wrapWarningWithDevCheck,
[
'inline-replace-variables',
{
__DEV__: {
type: 'node',
replacement: "process.env.NODE_ENV === 'development'",
replacement: "process.env.NODE_ENV !== 'production'",
},
},
],
Expand Down
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable import/no-commonjs */

module.exports = {
rootDir: process.cwd(),
setupFilesAfterEnv: [
Expand Down
8 changes: 4 additions & 4 deletions packages/autocomplete-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../rollup.base.config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
8 changes: 4 additions & 4 deletions packages/autocomplete-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../rollup.base.config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
8 changes: 4 additions & 4 deletions packages/autocomplete-plugin-query-suggestions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-plugin-query-suggestions/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../rollup.base.config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
8 changes: 4 additions & 4 deletions packages/autocomplete-plugin-recent-searches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"sideEffects": false,
"files": [
"dist/"
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-plugin-recent-searches/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../rollup.base.config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function createRecentSearchesPlugin<TItem extends RecentSearchesItem>({
// because we need to resolve the promise before getting the value.
if (!Array.isArray(lastItemsRef.current)) {
warn(
false,
'The `getAlgoliaQuerySuggestionsFacetFilters` function is not supported with storages that return promises in `getAll`.'
);
return params;
Expand Down
8 changes: 4 additions & 4 deletions packages/autocomplete-preset-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"source": "src/index.ts",
"types": "dist/esm/index.d.ts",
"module": "dist/esm/index.js",
"main": "dist/umd/index.js",
"umd:main": "dist/umd/index.js",
"unpkg": "dist/umd/index.js",
"jsdelivr": "dist/umd/index.js",
"main": "dist/umd/index.production.js",
"umd:main": "dist/umd/index.production.js",
"unpkg": "dist/umd/index.production.js",
"jsdelivr": "dist/umd/index.production.js",
"scripts": {
"build:clean": "rm -rf ./dist",
"build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm --ignore '**/*/__tests__/'",
Expand Down
15 changes: 2 additions & 13 deletions packages/autocomplete-preset-algolia/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { plugins } from '../../rollup.base.config';
import { getBundleBanner } from '../../scripts/getBundleBanner';
import { createRollupConfigs } from '../../rollup.base.config';

import pkg from './package.json';

export default {
input: 'src/index.ts',
output: {
file: 'dist/umd/index.js',
format: 'umd',
sourcemap: true,
name: pkg.name,
banner: getBundleBanner(pkg),
},
plugins,
};
export default createRollupConfigs({ pkg });
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('parseAlgoliaHitHighlight', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_highlightResult.description.value" does not exist on the hit. Did you set it in `attributesToHighlight`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('parseAlgoliaHitSnippet', () => {
},
},
});
}).toWarn(
}).toWarnDev(
'[Autocomplete] The attribute "_snippetResult.description.value" does not exist on the hit. Did you set it in `attributesToSnippet`?' +
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({

if (typeof highlightedValue !== 'string') {
warn(
false,
`The attribute ${JSON.stringify(
path
)} does not exist on the hit. Did you set it in \`attributesToHighlight\`?` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function parseAlgoliaHitSnippet<THit extends Hit<{}>>({

if (typeof highlightedValue !== 'string') {
warn(
false,
`The attribute ${JSON.stringify(
path
)} does not exist on the hit. Did you set it in \`attributesToSnippet\`?` +
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-shared/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const __DEV__: boolean;
22 changes: 17 additions & 5 deletions packages/autocomplete-shared/src/__tests__/warn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
import { warn } from '../warn';

describe('warn', () => {
test('logs when the condition is unmet', () => {
expect(() => {
warn(false, 'warning');
}).toWarnDev('[Autocomplete] warning');
});

test('does not log when the condition is unmet', () => {
expect(() => {
warn(true, 'warning');
}).not.toWarnDev();
});

test('trims the message', () => {
expect(() => {
warn('\nwarning! ');
}).toWarn('[Autocomplete] warning!');
warn(false, '\nwarning! ');
}).toWarnDev('[Autocomplete] warning!');
});

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

warn('warning1');
warn('warning1');
warn('warning2');
warn(false, 'warning1');
warn(false, 'warning1');
warn(false, 'warning2');

expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenNthCalledWith(1, '[Autocomplete] warning1');
Expand Down
14 changes: 13 additions & 1 deletion packages/autocomplete-shared/src/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ export const warnCache = {
current: {},
};

export function warn(message: string) {
/**
* Logs a warning if the condition is not met.
* This is used to log issues in development environment only.
*/
export function warn(condition: boolean, message: string) {
if (!__DEV__) {
return;
}

if (condition) {
return;
}

const sanitizedMessage = message.trim();
const hasAlreadyPrinted = warnCache.current[sanitizedMessage];

Expand Down
Loading

0 comments on commit c60f441

Please sign in to comment.