webpack - 只向头部注入一个文件,向身体注入所有其他文件

时间:2017-05-10 15:24:26

标签: webpack html-webpack-plugin

我无法得到这个解决方案,最好的方法是什么?我试图使用HtmlWebpackPlugin和webpack angular2

这是我的设置:

new HtmlWebpackPlugin({
    template: 'src/index.html',
    chunks: {
        "head": {
            "entry": "assets/js/ie9.js"
        }
    }
}),

1 个答案:

答案 0 :(得分: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}), {
  .
  .
  .
  }
}

添加 head-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' }
              ]
            };

你也可以用npm安装angular-cli并运行这个命令来自动添加webpack文件:

 npm install angular-cli --save-dev

并在安装后运行:

 ng-eject

添加webpack配置文件