I want to use a raw-loader with my Angular 7 to import text files into my TypeScript source code. I\'ve found plenty of information on the subject and spent a few hours tryi
Working in Angular 9,10 with Ivy
So I finally got it working, from this comment on an issue, here's the steps I took, if anyone else is looking for help.
yarn add -D @angular-builders/custom-webpack raw-loader
Change angular.json to use @angular-builders/custom-webpack
...
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.partial.js"
},
...
"build": {
"builder": "@angular-builders/custom-webpack:dev-server",
"options": {
"browserTarget": "PROJECTNAME:build"
},
...
webpack.partial.js next to angular.jsonmodule.exports = {
module: {
rules: [
{ test: /\.(txt|md)$/, loader: 'raw-loader' }
]
}
};
./types/textfile.d.tsdeclare module '!raw-loader!*' {
const contents: string;
export = contents;
}
tsconfig file knows about textfile.d.ts{
"compilerOptions": {
...
"typeRoots": ["node_modules/@types", "./types"],
... // ^ this is important
}
import { Component } from '@angular/core';
const myText = require('!raw-loader!./my-text-file.txt');
@Component({
template: `{{myText}}`,
})
export class ChangeLogComponent {
myText = myText;
}