javascript 用于创建HTML页面的插件,该页面根据浏览器功能加载ES5或ES6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 用于创建HTML页面的插件,该页面根据浏览器功能加载ES5或ES6相关的知识,希望对你有一定的参考价值。

const { concat, mapKeys, merge, uniq } = require('lodash');

/**
 * Tweaked from original by Mike Engel
 * https://github.com/jantimon/html-webpack-plugin/issues/782#issuecomment-331229728
 *
 * Use this with multiple Webpack configurations that generate different builds
 * for modern and legacy browsers. But use the same instance of the plugin in both configurations.
 *
 * It keeps track of assets seen in each build configuration, and appends script tags for
 * all the assets to subsequent builds - using type=module or nomodule to cause the appropriate
 * version of each one to be loaded.
 *
 * The HTML file will be written for each configuration, but only the last-emitted one (which will
 * overwrite any previous ones) will have all the asset tags.
 *
 * Many browsers (IE10, IE11, Firefox 57 at least) will download (but not run) both versions of
 * every asset - see https://github.com/philipwalton/webpack-esnext-boilerplate/issues/1
 */
function HtmlModuleScriptWebpackPlugin(options) {
  this.options = options;
  this.sharedAssets = { js: [], chunks: [] };
}

HtmlModuleScriptWebpackPlugin.prototype = {

  apply: function (compiler) {
    compiler.plugin('compilation', compilation => {
      compilation.plugin('html-webpack-plugin-before-html-processing', this.beforeHtmlProcessing.bind(this));
      compilation.plugin('html-webpack-plugin-alter-asset-tags', this.alterAssetTags.bind(this));
    });
  },

  beforeHtmlProcessing: function (htmlPluginData, callback) {
    // Avoid chunk name collisions, since they can be named the same between builds
    // { 'main': {} } to { 'main_modern': {} } if the filename matches
    const renamedChunks = mapKeys(htmlPluginData.assets.chunks, (chunk, name) => {
      if (chunk.entry.includes(this.options.entrySuffix)) {
        return `${name}${this.options.entrySuffix}`;
      }

      return name;
    });

    this.sharedAssets = {
      js: uniq(concat(this.sharedAssets.js, htmlPluginData.assets.js)),
      chunks: merge(this.sharedAssets.chunks, renamedChunks)
    };

    callback(null, merge(htmlPluginData.assets, this.sharedAssets));
  },

  alterAssetTags: function (htmlPluginData, callback) {
    const body = htmlPluginData.body.map(assetTag => {
      const isModern = assetTag.attributes.src.includes(this.options.entrySuffix);
      const attributes = isModern ? { type: 'module' }: { nomodule: true };

      return merge(assetTag, { attributes });
    });

    callback(null, merge(htmlPluginData, { body }));
  }
};

module.exports = HtmlModuleScriptWebpackPlugin;
const HtmlModuleScriptWebpackPlugin = require('./HtmlModuleScriptWebpackPlugin');

// Must be shared across configurations
const htmlModuleScriptPlugin = new HtmlModuleScriptWebpackPlugin({
  entrySuffix: '_modern'
});

// in your plugins section:
const plugins = [
  // Make the HTML page load a modern build if the browser supports it, otherwise a legacy one
  htmlModuleScriptPlugin,

  // Creates index.html
  new HtmlWebpackPlugin({
    favicon: './src/favicon.ico',
    title: 'React Starter'
  })
];

module.exports = [
  modernConfig, // with entry named "main_modern"
  legacyConfig
];

/*
 * You'll also need the "_modern" suffix on any other entry points, such as CommonsChunkPlugin ones
 * used for separating a vendor chunk and/or runtime(manifest) chunk.
 */

以上是关于javascript 用于创建HTML页面的插件,该页面根据浏览器功能加载ES5或ES6的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript库

php 用于创建图库postype和分类的插件。以及创建一些模板文件以在主题中显示图库帖子类型页面

boostrap插件

html 用于页面JavaScript日志的模板

web前端的javascript主要用于交互吗

是否可以在一个共同的地方设置样式和 JavaScript 引用并将其用于所有 HTML 页面?