Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit ae4df8a

Browse files
authored
feat(hooks): bundle as es-module (#3232)
* feat(hooks): package as a real es-module * replace es to cjs paths in cjs build * replace wholesale extensions * make jest resolve * make replaced files compatible with linux * suppress lint errors * fix some more import errors * fix ts * lint * move lint rules * remove comment * es: import instantsearch from /es too, to make it explicitly the module * not a module * diff with babel plugin instead * remove all extensions * Apply suggestions from code review * babel: resolve packages too * remove no longer needed extensions * undo change * test: add an assertion to make sure it actually exports a module * pin devdep * consolidate script * changes in configuration * add comment * update instantsearch * make importStartsWith a configuration * fix extension test * feat(deps): update helper requirement * chore(deps): update instantsearch with es fix * node 14 for esm test * fix typo
1 parent 29cd70a commit ae4df8a

File tree

18 files changed

+531
-35
lines changed

18 files changed

+531
-35
lines changed

.circleci/config.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ version: 2
2929
jobs:
3030
test_build:
3131
<<: *defaults
32+
docker:
33+
- image: cimg/node:14.18.2
3234
steps:
3335
- checkout
3436
- run: *install_yarn_version
@@ -38,6 +40,20 @@ jobs:
3840
- run:
3941
name: Build & Test packages size
4042
command: yarn test:build
43+
- store_artifacts:
44+
path: packages/react-instantsearch/dist
45+
- store_artifacts:
46+
path: packages/react-instantsearch-core/dist
47+
- store_artifacts:
48+
path: packages/react-instantsearch-dom/dist
49+
- store_artifacts:
50+
path: packages/react-instantsearch-dom-maps/dist
51+
- store_artifacts:
52+
path: packages/react-instantsearch-hooks/dist
53+
- store_artifacts:
54+
path: packages/react-instantsearch-hooks-server/dist
55+
- store_artifacts:
56+
path: packages/react-instantsearch-native/dist
4157

4258
test_unit:
4359
<<: *defaults

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ const config = {
143143
pathGroupsExcludedImportTypes: ['builtin'],
144144
},
145145
],
146+
'import/extensions': ['error', 'never'],
147+
},
148+
settings: {
149+
'import/parsers': {
150+
'@typescript-eslint/parser': ['.ts', '.tsx'],
151+
},
146152
},
147153
},
148154
{

babel.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');
2+
const extensionResolver = require('./scripts/babel/extension-resolver');
23

34
const isES = process.env.BABEL_ENV === 'es';
45
const isRollup = process.env.BABEL_ENV === 'rollup';
@@ -40,6 +41,18 @@ module.exports = (api) => {
4041
},
4142
},
4243
],
44+
isES && [
45+
extensionResolver,
46+
{
47+
// For verification, see test/module/packages-are-es-modules.mjs
48+
modulesToResolve: [
49+
// InstantSearch.js/es is an ES Module, so needs complete paths,
50+
'instantsearch.js',
51+
// React-DOM also fails if the paths are incomplete
52+
'react-dom',
53+
],
54+
},
55+
],
4356
]),
4457
overrides: [
4558
{

examples/hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"algoliasearch": "4.10.5",
11-
"instantsearch.js": "4.34.0",
11+
"instantsearch.js": "4.37.1",
1212
"react": "17.0.2",
1313
"react-dom": "17.0.2",
1414
"react-instantsearch-hooks": "6.18.0"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"test:e2e:saucelabs": "wdio wdio.saucelabs.conf.js",
2424
"test:integration": "NODE_ENV=development yarn test:regressions && yarn test:argos",
2525
"test:examples": "node scripts/test-examples.js",
26-
"test:build": "yarn build && yarn test:size",
26+
"test:build": "yarn build && yarn test:size && node ./test/module/packages-are-es-modules.mjs",
2727
"test:regressions": "webpack --config integration/webpack.config.js && node integration/runTest.js",
2828
"test:argos": "argos upload integration/screenshots --token $ARGOS_TOKEN || true",
2929
"test:size": "bundlesize",
@@ -93,6 +93,7 @@
9393
"eslint": "6.8.0",
9494
"eslint-config-algolia": "16.0.0",
9595
"eslint-config-prettier": "6.15.0",
96+
"eslint-import-resolver-typescript": "2.5.0",
9697
"eslint-plugin-eslint-comments": "3.2.0",
9798
"eslint-plugin-import": "2.24.2",
9899
"eslint-plugin-jest": "24.4.2",

packages/react-instantsearch-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"dependencies": {
3939
"@babel/runtime": "^7.1.2",
40-
"algoliasearch-helper": "^3.6.2",
40+
"algoliasearch-helper": "^3.7.0",
4141
"prop-types": "^15.6.2",
4242
"react-fast-compare": "^3.0.0"
4343
},

packages/react-instantsearch-dom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"dependencies": {
3737
"@babel/runtime": "^7.1.2",
38-
"algoliasearch-helper": "^3.6.2",
38+
"algoliasearch-helper": "^3.7.0",
3939
"classnames": "^2.2.5",
4040
"prop-types": "^15.6.2",
4141
"react-fast-compare": "^3.0.0",

packages/react-instantsearch-hooks-server/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"types": "dist/es/index.d.ts",
77
"main": "dist/cjs/index.js",
88
"module": "dist/es/index.js",
9+
"type": "module",
10+
"exports": {
11+
".": {
12+
"import": "./dist/es/index.js",
13+
"require": "./dist/cjs/index.js"
14+
}
15+
},
916
"sideEffects": false,
1017
"license": "MIT",
1118
"homepage": "https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/",
@@ -30,14 +37,14 @@
3037
"clean": "rm -rf dist",
3138
"watch": "yarn build:cjs --watch",
3239
"build": "yarn build:cjs && yarn build:es && yarn build:umd && yarn build:types",
33-
"build:cjs": "babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/cjs --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet",
40+
"build:cjs": "babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/cjs --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet && ../../scripts/replace-import-for-cjs.sh",
3441
"build:es": "BABEL_ENV=es babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/es --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet",
3542
"build:umd": "BABEL_ENV=rollup rollup -c rollup.config.js",
3643
"build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/es"
3744
},
3845
"dependencies": {
3946
"@babel/runtime": "^7.1.2",
40-
"instantsearch.js": "^4.34.0",
47+
"instantsearch.js": "^4.37.1",
4148
"react-instantsearch-hooks": "6.18.0"
4249
},
4350
"peerDependencies": {

packages/react-instantsearch-hooks/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"types": "dist/es/index.d.ts",
77
"main": "dist/cjs/index.js",
88
"module": "dist/es/index.js",
9+
"type": "module",
10+
"exports": {
11+
".": {
12+
"import": "./dist/es/index.js",
13+
"require": "./dist/cjs/index.js"
14+
}
15+
},
916
"sideEffects": false,
1017
"license": "MIT",
1118
"homepage": "https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/",
@@ -33,16 +40,16 @@
3340
"clean": "rm -rf dist",
3441
"watch": "yarn build:cjs --watch",
3542
"build": "yarn build:cjs && yarn build:es && yarn build:umd && yarn build:types",
36-
"build:cjs": "babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/cjs --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet",
43+
"build:cjs": "babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/cjs --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet && ../../scripts/replace-import-for-cjs.sh",
3744
"build:es": "BABEL_ENV=es babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/es --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet",
3845
"build:umd": "BABEL_ENV=rollup rollup -c rollup.config.js",
3946
"build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/es"
4047
},
4148
"dependencies": {
4249
"@babel/runtime": "^7.1.2",
43-
"algoliasearch-helper": "^3.6.2",
50+
"algoliasearch-helper": "^3.7.0",
4451
"dequal": "^2.0.0",
45-
"instantsearch.js": "^4.34.0"
52+
"instantsearch.js": "^4.37.1"
4653
},
4754
"peerDependencies": {
4855
"algoliasearch": ">= 3.1 < 5",

packages/react-instantsearch-hooks/src/__tests__/compat.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { render } from '@testing-library/react';
22
import React from 'react';
3+
import { InstantSearch as InstantSearchCore } from 'react-instantsearch-core';
34

45
import { createSearchClient } from '../../../../test/mock';
5-
import { InstantSearch as InstantSearchCore } from '../../../react-instantsearch-core';
66
import { useSearchBox } from '../useSearchBox';
77
import { noop } from '../utils';
88

0 commit comments

Comments
 (0)