SpoonX / aurelia-authentication

Authentication plugin for aurelia.
http://aurelia-authentication.spoonx.org
MIT License
90 stars 60 forks source link

ReferenceError: AuthService is not defined #406

Closed ekzobrain closed 3 years ago

ekzobrain commented 5 years ago

Hi!

I get this runtime error in Google Chrome when targeting es2015 build in Webpack (https://github.com/aurelia/webpack-plugin/wiki/Minimize-size#use-es6-if-you-can). Backtrace:

ReferenceError:  AuthService is not defined
    at Module.eval [as AuthService] (aurelia-authentication.js?f62e:1)
    at eval (authenticatedValueConverter.js?b58f:6)
    at Module.tY+i (vendor.aurelia-authentication~11a2131f.475434d374d8067341f2.chunk.js:47)
    at __webpack_require__ (runtime~app.475434d374d8067341f2.bundle.js:84)
    at eval (aurelia-authentication.js?f62e:1)
    at Module.aurelia-authentication (vendor.aurelia-authentication~11a2131f.475434d374d8067341f2.chunk.js:23)
    at __webpack_require__ (runtime~app.475434d374d8067341f2.bundle.js:84)
    at eval (app:3)
    at Module.app (app~d0ae3f07.475434d374d8067341f2.chunk.js:100)
    at __webpack_require__ (runtime~app.475434d374d8067341f2.bundle.js:84)

In Firefox this error is reported as: "ReferenceError: "can't access lexical declaration `AuthService' before initialization"

Thanks in advance, Dmitry.

RWOverdijk commented 5 years ago

Hello Dmitry,

I've never seen this error before myself. Chances are you'll have to find a solution for this as I'm not actively developing these modules anymore (I just maintain them) but I would be more than happy to accept a PR once you find the solution. Otherwise I'm pretty sure the recommended path works fine (a lot of installs and no complaints seem to verify this).

ekzobrain commented 5 years ago

Hi, @RWOverdijk

I experimentally found that it happens because of these lines (9-11) is source code:

// added for bundling
import {AuthFilterValueConverter} from './authFilterValueConverter'; // eslint-disable-line no-unused-vars
import {AuthenticatedFilterValueConverter} from './authenticatedFilterValueConverter'; // eslint-disable-line no-unused-vars
import {AuthenticatedValueConverter} from './authenticatedValueConverter'; // eslint-disable-line no-unused-vars

They lead to recursive imports in es2015 build (aurelia-authentication.js imports authFilterValueConverter.js at the top, which imports AuthService from aurelia-authentication.js, which is not yet defined at that time). Do you have ideas how to quickly fix this?

RWOverdijk commented 5 years ago

I don't know actually. It's strange that you're the first to have this issue. Perhaps it's a configuration issue? Otherwise, one of the two needs to get the other injected which is not really a quick fix I'm afraid.

MaximBalaganskiy commented 5 years ago

I see this as well, es2015 dist folder is unusable

MaximBalaganskiy commented 5 years ago

The reason not many people are seeing this is that most use aurelia-plugin's default dist: "native-modules" which does not have such a problem. Once you start producing es6 bundles it becomes an issue

MaximBalaganskiy commented 5 years ago

Removing those imports from spoonx.js did not visibly do any damage to the build

MaximBalaganskiy commented 5 years ago

As a temporary workaround with webpack you can use

resolve: {
    alias: {
    "aurelia-authentication": "aurelia-authentication/dist/native-modules"
    }
}
RWOverdijk commented 5 years ago

@MaximBalaganskiy :+1: nice find.

What do you recommend doing here? I've been out of the loop for a bit and went into maintenance mode for these plugins. Just removing the imports doesn't sound very compelling to me. @doktordirk Any input?

MaximBalaganskiy commented 5 years ago

The only reason for those imports to be there, as far as I can see, is to copy the files to the dist folder. Removing them does not change it - the files are still copied, so I still think this is the best solution.

RWOverdijk commented 5 years ago

It has been a while, but I remember that back then all of this was necessary to support different types of packagers. If everything still works it's fine by me. @doktordirk Do you remember?

MaximBalaganskiy commented 5 years ago

Webpack would find those via PLATFORM.moduleName. Not sure about the rest...

RWOverdijk commented 5 years ago

I'm not against getting a fix in. But since I don't actively use this anymore, and this is used in production by others, I need to be extra careful.

As long as there are tests to back it up we can definitely push changes though! :)

MaximBalaganskiy commented 5 years ago

i've just tested why converters are not included... the offender is the method they are added to global resources - dynamically from the config, Neither webpack nor CLI can pick them up unless there are explicit strings similar to

frameworkConfig.globalResources(PLATFORM.moduleName('./authFilterValueConverter'));
MaximBalaganskiy commented 5 years ago

I've just tested this in the latest CLI and WP projects