Skip to content

fix(addEntries): only add client entry to web targets#1775

Merged
alexander-akait merged 2 commits into
webpack:masterfrom
mlrawlings:universal-inline
Apr 17, 2019
Merged

fix(addEntries): only add client entry to web targets#1775
alexander-akait merged 2 commits into
webpack:masterfrom
mlrawlings:universal-inline

Conversation

@mlrawlings

@mlrawlings mlrawlings commented Apr 9, 2019

Copy link
Copy Markdown
Contributor
  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Yes

Motivation / Use-Case

When using an universal/isomorphic multicompiler setup and using the default setting of inline:true, the client runtime gets injected into the entries for each compilation. However, as far as I can tell, the client runtime is only designed to work in the browser (target:"web" or target:"webworker").

With target:"node", the following error is thrown:

urlParts.port = self.location.port;
                ^
ReferenceError: self is not defined

From

urlParts.port = self.location.port;

Changes

This PR updates addEntries to check the compilation target for each compiler. Since the client runtime is only designed for web targets, it is only prepended to those compilations. The HMR runtimes are still prepended for all compile targets (when options.hot or options.hotOnly are set), because as far as I can tell, these should work regardless of the compilation target.

Breaking Changes

None, if my assumptions are correct.

Additional Info

Related: #1256 #1338 #1441

@jsf-clabot

jsf-clabot commented Apr 9, 2019

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Apr 9, 2019

Copy link
Copy Markdown

Codecov Report

Merging #1775 into master will increase coverage by 0.2%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #1775     +/-   ##
=========================================
+ Coverage   87.58%   87.78%   +0.2%     
=========================================
  Files           9        9             
  Lines         596      606     +10     
  Branches      179      184      +5     
=========================================
+ Hits          522      532     +10     
  Misses         62       62             
  Partials       12       12
Impacted Files Coverage Δ
lib/utils/addEntries.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 58d1682...ca0334d. Read the comment docs.

@alexander-akait alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR, looks very good 👍

@alexander-akait

Copy link
Copy Markdown
Member

/cc @hiroppy

Comment thread lib/utils/addEntries.js

@alexander-akait alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting note

@mlrawlings

mlrawlings commented Apr 9, 2019

Copy link
Copy Markdown
Contributor Author

@evilebottnawi I see your concern and I also realized the target can also be a function which complicates things.

I've just pushed a commit that adds two options (plus tests): inlineClient and inlineHot. These options control whether, when inline is true, which configurations get the client runtime and hot runtimes inlined.

new DevServer(compiler, {
  /* default: web targets only */
  inlineClient: (config) => (
    config.target === 'web' ||
    config.target === 'webworker' ||
    config.target == null
  ) 
});
new DevServer(compiler, {
  /* add to all compilers */
  inlineClient: true 
});
new DevServer(compiler, {
  /* add to no compilers */
  inlineClient: false 
});

This would allow users to choose to inline based on any config value. The name option in particular could be useful to target specific compilations.

new DevServer(compiler, {
  /* add to all compilers (as a function) */
  inlineClient: (config) => config.name !== 'Server' 
});

Most users shouldn't ever have to set these options, but they're there if someone needs them. For the vast majority of users, webpack-dev-server should continue to work out-of-the-box.

@alexander-akait

Copy link
Copy Markdown
Member

Looks good, but inlineClient and inclideHot is very misleading, maybe injectClient and injectHot?

@mlrawlings

Copy link
Copy Markdown
Contributor Author

Updated. I like inject better as well.

@alexander-akait

Copy link
Copy Markdown
Member

/cc @hiroppy

@hiroppy hiroppy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed, LGTM. Thank you!

@hiroppy

hiroppy commented Apr 16, 2019

Copy link
Copy Markdown
Contributor

@mlrawlings Could you rebase?

@hiroppy

hiroppy commented Apr 16, 2019

Copy link
Copy Markdown
Contributor

BTW, I'll add an example for this after this pr is merged.

@alexander-akait alexander-akait merged commit cf4d0d0 into webpack:master Apr 17, 2019
@alexander-akait

Copy link
Copy Markdown
Member

Thanks great job!

@EECOLOR

EECOLOR commented Apr 29, 2019

Copy link
Copy Markdown

Awesome, it took a few hours to see this was the problem, but I was very happy to see it has already been solved and merged. Any indication on when this will be released?

Thanks!

For future reference, the error I received:

 ReferenceError: self is not defined

@EECOLOR

EECOLOR commented May 8, 2019

Copy link
Copy Markdown

Does it make sense to still have the HotModuleReplacementPlugin injected for non web targets?

@FallingSnow

Copy link
Copy Markdown

I'm running into this issue while using web audio workers at

self.addEventListener("beforeunload", () => {
status.isUnloading = true;
});

ReferenceError: self is not defined

I have tried the following in my webpack config but the issue remains.

output: {
  globalObject: "globalThis"
}

I also tried setting the target to webworker but the error persists.

@snitin315

snitin315 commented Sep 1, 2023

Copy link
Copy Markdown
Member

@FallingSnow Can you please create an issue with proper steps to reproduce?

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.

7 participants