Skip to content

Commit 58a737c

Browse files
authored
feat!: migrate to ESM (#2092)
* feat!: migrate to kubo-rpc-client currently testing esm module import errors with `npm run test:unit -- src/bundles/identity.test.js` * tmp: revert me.. just showing work * chore: set type=module * chore: use react-app-rewired-esm * chore: use esm import update rule from https://gist.github.com/SgtPooki/65e189531f4a5366ef4f80825feb2e5f * chore: run `npm run eslint -- --fix` * chore: lint * chore: getting tests working * chore: fix pqueue constructor error * fix: peer-locations.test.js * tmp: trying to fix identity.test.js * fix: AsyncRequestLoader.test.js * chore: fix e2e/setup/ipfs-backend.js * fix: npm start loads the webui * fix: build,start,test succeeding * fix: some e2e tests passing * fix: e2e/files.test.js * fix: e2e/ipns.test.js * fix: e2e/peers.test.js * fix: e2e/remote-api.test.js * fix: e2e/settings.test.js * fix: npm run build-storybook * fix: storybook build & test * fix(storybook): icons.stories.js * fix: storybook and e2e tests both passing * fix: lint * fix: pull package-lock.json from main and npm i * chore: use commit hash for react-app-rewired-esm * chore: fix eslint error on github CI see https://github.com/ipfs/ipfs-webui/actions/runs/4118899712/jobs/7111962669\#step:6:90 * chore: use published @sgtpooki/react-app-rewired-esm * fix: npm run test:unit:coverage * chore: fix github CI for e2e tests * chore: clean up debugging code * chore: address self-review PR comments * chore: address PR comment
1 parent fd23a23 commit 58a737c

File tree

186 files changed

+9915
-9506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+9915
-9506
lines changed

.eslintrc.js renamed to .eslintrc.cjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ module.exports = {
77
}
88
},
99
extends: ['react-app', 'standard', 'plugin:jsx-a11y/recommended'],
10-
plugins: ['jsx-a11y', 'storybook'],
10+
plugins: ['jsx-a11y', 'storybook', 'import'],
1111
// ignore .ts files because it fails to parse it.
1212
ignorePatterns: 'src/**/*.ts',
1313
rules: {
14+
'import/esm-extensions': 'error',
1415
'react/prop-types': [0, { ignore: ['className'], customValidators: [], skipUndeclared: true }] // TODO: set this rule to error when all issues are resolved.
1516
},
1617
overrides: [
1718
{
1819
files: ['src/**/*.stories.js'],
1920
excludedFiles: '*.test.js',
2021
rules: {
21-
'import/no-anonymous-default-export': 'off'
22+
'import/no-anonymous-default-export': 'off',
23+
'import/esm-extensions': 'error'
2224
}
2325
}
2426
]

.github/workflows/test-e2e.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ jobs:
3434
- name: Install dependencies
3535
run: npm ci --prefer-offline --no-audit --progress=false --cache ${{ github.workspace }}/.cache/npm
3636

37+
- name: Install playwright browsers
38+
run: npx playwright install --with-deps
39+
3740
# This is required to ensure that our code is instrumented with coverage details
3841
- name: Run test build
3942
run: npm run test:build

.storybook/main.js renamed to .storybook/main.cjs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
/* eslint-disable import/esm-extensions */
12
/**
23
* @file StoryBook configuration file
34
* @see https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#from-version-52x-to-53x
45
*/
56

67
const webpack = require('webpack')
78

