Skip to content
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

Dependency injection does not work #1

Open
ah1508 opened this issue Dec 29, 2021 · 1 comment
Open

Dependency injection does not work #1

ah1508 opened this issue Dec 29, 2021 · 1 comment

Comments

@ah1508
Copy link

ah1508 commented Dec 29, 2021

Hi,

The sample works but dependency injection does not. Example with HttpClient

import { HttpClientModule } from "@angular/common/http";
// other imports

@Component({
  selector: "app-component",
  template: "<div>AppComponent works!</div>",
})
export class AppComponent {
  constructor(httpClient : HttpClient) {}
}

@NgModule({
  declarations: [AppComponent],
  imports: [HttpClientModule, BrowserModule],
  bootstrap: [AppComponent],
})
export class AppModule {}

Adding AngularWebPackPlugin, @ngtools/webpack and linkerPlugin solves the problem.

Complete webpack config (with webpack 5, angular 13 and type="module" in package.json) :

import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { AngularWebpackPlugin } from '@ngtools/webpack';
import linkerPlugin from '@angular/compiler-cli/linker/babel';

const __dirname = path.dirname(new URL(import.meta.url).pathname)

export default {
    entry: { polyfills: "./src/polyfills.ts", main: "./src/main.ts" },
    resolve: { extensions: [".js", ".ts"] },
    output: { path: path.resolve(__dirname, 'dist'), filename: "[name].js" },
    module: {
        rules: [
            { test: /\.ts$/, use: "@ngtools/webpack" },
            { test: /\.m?js$/, use: { loader: "babel-loader", options: { plugins: [linkerPlugin] } } }
        ]
    },
    plugins: [
        new AngularWebpackPlugin(),
        new HtmlWebpackPlugin({ template: "./src/index.html" })
    ],
    devtool: "source-map",
    devServer: {
        historyApiFallback: true
    }
};

Still, the size of a production bundle for a simple "hello world" is 455 kb while it is less than 200 kb with angular cli. Too bad that Angular documentation does not explain how to optimize that.

@marcin-wosinek
Copy link
Owner

@ah1508 thanks a lot for pointing it out! I went recently through https://www.upgradingangularjs.com/, and they show there the same thing + they show how to set up AOT for @angular/cli-free builds. Seems I'll need to publish an updated version of the original post some time:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants