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

Trouble tracking down self is not defined issue #20

Open
tyler-dot-earth opened this issue Mar 10, 2017 · 8 comments
Open

Trouble tracking down self is not defined issue #20

tyler-dot-earth opened this issue Mar 10, 2017 · 8 comments
Labels

Comments

@tyler-dot-earth
Copy link

Base repo (w/ issue referred to below): https://github.com/tsnieman/webpack2test/tree/road-to-isomorphism_pothole1

Note: isomorphic-webpack works in the same repo here... the _pothole1 branch is just for illustrating the issue below. That's the only difference between the two.

I've got isomorphic-webpack working on a basic level in this test repo, but I'm having issues tracking down an issue which presents itself as the error below.

ReferenceError: self is not defined
   at isomorphic-webpack:271:30
   at isomorphic-webpack:266:47
   at module.exports (isomorphic-webpack:288:68)
   at Object.<anonymous> (isomorphic-webpack:1150:36)
   at __webpack_require__ (isomorphic-webpack:26:30)
   at Object.defineProperty.value (isomorphic-webpack:867:68)
   at __webpack_require__ (isomorphic-webpack:26:30)
   at Object.defineProperty.value (isomorphic-webpack:920:64)
   at __webpack_require__ (isomorphic-webpack:26:30)
   at Object.defineProperty.value (isomorphic-webpack:734:70)

Which seems to happen when I try to use the Home page component in the Router. Digging further, I discovered that it really only throws the self is not defined error when I include the line import styles from './Home.css'; (implicit or named import, doesn't matter).

Not, my CSS loader situation is something like this:

// webpack config
      // CSS
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true,
              importLoaders: 2,
              localIdentName: '[path]__[name]__[local]__[hash:base64:5]',
            },
          },
          'postcss-loader', // options in postcss.config.js
        ],
      },

At this point, I'm pretty deep into this and not sure how to continue to debug from the original error message. Might be two issues here:

  1. how do I debug more thoroughly from the given message? I'm using DEBUG=isomorphic-webpack but it mostly just mirrors what was spit out in the browser (original error at top).

  2. my problem of "why does importing css cause this".

Any insight into either issue would be helpful @gajus / other contributors. Thank you very much.

If it would be helpful, I would be happy to strip my repo down even more (since I might end up doing this anyway), but hoping that I pointed out all the important parts.

@gajus gajus added the question label Mar 11, 2017
@gajus
Copy link
Owner

gajus commented Mar 11, 2017

@tsnieman Are you using the error handler https://github.com/gajus/isomorphic-webpack#isomorphic-webpack-handling-errors ?

@tyler-dot-earth
Copy link
Author

@gajus Does formatErrorStack work anymore without the isomorphicMiddleware?

  1. couldn't find much about middleware for this project anymore, but see it mentioned a lot in docs

  2. I just tried adding formatErrorStack into my server code, but it didn't seem to output anything additional/different -- just the same self is not defined. Not sure if I am using this correctly either -- I'm using async/await + createCompilationPromise right now.

@gajus
Copy link
Owner

gajus commented Mar 12, 2017

The change you did seems to be correct. It is supposed to replace isomorphic-webpack:288:68 with references to the real code thats causing the problem.

@laggingreflex
Copy link

I was getting the same error and I upgraded the dependencies (especially the css-loader and style-loader, as pointed out in #20 that it's related to css/import) in both isomorphic-webpack and isomorphic-webpack-demo which solved it for me.

@tyler-dot-earth
Copy link
Author

tyler-dot-earth commented Mar 16, 2017

@laggingreflex Thanks for the idea. Though I never had issues with isomorphic-webpack-demo, and isomorphic-webpack works when I don't import CSS. Regardless, I upgraded my dependencies, but the problem still persists.

@laggingreflex
Copy link

laggingreflex commented Mar 17, 2017

Sorry it was actually downgrading the dependencies that helped, not upgrading.

The error seems to be coming from here: https://github.com/webpack-contrib/style-loader/blob/master/addStyles.js#L14.

return /msie [6-9]\b/.test(self.navigator.userAgent.toLowerCase());

Replacing self with window makes it work.

Edit: I'm only testing this out in isomorphic-webpack-demo for now, haven't actually integrated in any projects. But this change (replacing window with self for better service worker support (webpack-contrib/style-loader@9dc45a6)) might've been made in other projects (other than style-loader) as well.? It only affects server-side rendering so it might not be noticeable in those projects' tests which might be only testing in/for browser envs.

@gajus
Copy link
Owner

gajus commented Mar 17, 2017

I have left a comment with the original commit:

For the record, this broke change broke isomorphic-webpack. #20 (comment) Perhaps a window.self would have been a safer change?

– webpack-contrib/style-loader@9dc45a6#commitcomment-21377913

@tyler-dot-earth
Copy link
Author

tyler-dot-earth commented Mar 17, 2017

@laggingreflex Fantastic find! Rolling back to style-loader@0.13.1 fixed my issue! How in the world did you track that down? Impressive.

@gajus Thanks for commenting on the upstream issue.

EDIT: I suppose this is still open because the errors aren't working though, right? or?

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

3 participants