Skip to content

Commit

Permalink
fix "Error [ERR_REQUIRE_ESM]: require() of ES Module"
Browse files Browse the repository at this point in the history
```
/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/index.js:4
/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/node_modules/escape-string-regexp/index.js
from
/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/index.js
not supported.
/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/node_modules/escape-string-regexp/index.js
in
/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/index.js
to a dynamic import() which is available in all CommonJS modules.
(/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/index.js:4:28)
(/home/runner/work/postcss-selector-prefix/postcss-selector-prefix/index.test.js:5:16)
{
```
  • Loading branch information
robkorv committed Apr 8, 2024
1 parent 9991075 commit 5cd9280
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/**
* @type {import('postcss').PluginCreator}
*/
const escapeStringRegexp = require("escape-string-regexp");
// escape function taken from
// https://github.com/tc39/proposal-regex-escaping/blob/3d45b23af8e8824dd689deb35856a80fa52d78ca/polyfill.js
const regExpEscape = (s) => String(s).replace(/[\\^$*+?.()|[\]{}]/g, "\\$&");

module.exports = (prefix) => {
return {
postcssPlugin: "postcss-selector-prefix",
Once(root) {
// create prefix regex.
const escapedPrefix = escapeStringRegexp(prefix);
const escapedPrefix = regExpEscape(prefix);
const re = new RegExp("^" + escapedPrefix);

root.walkRules((rule) => {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"node": ">=18.0.0"
},
"peerDependencies": {
"postcss": "^8.4.27",
"escape-string-regexp": "^5.0.0"
"postcss": "^8.4.27"
},
"devDependencies": {
"eslint": "^8.47.0",
Expand Down

0 comments on commit 5cd9280

Please sign in to comment.