Giter VIP home page Giter VIP logo

html-webpack-plugin-replaceurl's Introduction

Html Webpack Replaceurl Plugin

license npm

此插件需配合html-webpack-plugin使用,作用是将html文档中对本地静态资源(js/style)的引用替换为经webpack编译后url。

安装

npm install html-webpack-replaceurl-plugin --save-dev

使用方法

配置webpack:

let HtmlWebpackPlugin = require('html-webpack-plugin');
let HtmlWebpackReplaceurlPlugin = require('html-webpack-replaceurl-plugin');
let webpackConfig = {
  entry: {
      'main.a':'main.a.js',
      'vendor': 'vue'
  },
  output: {
    path: 'dist',
    publicPath: '/',
    filename: '[name].[hash].js'
  },
  plugins: [
      new webpack.optimize.CommonsChunkPlugin({
          name: 'vendor',
          filename: 'vendor.js'
      }),
      new HtmlWebpackPlugin({
          inject: false, //此项必须配置为false
      }),
      new HtmlWebpackReplaceurlPlugin({
        // 解析模式:strict-严格模式;loose-宽松模式
        mode: 'loose',
        js: {
          // 文件名前缀
          mainFilePrefix: 'main',
          // 代替换文件名是否包含hash指纹
          useHash: false,
          // 文件名分隔符,默认文件名规范为main.[name].js
          separator: '.',
          // 是否存在common模块
          common: true,
          // dll文件信息
          dll: {
            name: 'webapp.dll.js',
            url: '/js/webapp.dll.js'
          }
        },
        css: {
          // 文件名前缀
          mainFilePrefix: 'main',
          // 是否使用hash
          useHash: false,
          // 文件名分隔符,默认文件名规范为main.[name].css
          separator: '.'
        },
        // 是否在url末尾加入时间戳,默认false
        urlTimestamp: false
      })]
};

上述配置项应用于index.html文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <script src="vendor.js"></script>
  </head>
  <body>
    <script src="main.a.js"></script>
  </body>
</html>

编译后的index.html内容为:

<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <script src="/dist/vendor.js"></script>
  </head>
  <body>
    <script src="/dist/main.a.bc2e63dd.js"></script>
  </body>
</html>

模式选择

  • loose-宽松模式,使用正则匹配,准确度略低;
  • strict-严格模式,解析html文档100%匹配准确度。

详细原理请参考boi-资源定位

html-webpack-plugin-replaceurl's People

Contributors

zhoujunpeng avatar

Stargazers

 avatar Jie avatar Dai Siyao avatar greenmashimaro avatar liuyi avatar  avatar 葫芦娃-十天 avatar Jekin Zhang avatar  avatar crazyliu- avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.