-
Notifications
You must be signed in to change notification settings - Fork 12k
Dynamic require only works after hot reload / incremental build #6629
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
Comments
There may be something I'm missing here, but it doesn't appear to be logical. |
I can see how the dynamic |
It would probably help if there was a more hinting context also. For example your context is specified as "./", instead you you should try for something more like
Additionally @Brocco is correct, this isn't really the recommended way to use ContextModules with webpack. Instead you should be writing something like this. const getLazyFile = (fileName) => System.import(`./templates/${fileName}.html`);
//.....
constructor() {
getLazyFile("seanThomasLarkin").then((module) => {
doSomethingWithThis(module);
});
} Also, It would be an easy enough to write a friendly codemod or script to convert them all when the time comes. Even the cli team could write one for when TS 2.4 becomes the default. |
A different context doesn't help, I used Further, I tried import { Component } from '@angular/core';
declare var System:any;
const getLazyFile = (filename) => System.import(`./${filename}.html`);
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
foobar:string;
constructor() {
getLazyFile("foobar").then((file) => {
this.foobar = file;
});
}
}
This time an additional chunk for lazy loading was correctly created and the app works just fine. So why was the issue closed? It still exists using |
@Brocco and/or @TheLarkInn any chance this issue is getting re-evaluated? If no, could you please give me a clear answer on why this is not being considered an issue? I don't want to appear as rude but if I don't hear back in this thread, I will file the issue again, as it's blocking our team from migrating to angular-cli. |
@pierrebeaucamp We can confirm a similiar issue (#6629 (comment)). When trying to lazy load |
We think we've (@markuskeunecke) identified the problem. If you want to load dynamic files with AoT such as |
Thanks for reporting a possible solution! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug Report or Feature Request (mark with an
x
)Related issues might be #5275 and / or #4431
Versions.
Also reproducible using
@ngtools/webpack
version1.4.1
,1.4.0
,1.2.7
. I did not check any version before1.2.7
.Further, the bug was also reproduced on OSX.
Repro steps.
After running
ng new
, I modified the following files like this:src/app/app.component.html
src/app/app.component.ts
src/app/foobar.html
Then,
ng serve
. I also tested this withng eject
andnpm start
(same behavior).The log given by the failure.
Desired functionality.
localhost:4200
should show:Mention any other details that might be useful.
The really weird thing about this bug is that it only occurs on the first build of the file. While
ng serve
ornpm start
is running, I can opensrc/app/app.component/ts
and save it (without any changes). The app compiles and works just fine.Edit:
I tried using
System.import
instead ofrequire
, but the same problem still exists. Doesn't work on first build, works after saving the file whileng
is watching.Edit 2:
Explicitly disabling aot doesn't help either (using
ng serve --no-aot
,ng serve --aot false
, orng serve --aot=false
Edit 3:
I experimented a bit more with
System.import
. My component looks like this:On first compilation, the output is:
Then, if I open the file and save it, the output of webpack is:
As you can see, the incremental build includes a chunk file (and everything works as expected).
Edit 4:
ng build
and serving it using a standalone server doesn't solve the problem.Edit 5:
Since
System.import
is deprecated in webpack, I tried usingrequire.ensure
, however I get the exact behavior as previously withSystem.import
Edit 6:
I tried using typescript 2.4.0, since it supports native
import
statements. I had to change themodule
compiler option insrc/tsconfig.app.json
for it to compile, but didn't have much luck - it appears to be broken in a different way than this bug and I couldn't get it to work at all.Edit 7:
Same behavior using
require.context
.The text was updated successfully, but these errors were encountered: