简体   繁体   English

构建 Angular 库时是否可以使用自定义 Webpack 配置?

[英]Is it possible to use a custom Webpack Configuration when building an Angular Library?

I have an Angular 8 library ( not application ) what is shared by other Angular Applications that I want to pass some ENV variables to.我有一个 Angular 8 库(不是应用程序),我想将一些 ENV 变量传递给其他 Angular 应用程序共享的内容。 It seems the best way to do this might be via webpack, but I don't see any way to customize the library that is being built.似乎最好的方法可能是通过 webpack,但我看不到任何自定义正在构建的库的方法。

{
  loader: 'sass-loader',
  options: {
    prependData: '$env: ' + process.env.NODE_ENV + ';',
  }
}

I know I can customize a Angular applications' Webpack configuration pretty easily.我知道我可以很容易地自定义 Angular 应用程序的 Webpack 配置。

Any advice?有什么建议吗?

Also I'm sure someone will ask (Why do you want to do this?) okay fair enough question.另外我相信有人会问(你为什么要这样做?)好吧,这个问题很公平。 Because I want to set an Environmental variable that will be used in my SCSS files when they are build for the library.因为我想设置一个环境变量,当它们为库构建时,它将在我的 SCSS 文件中使用。

Angular libraries do not use Webpack they use a different kind of build system called build-ng-packagr Angular 库不使用 Webpack 他们使用不同类型的构建系统,称为build-ng-packagr

You can use custom webpack , then use customWebpackConfig in your angular.json like that您可以使用 自定义 webpack ,然后在angular.json中使用customWebpackConfig

"architect": {
  ...
  "build": {
    "builder": "@angular-builders/custom-webpack:browser"
    "options": {
      "customWebpackConfig": {
        "path": "./custom-webpack.config.js",
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM