Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Splitting Support #1056

Closed
donnieflorence opened this issue Nov 17, 2016 · 17 comments
Closed

Code Splitting Support #1056

donnieflorence opened this issue Nov 17, 2016 · 17 comments

Comments

@donnieflorence
Copy link

Is it viable to support Code Splitting with CRA?

@gaearon
Copy link
Contributor

gaearon commented Nov 17, 2016

It's already supported with require.ensure. We don't document that because we're waiting until Webpack 2 is ready. When it's ready, we'll switch to it and document System.import() instead.

@gaearon
Copy link
Contributor

gaearon commented Dec 7, 2016

Please see #1192 if you'd like to help!

@denkristoffer
Copy link
Contributor

Is this possible now?

@chemitaxis
Copy link

Hi, any news about that? Thanks!

@Timer
Copy link
Contributor

Timer commented Mar 16, 2017

A 0.10 alpha will be out soon with import support. In the mean time, you can use require.ensure. Simply polyfill it in your test environment.

@chemitaxis
Copy link

Sorry, but what do you mean with polyfill it in the test environment? Thanks

@Timer
Copy link
Contributor

Timer commented Mar 16, 2017

Sorry I wasn't more clear.

Currently, you can use require.ensure in dev & prod and it will work fine.

If you want it to work in Jest (npm test) you will need to add something like this to index.js:

if (process.env.NODE_ENV === 'test') {
  require.ensure = (deps, cb) => cb(require);
}

@chemitaxis
Copy link

chemitaxis commented Mar 16, 2017 via email

@Timer
Copy link
Contributor

Timer commented Mar 16, 2017

I'm not sure I understand, sorry!

@GautierT
Copy link
Contributor

Do you have any ressources/tutorial to install code splitting on a CRA app with react-router ?
Can't find anything without webpack config... Thanks !

@gaearon
Copy link
Contributor

gaearon commented Apr 13, 2017

This is not using React Router but should give you an idea: #1968 (comment).

I’m not really sure why you can’t find examples without webpack config changes because code splitting in webpack works out of the box. So it’s not necessary to change anything.

@GautierT
Copy link
Contributor

Thanks @gaearon but even after putting my route this way :

	return (
		<Route path="/" getComponent={(location, callback) => {
			require.ensure([], (require) => {
				callback(null, require('./containers/Parent').default)
			}, 'Parent')
		}}>
			<Route path="/cgu" getComponent={(location, callback) => {
				require.ensure([], (require) => {
					callback(null, require('./containers/CGU').default)
				}, 'CGU')
			}}/>
			<IndexRoute getComponent={(location, callback) => {
				require.ensure([], (require) => {
					callback(null, require('./containers/Home').default)
				}, 'HomePage')
			}}/>

			<Route path="/authCallback" getComponent={(location, callback) => {
				require.ensure([], (require) => {
					callback(null, require('./containers/AuthCallback').default)
				}, 'Auth')
			}}/>
			<Route path="/produit" getComponent={(location, callback) => {
				require.ensure([], (require) => {
					callback(null, require('./containers/landing/Produit').default)
				}, 'Produit')
			}}/>
			
		</Route>
			
		)

I still see only one js chunk...

Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  541.27 KB (+17 B)  build/static/js/main.65e37619.js
  93.09 KB           build/static/css/main.9751ff5b.css

@gaearon
Copy link
Contributor

gaearon commented Apr 13, 2017

Please provide a full project reproducing the problem.

@a-x-
Copy link

a-x- commented Apr 24, 2017

what about import('package').then(package => {})?

it's more convenient rather than require.ensure for my taste

"react-scripts": "^0.9.5"

webpackHotDevClient.js:233 Error in ./src/pages/new-account.jsx
Syntax error: Unexpected token, expected { (12:6)

  10 | };
  11 | 
> 12 | import('../data/account-parts-tree.json').then(accountPartsTree => {console.log('tree', accountPartsTree)})
     |       ^
  13 | 
  14 | export default class NewAccount extends Component {
  15 |   constructor(props) {

@Timer
Copy link
Contributor

Timer commented Apr 24, 2017

@a-x- using import() is only available on 0.10. You can upgrade to the alpha if you'd like to start using it now.

Please note CSS isn't minified in that alpha.

@Viral-Inc
Copy link

if anyone has an update on the timing of the official switch to System.import() please let me know :) :)

@armand1m
Copy link

I'm trying to use import() using react-script@1.0.9 but I've got the same error as @a-x-

I'm trying to go according the docs about this.

@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants