Closed bisubus closed 7 years ago
@bisubus Hi
Sorry for the late response.
This is not an ngc-webpack issue, it is just how the AOT compiler works.
The AOT compiler does not know about webpack and it expects client code, sending require
to it will make it fail.
ngc-webpack does not process your code, the AOT compiler does (actually it's TS) and both AOT compiler and TS doesn't know about require
But it cannot be easily used in JIT.
This is not correct, use angular2-template-loader
as done in angular-starter
This will work in JIT and AOT.
Use it and it will solve the issue, this is exactly the purpose of that loader.
@shlomiassaf Thank you for commenting on this. Are you certain about angular2-template-loader and the way it works with ngc-webpack? I have it in my config currently, but I honestly don't remember how it affected AOT/JiT build.
IIUC, all angular2-template-loader is supposed to do is transforming styleUrls: ['./foo-bar.scss']
to styles: [require('./foo-bar.scss')]
, isn't it? Why is it expected to work then, while using styles: [require('./foo-bar.scss')]
directly doesn't work?
You forget that AOT build process has 2 steps.
First step is the AOT compiler going over TS files any creating ngfactory files from them, this is pure TS code generation and does not involve webpack and exactly why require does not work. It's also why you need to write only a string literal for the path.
The template loader converts that string path into a require statement and this happens in the 2nd step, where webpack bundles everything.
Funny thing but the template loader is only for JIT. In AOT all of the data is already in the metadata ngfactory files that the AOT compiler creates
Sent from my iPhone
On Jul 31, 2017, at 12:36 AM, bisubus notifications@github.com wrote:
@shlomiassaf Thank you for commenting on this. Are you certain about angular2-template-loader and the way it works with ngc-webpack? I have it in my config currently, but I honestly don't remember how it affected AOT/JiT build.
IIUC, all angular2-template-loader is supposed to do is transforming styleUrls: ['./foo-bar.scss'] to styles: [require('./foo-bar.scss')], isn't it? Why is it expected to work then, while using styles: [require('./foo-bar.scss')] directly doesn't work?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
@shlomiassaf The explanation makes sense, thanks. I hope the issue is solved for me.
Loader chain itself works fine, and
const style = require('./foo-bar.scss')
results in a string, the problem looks like compilation precedes loader chain whenrequire
is used.Having
styleUrls
for component styles works well withngc-webpack
:But it cannot be easily used in JIT.
And having
require
for component styles likeresults in Webpack error:
Debugging
assertArrayOfStrings
showed thatstyles: [require('./foo-bar.scss')]
is evaluated in compiler tostyles: [null]
, no matter how style loaders are configured. It can bestyles: [require('raw-loader!foo-bar.html')]
orstyles: [require('non-existing-loader!non-existing-file')]
, the result is the same. Loaders just aren't considered at the moment when compiler runs.The setup is loosely based on https://github.com/AngularClass/angular2-webpack-starter and is quite ordinary, here are the most relevant details:
package.json
webpack.config.js
tsconfig.json