【问题标题】:Angular running custom plugin after build构建后Angular运行自定义插件
【发布时间】:2020-09-09 21:41:02
【问题描述】:

我正在尝试将async-css-plugin (https://www.npmjs.com/package/async-css-plugin) 添加到 Angular 项目中。这个插件的作用是,它将 html 文件中的外部样式链接标记转换为:

<link href=app.cfadf482.css rel=stylesheet media=print onload="this.media='all'">

我知道我们可以扩展 webpack 配置,为此我使用 ngx-build-plus (https://github.com/manfredsteyer/ngx-build-plus)

我在下面添加了配置extend.webpack.config文件:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const AsyncCssPlugin = require("async-css-plugin");

module.exports = {
  plugins: [
    new HtmlWebpackPlugin(),
    new AsyncCssPlugin({ /* options */ })
  ]
};

但是现在当我运行npm run build -- --prod --extra-webpack-config extend.webpack.config.js 时,它不起作用。

它不会修改 index.html 文件。

如果我遗漏了什么,有人可以指导吗?

谢谢!

更新 这是复制回购https://github.com/andreashuber69/async-css-angular-example

这是插件源代码https://github.com/andreashuber69/async-css-plugin/blob/37e9030fc4791370cd7efaf25a7275ff425c2d36/src/AsyncCssPlugin.ts

【问题讨论】:

  • 把它改成 "customWebpackConfig": { "path": "./custom-webpack.config.js", "mergeStrategies": { "module.rules": "prepend" }, "replaceDuplicatePlugins ": 真 }
  • 试过@naveen?
  • 是的仍然没有成功,样式表标签没有更新。 :(
  • 前置应该可以工作,前置确保你的脚本最后运行。参考这个:-npmjs.com/package/@angular-builders/custom-webpack
  • 尝试从您的构建命令中删除 --extra-webpack-config 标志并保留我在上面评论中给出的配置?

标签: angular webpack html-webpack-plugin angular-builder ngx-build-plus


【解决方案1】:

更新的 Webpack 配置:-

const HtmlWebpackPlugin = require('html-webpack-plugin')
const AsyncCssPlugin = require("async-css-plugin");
var path = require('path');
module.exports = {
  output: {
    path: path.resolve(__dirname, './dist')
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new AsyncCssPlugin({ logLevel: "info" })
  ]
};

更改 Angular.json Customwebpackconfig 像:-

"customWebpackConfig": { "path": "./custom-webpack.config.js", "mergeStrategies": { "module.rules": "prepend" }, "replaceDuplicatePlugins": true }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-05-29
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多