的WebPack - 仅注入一个文件头和所有其他的身体

问题描述 投票:0回答:1

我不能让这个解决方案,什么是最好的方法呢?我试图用的WebPack angular2使用HtmlWebpackPlugin

这是我的设置:

new HtmlWebpackPlugin({
    template: 'src/index.html',
    chunks: {
        "head": {
            "entry": "assets/js/ie9.js"
        }
    }
}),
webpack html-webpack-plugin
1个回答
0
投票

你需要所有的事情是:

你应该把这个在index.html的头

<head>
.
.
.
  <% if (webpackConfig.htmlElements.headTags) { %>
  <!-- Configured Head Tags  -->
      <%= webpackConfig.htmlElements.headTags %>
  <% } %>
.
.
.
</head>

在您的webpack.common.js:

 new HtmlWebpackPlugin({
    template: 'src/index.html',
    title: METADATA.title,
    chunksSortMode: 'dependency',
    metadata: METADATA,
    inject: 'head'
  }),

  new HtmlElementsPlugin({
    headTags: require('./head-config.common')
  })

在webpack.dev.js添加这些代码与webpack.common合并

 const commonConfig = require('./webpack.common.js');
 const webpackMerge = require('webpack-merge'); // used to merge webpack configs
 const webpackMergeDll = webpackMerge.strategy({plugins: 'replace'});

 const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, {
  host: HOST,
  port: PORT,
  ENV: ENV,
  HMR: HMR
});


module.exports = function (options) {
  return webpackMerge(commonConfig({env: ENV}), {
  .
  .
  .
  }
}

添加头部config.common.js文件并写入这些代码(例如):

            module.exports = {
              link: [
                /** <link> tags for 'apple-touch-icon' (AKA Web Clips). **/
                { rel: 'apple-touch-icon', sizes: '57x57', href: '/assets/icon/apple-icon-57x57.png' },
                { rel: 'apple-touch-icon', sizes: '60x60', href: '/assets/icon/apple-icon-60x60.png' },
                { rel: 'apple-touch-icon', sizes: '72x72', href: '/assets/icon/apple-icon-72x72.png' },
                { rel: 'apple-touch-icon', sizes: '76x76', href: '/assets/icon/apple-icon-76x76.png' },
                { rel: 'apple-touch-icon', sizes: '114x114', href: '/assets/icon/apple-icon-114x114.png' },
                { rel: 'apple-touch-icon', sizes: '120x120', href: '/assets/icon/apple-icon-120x120.png' },
                { rel: 'apple-touch-icon', sizes: '144x144', href: '/assets/icon/apple-icon-144x144.png' },
                { rel: 'apple-touch-icon', sizes: '152x152', href: '/assets/icon/apple-icon-152x152.png' },
                { rel: 'apple-touch-icon', sizes: '180x180', href: '/assets/icon/apple-icon-180x180.png' },

                /** <link> tags for android web app icons **/
                { rel: 'icon', type: 'image/png', sizes: '192x192', href: '/assets/icon/android-icon-192x192.png' },

                /** <link> tags for favicons **/
                { rel: 'icon', type: 'image/png', sizes: '32x32', href: '/assets/icon/favicon-32x32.png' },
                { rel: 'icon', type: 'image/png', sizes: '96x96', href: '/assets/icon/favicon-96x96.png' },
                { rel: 'icon', type: 'image/png', sizes: '16x16', href: '/assets/icon/favicon-16x16.png' },

                /** <link> tags for a Web App Manifest **/
                { rel: 'manifest', href: '/assets/manifest.json' }
              ],
              meta: [
                { name: 'msapplication-TileColor', content: '#00bcd4' },
                { name: 'msapplication-TileImage', content: '/assets/icon/ms-icon-144x144.png', '=content': true },
                { name: 'theme-color', content: '#00bcd4' }
              ]
            };

你也可以安装角CLI与新公共管理和运行这个命令来自动添加的WebPack文件:

 npm install angular-cli --save-dev

安装后运行:

 ng-eject

添加的WebPack配置文件

© www.soinside.com 2019 - 2024. All rights reserved.