-
Notifications
You must be signed in to change notification settings - Fork 70
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
Doesn't work with raw-loader v2.0.0 #86
Comments
Repro is here: https://github.com/reduckted/repro-angular2-template-loader-86
That will compile the application, start the web server and open the URL in a browser. Open the developer console and you'll see the error. The result of
|
@reduckted your assumptions are absolutely correct, I've encountered the same issue today and it confused me a lot! It gets complicated even more when you want to use different loaders for styles and templates, in my case As a proof of concept, I moved code that formats require string into configuration option: function replaceStringsWithRequires(string, formatRequire = (url) => 'require(\'' + url + '\')') {
return string.replace(stringRegex, function(match, quote, url) {
if (url.charAt(0) !== '.')
url = './' + url;
return formatRequire(url);
});
} and used the following loader config: {
test: /\.(ts)$/,
exclude: /\.spec\.ts$/,
use: {
loader: 'angular2-template-loader',
options: {
formatRequire(url) {
if (url.match(/\.scss$/)) return `require('${url}')`;
else if (url.match(/\.html$/)) return `require('${url}').default`;
}
}
}
} This does solve the issue, maybe I should make a PR. @TheLarkInn do you think this is a reasonable approach for cjs/esm issue? |
As alternative, you can use a custom loader to append // fix-component-template-import-loader.js'
module.exports = function(source, sourcemap) {
this.cacheable && this.cacheable();
const newSource = source.replace(templateRegExp, (match) => `${match}.default`);
if (this.callback)
this.callback(null, newSource, sourcemap);
else
return newSource;
}; {
test: /\.(ts)$/,
exclude: /\.spec\.ts$/,
use: [
require.resolve('./fix-component-template-import-loader.js'),
'angular2-template-loader'
]
} I think this is less intrusive. |
This has been bugging me for a while, up until now I've been keeping |
Mind blown, @cornflourblue -- thanks! I can now totally get rid of my |
Since this package hasn't received any updates in 3 years we also needed a fix for this and had the same discussion in this issue storybookjs/storybook#7877 (comment) You should also read storybookjs/storybook#7877 (comment) if you need backwards compatibility |
I tried the @cornflourblue solution but still getting the error, maybe there is something I didn't configure correctly? Below is my EDIT N°1 EDIT N°2 And now my app works with Angular 8 (8.2.9). EDIT N°3 So looks like there is a lot of things happening between loaders that could lead developers (like me) to get unexpected errors while trying to run an app. |
I'm going to close this issue. The Angular CLI makes this particular loader redundant, and the repo hasn't been touched in three years, so it's fair to see this loader is no longer supported. |
A few days ago, raw-loader v2.0.0 was published. This release changed to use ES Module exports instead of CommonJS exports (webpack-contrib/raw-loader#69). I believe this has caused
angular2-template-loader
to no longer work.Everything compiles fine, but when you run the Angular application, you get the error:
I think this is because this loader is using
require(...)
, but that's now returning an object with adefault
property that contains a string, rather than just returning a string. I haven't confirmed that though. I wanted to submit this issue while it was still fresh in my mind. When I get some time later on, I'll create a simple repro and investigate a bit further.Current Workarounds:
raw-loader
to1.0.0
.The text was updated successfully, but these errors were encountered: