gulp-html-replace

2024-08-27 13:08
文章标签 html frontend replace gulp

本文主要是介绍gulp-html-replace,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文链接:https://www.npmjs.com/package/gulp-html-replace?from=singlemessage&isappinstalled=1%20For%20security,%20do%20not%20share%20your%20WeChat%20account%20information%20online.

  • Usage
  • API
  • Example
  • Upgrade

Install:

  
npm install --save-dev gulp-html-replace

Put some blocks in your HTML file:

<!-- build:<name> -->
Everything here will be replaced
<!-- endbuild -->

name is the name of the block. Could consist of letters, digits, underscore ( _ ) and hyphen ( - ) symbols.

Type: Object {task-name: replacement}

  • task-name - The name of the block in your HTML.
  • replacement - String|Array|stream.Readable|Object The replacement. See examples below.
// Options is a single string 
htmlreplace({js: 'js/main.js'})
 
// Options is an array of strings 
htmlreplace({js: ['js/monster.js', 'js/hero.js']})

If your options strings ends with .js or .css they will be replaced by correct script/style tags, so you don't need to specify a template like in the example below.

// Options is an object 
htmlreplace({
  js: {
    src: 'img/avatar.png',
    tpl: '<img src="%s" align="left" />'
  }
})
 
// Multiple tag replacement 
htmlreplace({
  js: {
    src: [['data-main.js', 'require-src.js']],
    tpl: '<script data-main="%s" src="%s"></script>'
  }
})
  • src - String|Array|stream.Readable Same thing as in simple example.
  • tpl - String Template string. Uses util.format() internally.

In the first example %s will be replaced with img/avatar.png producing <img src="img/avatar.png" align="left"> as the result.

In the second example data-main="%s" and src="%s" will be replaced with data-main.js and require-src.js accordingly, producing <script data-main="data-main.js" src="require-src.js"></script> as the result

// Replacement based on the file being processed 
htmlreplace({
  js: {
    src: null,
    tpl: '<script src="%f".js></script>'
  }
})
// Extended replacement combined with standard replacement 
htmlreplace({
  js: {
    src: 'dir',
    tpl: '<script src="%s/%f".js"></script>'
  }
})
 
  • src - null|String|Array|stream.Readable Same as examples above but null if there are no standard replacements in the template.
  • tpl - String Template string. Extended replacements do not use util.format() and are performed before standard replacements.

In the first example src is null because there are no standard replacements. %f is replaced with the name (without extension) of the file currently being processed. If the file being processed is xyzzy.html the result is<script src="xyzzy.js"></script>.

In the second example src has been set to the string 'dir'. Extended replacements are processed first, replacing %f with xyzzy, then %s will be replaced with dir resulting in <script src="dir/xyzzy.js"></script>.

Valid extended replacements are:

  • %f - this will be replaced with the filename, without an extension.
  • %e - this will be replaced with the extension including the . character.

Everywhere a string replacement can be given, a stream of vinyl is also accepted. The content of each file will be treated as UTF-8 text and used for replacement. If the stream produces more than a file the behavior is the same as when an array is given.

// Replacement is a stream 
htmlreplace({
  cssInline: {
    src: gulp.src('style/main.scss').pipe(sass()),
    tpl: '<style>%s</style>'
  }
})
 

Type: object

All false by default.

  • {Boolean} keepUnassigned - Whether to keep blocks with unused names or remove them.
  • {Boolean} keepBlockTags - Whether to keep <!-- build --> and <!-- endbuild --> comments or remove them.
  • {Boolean} resolvePaths - Try to resolve relative paths. For example if your cwd is /, your html file is/page/index.html and you set replacement as lib/file.js the result path in that html will be../lib/file.js
htmlreplace({
  js: {
    src: null,
    tpl: '<script src="%f".js></script>'
  }
}, {
  keepUnassigned: false,
  keepBlockTags: false,
  resolvePaths: false
})

index.html:

<!DOCTYPE html>
<html>
    <head>
 
    <!-- build:css -->
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">
    <!-- endbuild -->
 
    </head>
    <body>
 
    <!-- build:js -->
    <script src="js/player.js"></script> 
    <script src="js/monster.js"></script> 
    <script src="js/world.js"></script> 
    <!-- endbuild -->

gulpfile.js:

var gulp = require('gulp');
var htmlreplace = require('gulp-html-replace');
 
gulp.task('default', function() {
  gulp.src('index.html')
    .pipe(htmlreplace({
        'css': 'styles.min.css',
        'js': 'js/bundle.min.js'
    }))
    .pipe(gulp.dest('build/'));
});

Result:

<!DOCTYPE html>
<html>
    <head>
 
    <link rel="stylesheet" href="styles.min.css">
 
    </head>
    <body>
 
    <script src="js/bundle.min.js"></script> 

This version introduces streaming support, less confusing API, new option keepUnused and full code overhaul.

  • If you used single task like this: htmlreplace('js', 'script.js') just change it to htmlreplace({js: 'script.js'})
  • If you used single task with template: htmlreplace('js', 'script.js', '<script="%s">') change it tohtmlreplace({js: {src: 'script.js', tpl: '<script="%s">'})
  • files renamed to src, see previous example. Rename if needed.

This version switches to the new way of specifying options which is more future-proof. Before it washtmlreplace(tasks, keepUnassigned = false), now it's htmlreplace(tasks, {keepUnassigned: false}). No action required, old syntax will still work, but it is advisable to switch to the new syntax.


这篇关于gulp-html-replace的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111753

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

详解如何在React中执行条件渲染

《详解如何在React中执行条件渲染》在现代Web开发中,React作为一种流行的JavaScript库,为开发者提供了一种高效构建用户界面的方式,条件渲染是React中的一个关键概念,本文将深入探讨... 目录引言什么是条件渲染?基础示例使用逻辑与运算符(&&)使用条件语句列表中的条件渲染总结引言在现代

详解Vue如何使用xlsx库导出Excel文件

《详解Vue如何使用xlsx库导出Excel文件》第三方库xlsx提供了强大的功能来处理Excel文件,它可以简化导出Excel文件这个过程,本文将为大家详细介绍一下它的具体使用,需要的小伙伴可以了解... 目录1. 安装依赖2. 创建vue组件3. 解释代码在Vue.js项目中导出Excel文件,使用第三

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Vue.js报错:ReferenceError: “Vue is not defined“ 的原因与解决方案

《使用Vue.js报错:ReferenceError:“Vueisnotdefined“的原因与解决方案》在前端开发中,ReferenceError:Vueisnotdefined是一个常见... 目录一、错误描述二、错误成因分析三、解决方案1. 检查 vue.js 的引入方式2. 验证 npm 安装3.

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE