shlomiassaf / ng-router-loader

Webpack loader for NgModule lazy loading using the angular router
MIT License
46 stars 8 forks source link

Error: Can't resolve static string. Type Identifier is not allowed. #15

Closed paulsouche closed 7 years ago

paulsouche commented 7 years ago

Hi

I don't know if it is a bug or a feature but I get this error trying to do something like

import { RouterModule } from '@angular/router';

const routes = ['home', 'contact', 'about'].map((path) => ({
  path,
  loadChildren: `./src/${path}/${path}.module#${path.replace(/^(.)/, ($1) => $1.toUpperCase())}Module`
})).concat({
  path: '',
  redirectTo: 'home',
  pathMatch: 'full'
});

// const routes = [
// { path: '', loadChildren: './src/home/home.module#HomeModule' },
// { path: 'contact', loadChildren: './src/contact/contact.module#ContactModule' },
// { path: 'about', loadChildren: './src/about/about.module#AboutModule' }
// ];

export default RouterModule.forRoot(routes);

Everything works fime without map

full stackTrace

Error: Can't resolve static string. Type Identifier is not allowed.
    at resolveStringValue (\node_modules\ng-router-loader\src\ast.js:36:15)
    at resolveStringValue (\node_modules\ng-router-loader\src\ast.js:20:49)
    at resolveStringValue (\node_modules\ng-router-loader\src\ast.js:20:16)
    at resolveStringValue (\node_modules\ng-router-loader\src\ast.js:20:16)
    at resolveStringValue (\node_modules\ng-router-loader\src\ast.js:20:16)
    at Context.visitProperty (\node_modules\ng-router-loader\src\ast.js:56:33)
    at Context.invokeVisitorMethod (\node_modules\ast-types\lib\path-visitor.js:344:49)
    at Visitor.PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:196:32)
    at NodePath.each (\node_modules\ast-types\lib\path.js:101:26)
    at visitChildren (\node_modules\ast-types\lib\path-visitor.js:219:18)
    at Visitor.PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:204:20)
    at visitChildren (\node_modules\ast-types\lib\path-visitor.js:246:25)
    at Visitor.PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:204:20)
    at NodePath.each (\node_modules\ast-types\lib\path.js:101:26)
    at visitChildren (\node_modules\ast-types\lib\path-visitor.js:219:18)
    at Visitor.PVp.visitWithoutReset (\node_modules\ast-types\lib\path-visitor.js:204:20)

You can see what I want to do working on this plunker

Thanks anyway for the great job

shlomiassaf commented 7 years ago

It is not possible.

The loader takes a string and converts it into a callback that returns a module, this is how webpack works with require.ensure and now with the new import() option...

When a loader run (any loader) it will do some work and return a transformed version of the code. A loader does not execute the code.

So in your case when the loader runs it sees the code as is, not the executed, runtime, result.