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

Won't work if I use webpack to compiled code for server side #181

Closed
abramstyle opened this issue Dec 11, 2018 · 6 comments
Closed

Won't work if I use webpack to compiled code for server side #181

abramstyle opened this issue Dec 11, 2018 · 6 comments
Labels

Comments

@abramstyle
Copy link
Contributor

🐛 Bug Report

I compiled my code for server side rendering using webpack. Webpack converted require into __webpack_require__ so the code is broken because the statsFile is dynamic require.
I found a solution to make it work, just change the require to __non_webpack_require__ in the file utils https://github.com/smooth-code/loadable-components/blob/1e073267ffa3f7d12351651a7357ca8707df801a/packages/server/src/util.js#L9

So may be we can check if the code in the webpack environment then decide use require or __non_webpack_require__.

To Reproduce

const statsFile = `${config.build.target}/loadable-stats.json`;
const extractor = new ChunkExtractor({ statsFile });

Expected behavior

Code run as normal

Link to repl or repo (highly encouraged)

Run npx envinfo --system --binaries --npmPackages @loadable/component,@loadable/server,@loadable/webpack-plugin,@loadable/babel-plugin --markdown --clipboard

Paste the results here:

## System:
 - OS: macOS 10.14.1
 - CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
 - Memory: 331.79 MB / 16.00 GB
 - Shell: 5.3 - /bin/zsh
## Binaries:
 - Node: 8.11.1 - /usr/local/bin/node
 - Yarn: 1.10.1 - /usr/local/bin/yarn
 - npm: 6.4.1 - /usr/local/bin/npm
## npmPackages:
 - @loadable/babel-plugin: ^5.0.1 => 5.0.1
 - @loadable/server: ^5.2.0 => 5.2.0
 - @loadable/webpack-plugin: ^5.2.1 => 5.2.1
@gregberge
Copy link
Owner

Hello @abramstyle, thanks for your issue. I submitted a PR, I use eval("module.require"), same trick used for Babel plugin. Does it look good for you?

@abramstyle
Copy link
Contributor Author

Hi @neoziro , I changed the code, It works fine now. Thanks.

@abramstyle
Copy link
Contributor Author

abramstyle commented Dec 12, 2018

Hey @neoziro, I found a better way to solve this problem. Just add @loadable/server to webpack config file like this:

{
  // other configs...
  externals: {
    '@loadable/server': 'commonjs @loadable/server'
  }
}

Then webpack would not to compile the code from package '@loadable/server'.

Sorry to waste your time looking for a solution, May be the PR should be closed now.

@abramstyle abramstyle reopened this Dec 12, 2018
@gregberge
Copy link
Owner

I think I will merge the PR, it is better if it works out of the box.

@abramstyle
Copy link
Contributor Author

OK, understood. I will close this issue now.

@gregberge
Copy link
Owner

gregberge commented Dec 12, 2018

@abramstyle OK, merged and released anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants