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

Dynamic import #32

Merged
merged 3 commits into from
Oct 8, 2018
Merged

Dynamic import #32

merged 3 commits into from
Oct 8, 2018

Conversation

trotzig
Copy link
Contributor

@trotzig trotzig commented Oct 8, 2018

By default, webpack will treat a dynamic import as a split point,
meaning it will try to download a bundle as part of evaluating an
import(somePath) statement. This won't work for Happo -- since there's
no running server, people end up with something like this:

  Error: Could not load script: "http://localhost/92.happo-bundle-react-L3Jvb3Qvc3R1ZGlv.js"
      at onErrorWrapped (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/per-document-resource-loader.js:39:19)
      at Object.check (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/resource-queue.js:58:23)
      at request.then.catch.err (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/resource-queue.js:104:14)

To work around this, I'm using the babel-plugin-dynamic-import-node
babel plugin. Thanks to @ljharb for making the suggestion and writing
the plugin. This plugin will transpile import() into a (deferred)
regular require, which is all we need to avoid the split point in
webpack.

While adding a test for this, I had to modify eslint a little, plus add
a babel plugin to make the dynamic imports recognized as valid syntax.

Fixes #31

Now that the organization is named "happo", it's time to remove old
references.
I added this plugin a while ago, but forgot to mention it here.
By default, webpack will treat a dynamic import as a split point [1],
meaning it will try to download a bundle as part of evaluating an
`import(somePath)` statement. This won't work for Happo -- since there's
no running server, people end up with something like this:

  Error: Could not load script: "http://localhost/92.happo-bundle-react-L3Jvb3Qvc3R1ZGlv.js"
      at onErrorWrapped (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/per-document-resource-loader.js:39:19)
      at Object.check (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/resource-queue.js:58:23)
      at request.then.catch.err (/root/studio/node_modules/jsdom/lib/jsdom/browser/resources/resource-queue.js:104:14)

To work around this, I'm using the babel-plugin-dynamic-import-node [2]
babel plugin. Thanks to @ljharb for making the suggestion and writing
the plugin. This plugin will transpile `import()` into a (deferred)
regular `require`, which is all we need to avoid the split point in
webpack.

While adding a test for this, I had to modify eslint a little, plus add
a babel plugin to make the dynamic imports recognized as valid syntax.

[1] https://webpack.js.org/api/module-methods/#import-
[2] https://www.npmjs.com/package/babel-plugin-dynamic-import-node
@trotzig trotzig merged commit edb8511 into master Oct 8, 2018
@trotzig trotzig deleted the dynamic-import branch October 8, 2018 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant