Skip to content

Commit 017f1e7

Browse files
committed
feat: hybrid esm/cjs
1 parent 5d67af3 commit 017f1e7

8 files changed

Lines changed: 6704 additions & 1456 deletions

File tree

build.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
rollup: {
5+
emitCJS: true,
6+
},
7+
rootDir: './lib',
8+
outDir: '../dist',
9+
entries: ['index.js'],
10+
declaration: true,
11+
})

dist/index.cjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const omit = require('lodash.omit');
4+
const matchHelper = require('posthtml-match-helper');
5+
6+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7+
8+
const omit__default = /*#__PURE__*/_interopDefaultCompat(omit);
9+
const matchHelper__default = /*#__PURE__*/_interopDefaultCompat(matchHelper);
10+
11+
const plugin = (options = {}) => (tree) => {
12+
options.attributes = options.attributes || {};
13+
options.overwrite = options.overwrite || false;
14+
const process = (node) => {
15+
const attributes = Object.keys(options.attributes);
16+
const matchers = attributes.map((attribute) => matchHelper__default(attribute));
17+
attributes.forEach((key, i) => {
18+
tree.match(matchers[i], (node2) => {
19+
for (const [k, v] of Object.entries(options.attributes[key])) {
20+
if (k === "class" && node2.attrs && node2.attrs.class) {
21+
node2.attrs.class = [.../* @__PURE__ */ new Set([...node2.attrs.class.split(" "), ...v.split(" ")])].join(" ");
22+
} else {
23+
const attributes2 = options.overwrite ? options.attributes[key] : omit__default(options.attributes[key], Object.keys(node2.attrs || {}));
24+
node2.attrs = { ...node2.attrs, ...attributes2 };
25+
}
26+
}
27+
return node2;
28+
});
29+
});
30+
return node;
31+
};
32+
return tree.walk(process);
33+
};
34+
35+
module.exports = plugin;

dist/index.d.cts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type PluginOptions = {
2+
/**
3+
An object defining which elements should get what attributes.
4+
5+
@default {}
6+
7+
@example
8+
{
9+
table: {
10+
cellpadding: 0,
11+
cellspacing: 0,
12+
role: 'none'
13+
},
14+
img: {
15+
alt: ''
16+
},
17+
}
18+
*/
19+
attributes?: {
20+
[tagName: string]: {
21+
[attribute: string]: string | number;
22+
};
23+
};
24+
25+
/**
26+
* Overwrite existing attribute values.
27+
*
28+
* @default false
29+
*
30+
* @example
31+
* {
32+
* overwrite: true
33+
* }
34+
*/
35+
overwrite?: boolean;
36+
};
37+
38+
export type { PluginOptions };

dist/index.d.mts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type PluginOptions = {
2+
/**
3+
An object defining which elements should get what attributes.
4+
5+
@default {}
6+
7+
@example
8+
{
9+
table: {
10+
cellpadding: 0,
11+
cellspacing: 0,
12+
role: 'none'
13+
},
14+
img: {
15+
alt: ''
16+
},
17+
}
18+
*/
19+
attributes?: {
20+
[tagName: string]: {
21+
[attribute: string]: string | number;
22+
};
23+
};
24+
25+
/**
26+
* Overwrite existing attribute values.
27+
*
28+
* @default false
29+
*
30+
* @example
31+
* {
32+
* overwrite: true
33+
* }
34+
*/
35+
overwrite?: boolean;
36+
};
37+
38+
export type { PluginOptions };

dist/index.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type PluginOptions = {
2+
/**
3+
An object defining which elements should get what attributes.
4+
5+
@default {}
6+
7+
@example
8+
{
9+
table: {
10+
cellpadding: 0,
11+
cellspacing: 0,
12+
role: 'none'
13+
},
14+
img: {
15+
alt: ''
16+
},
17+
}
18+
*/
19+
attributes?: {
20+
[tagName: string]: {
21+
[attribute: string]: string | number;
22+
};
23+
};
24+
25+
/**
26+
* Overwrite existing attribute values.
27+
*
28+
* @default false
29+
*
30+
* @example
31+
* {
32+
* overwrite: true
33+
* }
34+
*/
35+
overwrite?: boolean;
36+
};
37+
38+
export type { PluginOptions };

dist/index.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import omit from 'lodash.omit';
2+
import matchHelper from 'posthtml-match-helper';
3+
4+
const plugin = (options = {}) => (tree) => {
5+
options.attributes = options.attributes || {};
6+
options.overwrite = options.overwrite || false;
7+
const process = (node) => {
8+
const attributes = Object.keys(options.attributes);
9+
const matchers = attributes.map((attribute) => matchHelper(attribute));
10+
attributes.forEach((key, i) => {
11+
tree.match(matchers[i], (node2) => {
12+
for (const [k, v] of Object.entries(options.attributes[key])) {
13+
if (k === "class" && node2.attrs && node2.attrs.class) {
14+
node2.attrs.class = [.../* @__PURE__ */ new Set([...node2.attrs.class.split(" "), ...v.split(" ")])].join(" ");
15+
} else {
16+
const attributes2 = options.overwrite ? options.attributes[key] : omit(options.attributes[key], Object.keys(node2.attrs || {}));
17+
node2.attrs = { ...node2.attrs, ...attributes2 };
18+
}
19+
}
20+
return node2;
21+
});
22+
});
23+
return node;
24+
};
25+
return tree.walk(process);
26+
};
27+
28+
export { plugin as default };

0 commit comments

Comments
 (0)