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

Commit f72b7d3

Browse files
committed
feat: correctly clean up webpack for react-scripts for lazy dynamic imports
1 parent 3bee1e2 commit f72b7d3

File tree

10 files changed

+73
-21
lines changed

10 files changed

+73
-21
lines changed

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module.exports = {
22
presets: ['@babel/preset-env', '@babel/preset-react'],
33
plugins: [
4+
// allow lazy loaded components with dynamic "import(...)"
5+
// https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import/
6+
'@babel/plugin-syntax-dynamic-import',
47
'@babel/plugin-proposal-class-properties',
58
// https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs
69
// loose ES6 modules allow us to dynamically mock imports during tests
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
3+
const OtherComponent = React.lazy(() => import('./OtherComponent'))
4+
5+
export default App = () => (
6+
<div className="app">
7+
<OtherComponent />
8+
</div>
9+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <div className="other">The Other Component</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import { mount } from 'cypress-react-unit-test'
3+
// import App from './App'
4+
5+
// https://github.com/bahmutov/cypress-react-unit-test/issues/136
6+
describe.skip('App loads', () => {
7+
it('renders lazy component', () => {
8+
mount(<App />)
9+
cy.contains('The Other')
10+
})
11+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default (a, b) => a + b
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://github.com/bahmutov/cypress-react-unit-test/issues/136
2+
describe.skip('dynamic import', () => {
3+
it('loads', () => {
4+
cy.wrap(import('./lazy-add'))
5+
})
6+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import { mount } from 'cypress-react-unit-test'
3+
import OtherComponent from './OtherComponent'
4+
5+
describe('Other component by itself', () => {
6+
it('renders', () => {
7+
mount(<OtherComponent />)
8+
cy.contains('The Other')
9+
})
10+
})

cypress/plugins/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path')
2-
const webpack = require('@cypress/webpack-preprocessor')
2+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
33
const babelConfig = require('../../babel.config.js')
44
// const { initPlugin } = require('cypress-plugin-snapshots/plugin')
55

@@ -39,7 +39,7 @@ const options = {
3939
}
4040

4141
module.exports = (on, config) => {
42-
on('file:preprocessor', webpack(options))
42+
on('file:preprocessor', webpackPreprocessor(options))
4343

4444
// initPlugin(on, config)
4545
return config

package-lock.json

Lines changed: 23 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
"devDependencies": {
4747
"@babel/core": "7.4.5",
4848
"@babel/plugin-proposal-class-properties": "7.4.4",
49+
"@babel/plugin-syntax-dynamic-import": "7.8.3",
4950
"@babel/plugin-transform-modules-commonjs": "7.7.5",
5051
"@babel/preset-env": "7.4.5",
5152
"@babel/preset-react": "7.0.0",
53+
"@emotion/core": "10.0.22",
5254
"@material-ui/core": "4.9.5",
5355
"@material-ui/icons": "4.5.1",
5456
"@material-ui/lab": "4.0.0-alpha.39",
@@ -69,8 +71,8 @@
6971
"prettier": "1.19.1",
7072
"pretty": "2.0.0",
7173
"prop-types": "15.7.2",
72-
"react": "16.8.6",
73-
"react-dom": "16.8.6",
74+
"react": "^16.13.1",
75+
"react-dom": "^16.13.1",
7476
"react-google-maps": "9.4.5",
7577
"react-router": "6.0.0-alpha.1",
7678
"react-router-dom": "6.0.0-alpha.1",
@@ -81,8 +83,7 @@
8183
"svg-url-loader": "3.0.3",
8284
"typescript": "3.7.3",
8385
"webpack": "4.33.0",
84-
"webpack-cli": "3.3.9",
85-
"@emotion/core": "10.0.22"
86+
"webpack-cli": "3.3.9"
8687
},
8788
"standard": {
8889
"globals": [

0 commit comments

Comments
 (0)