Ui-router / angular-hybrid doesn't work with AOT / rollup
I am trying to get ui-router to work for a hybrid angular app. It works when using JIT compilation, but when I try to get it to work with AOT and rollup the collapse step fails with mesaage error:
'@ uirouter / angular-hybrid' is imported by src \ app.module.js but cannot be resolved - treating it as an external dependency
I have opened an issue on github here . The error can be reproduced by downloading a minimal example angular-hybrid minimal example and setting up the AOT and accumulating in this example the way described in angular, as you can see from the files of this plunker . Import from '@ uirouter / angular-hybrid' is not deployable.
import { UIRouterUpgradeModule } from '@uirouter/angular-hybrid';
Does anyone have any experience to make angular-hybrid work in combination with AOT / rollup? Has anyone succeeded in this?
UPDATE
I was able to do the rolling step by adding a custom plugin to the rollup-config which allows the angular-hybrid package. But even so, the app doesn't run at runtime when angular loads and asks for UrlService. The provider for UrlService was not found with the following call (interestingly, UrlService exists among the registered injector providers, but cannot be found using the UrlService marker):
var router = injector.get(UrlService);
Here is the adjusted rollup-config file which I'm not sure is correct since DI is not working. That said, the old question of how to make the angular-hybrid drive compatible remains.
import nodeResolve from "rollup-plugin-node-resolve"
import commonjs from "rollup-plugin-commonjs";
import uglify from "rollup-plugin-uglify";
import progress from "rollup-plugin-progress";
class UIRouterHybridResolver
{
constructor(options)
{
this.options = options;
}
resolveId(id, from)
{
if (id.startsWith("@uirouter/angular-hybrid"))
{
return `${__dirname}/node_modules/@uirouter/angular-hybrid/lib/angular-hybrid.js`;
}
return null;
}
}
const uIRouterHybridResolver = (config) => new UIRouterHybridResolver(config);
export default {
entry: "src/main.js",
dest: "src/build.js", // output a single application bundle
sourceMap: false,
format: "iife",
onwarn: function (warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if (warning.code === "THIS_IS_UNDEFINED") { return; }
// console.warn everything else
console.warn(warning.message);
},
plugins: [
commonjs({
include: [
"node_modules/rxjs/**",
"node_modules/@uirouter/core/**"
]
}),
nodeResolve({ jsnext: true, module: true }),
uIRouterHybridResolver(),
uglify(),
progress({ clearLine: true })
],
globals: { angular: "angular" },
external: ["angular"]
};
source to share
You're almost there. The ultimate essence of our package rollup.js
:
a
UIRouter CORE
esm version (see What is the purpose of esm directories in Angular 2 modules? ).
This code snippet is available at node_modules\@uirouter\core\lib-esm\
( \lib-esm
end). To use it, we need to configure the plugin UIRouterHybridResolver
:
class UIRouterHybridResolver
{
resolveId(id, from)
{
if (id.startsWith("@uirouter/core"))
{
return `${__dirname}/node_modules/@uirouter/core/lib-esm/index.js`;
}
if (id.startsWith("@uirouter/angular-hybrid"))
{
return `${__dirname}/node_modules/@uirouter/angular-hybrid/lib/angular-hybrid.js`;
}
}
}
const uIRouterHybridResolver = () => new UIRouterHybridResolver();
We also need to make sure that this is the very first plugin in our exported config:
export default {
entry: "src/main.js",
dest: "src/build.js",
...
plugins: [
uIRouterHybridResolver(),
commonjs({
include: [
"node_modules/rxjs/**"
]
}),
...
NOTE. Also, no plugin
commonjs
needed (shouldn't)"node_modules/@uirouter/core/**"
, just include"node_modules/rxjs/**"
... and this ... a hybrid UIRouter with AOT works ...
EXTEND
Also, make sure we follow the instructions outlined here: https://www.npmjs.com/package/@uirouter/angular-hybrid . Basically what onlypackage.json
contains the key :@uirouter/angular-hybrid
dependencies: {
...
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/upgrade": "^4.0.0",
...
"@uirouter/angular-hybrid": "3.1.2",
...
those. don't add things like "@uirouter/angular": "1.0.0-beta.7"
etc. Finally, make sure the dependencies are up to date by callingnpm i
source to share