angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

How to configure Webpack loaders

Open httpdigest opened this issue 10 years ago • 13 comments

This is just a simple question, I hope: How do I configure webpack 2 loaders with angular-cli? I need to pack also .json files using the file loader. But whenever I try to import "my.json"; the ng build process exits with You may need an appropriate loader to handle this file type. But when I run ng init projectname there is no webpack.config.js to edit.

httpdigest avatar Sep 21 '16 11:09 httpdigest

@httpdigest if you find anything on this, please let me know too, i will do the same if i find something related on this. thanks

mirwaqaskhan avatar Sep 21 '16 11:09 mirwaqaskhan

I just had a look at angular-cli's internal sources (under ~/AppData/Roaming/npm/node_modules/angular-cli/models/webpack-build-common.js), and it looks like it already is able to handle .json files. I just needed to npm install the json-loader.

EDIT: I don't know, however, how one can augment or completely replace the internal webpack configuration file.

httpdigest avatar Sep 21 '16 11:09 httpdigest

Oh, the actual use-case for me happened to be able to just copy some files into the dist folder. We apparently did this in the past via the webpack file or raw loader and doing a require() on it so that webpack would pick the file up/recognize it. Is there any other way to tell angular-cli and ultimately webpack to pick up files and copy them to the dist folder?

httpdigest avatar Sep 21 '16 12:09 httpdigest

Put them in the src/assets folder and in your angular-cli.json have this config:

"apps": [
        {
            "root": "src",
            "outDir": "dist",
            "assets": "assets"
....

alexciesielski avatar Sep 21 '16 12:09 alexciesielski

Thanks alot! Did not know about that assets folder. Is there a way to avoid angular-cli creating the "assets" folder inside the "dist" folder, but instead just copy the content of src/assets into dist, much like cp src/assets/* dist/ ?

httpdigest avatar Sep 21 '16 12:09 httpdigest

I don't know any option like that. Although I've read somewhere that when nearing final status, the CLI team will look into ways to implement addons or more customization options, so I think it is just a matter of time.

alexciesielski avatar Sep 21 '16 12:09 alexciesielski

Okay, thank you. I will stay with the best-practice approach of having the "assets/" folder, then.

httpdigest avatar Sep 21 '16 12:09 httpdigest

Hi, sorry if the question is too stupid, but after moving the json files to the assets folder, how do I import them in my component?

I'm trying so:

import * as translationEN from '../assets/resources/locale-en.json'; import * as translationES from '../assets/resources/locale-es.json';

But I'm getting errors:

ERROR in [default] ....\workspaceFrontend\src\app\app.component.ts:10:31 Cannot find module '../assets/resources/locale-en.json'.

ERROR in [default] ....\workspaceFrontend\src\app\app.component.ts:11:31 Cannot find module '../assets/resources/locale-es.json'.

Folders structure: workspaceFrontend\src\app... workspaceFrontend\src\assets\resources... workspaceFrontend\src\assets\fonts... workspaceFrontend\src\assets\images... workspaceFrontend\src\assets...

ng --version angular-cli: 1.0.0-beta.18 node: 6.9.1 os: win32 x64

tsc -v Version 2.0.0

npm -v 3.10.6

thanks!

sdeuvarow avatar Oct 27 '16 23:10 sdeuvarow

There is a solution for custom loaders? I wan't to use custom loader for a specific type of files. Can't use this at the moment because angular-cli does not let you change the Webpack config file. My only solution at the moment is to use myLoader!myFile when importing the file...

dotansimha avatar Dec 05 '16 09:12 dotansimha

@sdeuvarow Did you find a solution to that problem? I want to load the .json files containing the translated strings dynamically at runtime using the angular-cli.

tschuege avatar Dec 30 '16 16:12 tschuege

Hi tschuege, I import the files by:

var enlang = require("../assets/lang-en.json");
var eslang = require("../assets/lang-es.json");

Then I'm using them so:

    this.locale.addLanguage('en');
    this.localization.addTranslation('en', enlang);

    this.locale.addLanguage('es');
    this.localization.addTranslation('es', eslang);

I think that is all I do ..

sdeuvarow avatar Dec 30 '16 16:12 sdeuvarow

@sdeuvarow I also faced the same problem, but I was able to solve it with reference to this article. It may be too late ..

TypeScript Officeal Document: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#wildcard-character-in-module-names

tomof avatar May 03 '17 01:05 tomof

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.