Plugin Starter Kit
A starter project for PostHTML plugins
[![Version][npm-version-shield]][npm] [![Build][github-ci-shield]][github-ci] [![License][license-shield]][license]
About
在打包为离线包的情况下,使用@vitejs/plugin-legacy
后,script
标签包含了module
属性和nomodule
属性,但是type = module
的情况下,不能支持本地文件路径,所以直接打开会报错。
删除掉带有type=module
的script
标签,同时我们要去掉script
的nomodule
的属性,这样兼容传统浏览器的脚本在现代浏览器下也可以执行。
git clone https://github.com/posthtml/posthtml-plugin-starter.git
Features
Tests
The testing boilerplate includes a process()
method which accepts 4 parameters:
-
t
the test object -
name
the file name of the fixture/expected files, excluding extension -
options
any options to pass to the plugin when testing -
log
a boolean that turns on logging to console
For example, imagine we're writing a test that uses /test/fixtures/skip-nodes.html
:
test('It skips nodes defined in `skipNodes` option', t => {
return process(t, 'skip-nodes', {skipNodes: ['a']}, true)
})
As you can see, the second parameter passed to the process()
method is the fixture file name, without the .html
extension.
Testing for Errors
To test errors thrown by your plugin, use the error()
method:
test('Syntax error', t => {
return error('syntax-error', err => {
t.is(err.message, 'Invalid or unexpected token')
})
})
Just like before, the first parameter passed to error()
is the fixture file name, without the extension.
Introduction
删除vite
打包后的部分script
和移除nomodule
属性
Input:
<script type="module">xxxxx</script>
<script nomodule id="vite-legacy-polyfill" src="./assets/polyfills-legacy.1b8c3f41.js"></script>
Output:
<script id="vite-legacy-polyfill" src="./assets/polyfills-legacy.1b8c3f41.js"></script>
Install
$ npm i posthtml module-tag-remove
Usage
Provide clear code samples showing how to use the plugin:
import posthtml from'posthtml'
import removeScript from 'module-tag-remove'
let result = posthtml().use(plugin()).process(html, { sync: true }).html