html post文件,GitHub - posthtml/posthtml-loader: PostHTML for Webpack

本文档详细介绍了如何在Webpack中配置PostHTML-Loader,包括安装、使用方法、各种选项设置,如自定义解析器、渲染器、插件等。通过PostHTML-Loader,你可以对HTML文件进行各种转换和优化操作,如使用PostHTML插件进行模板包含、内容注入等。此外,还提到了配置文件的路径设置和环境变量的使用。
摘要由CSDN通过智能技术生成

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f706f737468746d6c2d6c6f616465722e737667

68747470733a2f2f696d672e736869656c64732e696f2f6e6f64652f762f706f737468746d6c2d6c6f616465722e737667

68747470733a2f2f64617669642d646d2e6f72672f706f737468746d6c2f706f737468746d6c2d6c6f616465722e737667

687474703a2f2f696d672e736869656c64732e696f2f7472617669732f706f737468746d6c2f706f737468746d6c2d6c6f616465722e737667

68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f706f737468746d6c2f706f737468746d6c2d6c6f616465722f62616467652e737667

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532307374796c652d7374616e646172642d79656c6c6f772e737667

68747470733a2f2f6261646765732e6769747465722e696d2f706f737468746d6c2f706f737468746d6c2e737667

687474703a2f2f706f737468746d6c2e6769746875622e696f2f706f737468746d6c2f6c6f676f2e737667

68747470733a2f2f7765627061636b2e6a732e6f72672f6173736574732f69636f6e2d7371756172652d6269672e737667

PostHTML Loader

Install

npm i -D posthtml-loader

Usage

import html from './file.html'

webpack.config.js

module: {

rules: [

{

test: /\.html$/,

use: [

'html-loader',

{

loader: 'posthtml-loader',

options: {

ident: 'posthtml',

parser: 'PostHTML Parser',

plugins: [

/* PostHTML Plugins */

require('posthtml-plugin')(options)

]

}

}

]

}

]

},

Options

Name

Type

Default

Description

{Object}

undefined

PostHTML Config

{String/Function}

undefined

PostHTML Parser

{Boolean}

false

PostHTML Options SkipParse

{String/Function}

undefined

PostHTML Render

{Array/Function}

[]

PostHTML Plugins

{boolean}

false

PostHTML Options Sync

{Array}

[]

PostHTML Options custom Directives

Config

Name

Type

Default

Description

{String}

loader.resourcePath

PostHTML Config Path

{Object}

{}

PostHTML Config Context

If you want to use are shareable config file instead of inline options in your webpack.config.js create a posthtml.config.js file and place it somewhere down the file tree in your project. The nearest config relative to dirname(file) currently processed by the loader applies. This enables Config Cascading. Despite some edge cases the config file will be loaded automatically and no additional setup is required. If you don't intend to use Config Cascading, it's recommended to place posthtml.config.js in the root ./ of your project

src

├── components

│   ├── component.html

│   ├── posthtml.config.js (components)

├── index.html

├── posthtml.config.js (index)

└── webpack.config.js

Path

If you normally place all your config files in a separate folder e.g ./config it is necessary to explicitly set the config path in webpack.config.js

webpack.config.js

{

loader: 'posthtml-loader',

options: {

config: {

path: 'path/to/.config/'

}

}

}

Context

Name

Type

Default

Description

env

{String}

'development'

process.env.NODE_ENV

file

{Object}

{ dirname, basename, extname }

File

options

{Object}

{}

Plugin Options (Context)

module.exports = ({ file, options, env }) => ({

parser: 'posthtml-sugarml',

plugins: {

'posthtml-include': options.include,

'posthtml-content': options.content,

'htmlnano': env === 'production' ? {} : false

}

})

webpack.config.js

{

loader: 'posthtml-loader',

options: {

config: {

ctx: {

include: {...options},

content: {...options}

}

}

}

}

Parser

If you want to use a custom parser e.g SugarML, you can pass it in under the parser key in the loader options

{String}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

parser: 'posthtml-sugarml'

}

}

{Function}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

parser: require('posthtml-sugarml')()

}

}

skipParse

If you want to use disable parsing, you can pass it in under the skipParse key in the loader options

{Boolean}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

skipParse: false

}

}

Render

If you want to use a custom render, you can pass it in under the render key in the loader options

{String}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

render: 'posthtml-you-render'

}

}

{Function}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

parser: require('posthtml-you-render')()

}

}

Plugins

Plugins are specified under the plugins key in the loader options

{Array}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

plugins: [

require('posthtml-plugin')()

]

}

}

{Function}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

plugins (loader) {

return [

require('posthtml-plugin')()

]

}

}

}

Sync

Enables sync mode, plugins will run synchronously, throws an error when used with async plugins

{Boolean}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

sync: true

}

}

Directives

If you want to use a custom directives, you can pass it in under the directives key in the loader options

{Array}

webpack.config.js

{

loader: 'posthtml-loader',

options: {

directives: [{name: '?php', start: ''}]

}

}

Maintainer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值