8-
const { webpack: webpackOverride } = require('../config-overrides')
9-
109
/** @type {import('@storybook/core-common').StorybookConfig} */
1110
const storybookConfig = {
1211
core: {
1312
builder: 'webpack5'
1413
},
1514
reactOptions: {
16-
legacyRootApi: true
15+
legacyRootApi: false
1716
},
1817
stories: [
1918
'../src/**/*.stories.@(ts|js|tsx|jsx)'
@@ -35,11 +34,31 @@ const storybookConfig = {
3534
],
3635
features: {
3736
postcss: false,
38-
storyStoreV7: true
37+
storyStoreV7: true,
38+
babelModeV7: true
3939
},
4040
webpackFinal: async (config) => {
41+
const { webpack: webpackOverride } = (await import('../config-overrides.js')).default
42+
43+
config.module.rules.push({
44+
test: /\.(m?js)$/,
45+
type: 'javascript/auto',
46+
resolve: {
47+
fullySpecified: false
48+
}
49+
})
4150
return webpackOverride({
4251
...config,
52+
resolve: {
53+
...config.resolve,
54+
alias: {
55+
...config.resolve.alias
56+
},
57+
extensions: [
58+
...config.resolve.extensions,
59+
'dist/esm/index.js'
60+
]
61+
},
4362
// @see https://github.com/storybookjs/storybook/issues/18276#issuecomment-1137101774
4463
plugins: config.plugins.map(plugin => {
4564
if (plugin.constructor.name === 'IgnorePlugin') {

.storybook/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

.storybook/preview.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import React from 'react'
12
import { Provider } from 'redux-bundler-react'
23
import { I18nextProvider } from 'react-i18next'
34
import { DndProvider } from 'react-dnd'
45
import 'react-virtualized/styles.css'
56
import '../src/index.css'
67

7-
import getStore from '../src/bundles'
8-
import i18n from '../src/i18n'
9-
import DndBackend from '../src/lib/dnd-backend'
8+
import getStore from '../src/bundles/index.js'
9+
import i18n from '../src/i18n.js'
10+
import DndBackend from '../src/lib/dnd-backend.js'
1011

1112
/**
1213
* @type {import('@storybook/addons').BaseAnnotations}

babel.config.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module.exports = {
3+
presets: ['@babel/preset-react']
4+
}

config-overrides.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* @see https://github.com/facebook/create-react-app/issues/11756#issuecomment-1184657437
55
* @see https://alchemy.com/blog/how-to-polyfill-node-core-modules-in-webpack-5
66
*/
7-
const webpack = require('webpack')
7+
import webpack from 'webpack'
8+
89
const PURE_ESM_MODULES = [
910
'ipfs-geoip'
1011
]
@@ -67,14 +68,13 @@ function modifyBabelLoaderRuleForTest (rules) {
6768
})
6869
}
6970

70-
function webpackOverride(config) {
71+
function webpackOverride (config) {
7172
const fallback = config.resolve.fallback || {}
7273

7374
Object.assign(fallback, {
74-
assert: require.resolve('./src/webpack-fallbacks/assert'),
75-
stream: require.resolve('./src/webpack-fallbacks/stream'),
76-
os: require.resolve('./src/webpack-fallbacks/os'),
77-
path: require.resolve('./src/webpack-fallbacks/path')
75+
stream: 'stream-browserify',
76+
os: 'os-browserify/browser',
77+
path: 'path-browserify'
7878
})
7979

8080
config.resolve.fallback = fallback
@@ -87,6 +87,20 @@ function webpackOverride(config) {
8787
])
8888

8989
config.module.rules = modifyBabelLoaderRuleForBuild(config.module.rules)
90+
config.module.rules.push({
91+
test: /\.jsx?$/,
92+
exclude: /(node_modules|bower_components)/,
93+
loader: 'babel-loader',
94+
options: { presets: ['@babel/env', '@babel/preset-react'] }
95+
})
96+
97+
config.module.rules.push({
98+
test: /\.m?js$/,
99+
type: 'javascript/auto',
100+
resolve: {
101+
fullySpecified: false
102+
}
103+
})
90104

91105
// Instrument for code coverage in development mode
92106
const REACT_APP_ENV = process.env.REACT_APP_ENV ?? process.env.NODE_ENV ?? 'production'
@@ -97,7 +111,22 @@ function webpackOverride(config) {
97111
return config
98112
}
99113

100-
module.exports = {
114+
const configOverride = {
101115
webpack: webpackOverride,
102-
jest: (config) => config
116+
jest: (config) => {
117+
/**
118+
* @type {import('jest').Config}
119+
*/
120+
return ({
121+
...config,
122+
setupFiles: [...config.setupFiles, 'fake-indexeddb/auto'],
123+
moduleNameMapper: {
124+
...config.moduleNameMapper,
125+
'multiformats/basics': '<rootDir>/node_modules/multiformats/cjs/src/basics.js',
126+
'multiformats/bases/(.*)$': '<rootDir>/node_modules/multiformats/cjs/src/bases/$1.js'
127+
}
128+
})
129+
}
103130
}
131+
132+
export default configOverride

0 commit comments

Comments
 (0)