npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-webpack-custom-position

v1.0.0

Published

自定义资源在HTML上的位置

Downloads

2

Readme

在HTML-webpack-plugin 中自定义静态资源的位置


Build Status

npm安装

$ npm install --save-dev html-webpack-custom-position

基本用法

require插件:

const HtmlWebpackCustomPosition = require('html-webpack-custom-position');

在webpack插件配置中中添加:

plugins: [
  new HtmlWebpackPlugin(),
  new HtmlWebpackCustomPosition()
]

因为没有配置参数,上面的写法没有任何影响。

一般使用场景是将控制css相关的js代码提前到head里面执行,从而可以避免页面闪屏。

const webpackConfig = {
  entry: {
    fontSize: 'flexible.js'
  },
  ...
  plugins: [
    new HtmlWebpackPlugin(),
    new HtmlWebpackCustomPosition({
      head: ['fontSize']
    })
  ]
}

可配置选项

  • head: array 数组里面的选项应该对应于对应于entry的key,或者来自于ExtractTextPlugin的filename字段值
  • body: array 同上

更多例子

将 css 放到body最后,同时将 js 放到head中间

  const webpackConfig = {
  entry: {
    fontSize: 'flexible.js'
  },
  ...
  plugins: [
    new ExtractTextPlugin({
      filename: 'style.css'
    }),
    new HtmlWebpackPlugin(),
    new Custom({
      body: ['\.css$'],
      head: ['fontSize']
    })
  ]
}

执行结果如下

<head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
  <script type="text/javascript" src="fontSize.js"></script></head>
  <body>
  <link href="style.css" rel="stylesheet">
</body>

注意

由于自定义之后的资源已经失去了正确的引用关系,可能会报错找不到webpackjson方法。 如果使用webpack.optimize.CommonsChunkPlugin 的时候将自定义位置的资源也包含进去,则运行时会报错。解决办法是不要对该资源进行公共代码抽离。

  new webpack.optimize.CommonsChunkPlugin({
    chunks: ['js/app', 'js/view', /** 'js/fontSize' **/], // 不要对js/fontSize进行代码抽离
    names: ['js/vendor']
  }),

将JS代码前置后,一般配合 html-webpack-inline-source-plugin 将资源写入行内,从而加快页面渲染速度