diff --git a/gatsby-theme-oi-wiki/.eslintrc.js b/gatsby-theme-oi-wiki/.eslintrc.js index 3f1d19f7..1ed5e31c 100644 --- a/gatsby-theme-oi-wiki/.eslintrc.js +++ b/gatsby-theme-oi-wiki/.eslintrc.js @@ -74,6 +74,7 @@ module.exports = { files: [ '.eslintrc.js', 'gatsby-*.js', + 'esmRequire.js', 'plugins/**/*.js', ], env: { diff --git a/gatsby-theme-oi-wiki/esmRequire.js b/gatsby-theme-oi-wiki/esmRequire.js new file mode 100644 index 00000000..7dd9f2e6 --- /dev/null +++ b/gatsby-theme-oi-wiki/esmRequire.js @@ -0,0 +1,34 @@ +/** + * @file export compiled ES modules as a workaround before Gatsby properly handles it + */ + +const esm = require('esm') +const fs = require('fs') +const Module = require('module') + + +// Node: bypass [ERR_REQUIRE_ESM] +const orig = Module._extensions['.js'] +Module._extensions['.js'] = function (module, filename) { + try { + return orig(module, filename) + } catch (e) { + if (e.code === 'ERR_REQUIRE_ESM') { + const content = fs.readFileSync(filename, 'utf8') + module._compile(content, filename) + } + } +} + +const _esmRequire = esm(module, { + cjs: true, + mode: 'all', +}) + +// don't pollute Module +Module._extensions['.js'] = orig + + +module.exports = function esmRequire(id) { + return _esmRequire(id) +} diff --git a/gatsby-theme-oi-wiki/gatsby-config.esm.js b/gatsby-theme-oi-wiki/gatsby-config.esm.js deleted file mode 100644 index e3a7b624..00000000 --- a/gatsby-theme-oi-wiki/gatsby-config.esm.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @file export compiled ES modules as a workaround before Gatsby properly handles it - */ - -const esm = require('esm') -const fs = require('fs') - -// Node: bypass [ERR_REQUIRE_ESM] limitations -const Module = require('module') -const orig = Module._extensions['.js'] -Module._extensions['.js'] = function (module, filename) { - const contents = fs.readFileSync(filename, 'utf8') - module._compile(contents, filename) -} - -// ESM: compile -const esmRequire = esm(module, { - cjs: true, - mode: 'all', -}) - -const imports = { - remarkDetails: esmRequire('remark-details').default, -} - -Module._extensions['.js'] = orig -module.exports = imports diff --git a/gatsby-theme-oi-wiki/gatsby-config.js b/gatsby-theme-oi-wiki/gatsby-config.js index a34fc25b..c2527eb6 100644 --- a/gatsby-theme-oi-wiki/gatsby-config.js +++ b/gatsby-theme-oi-wiki/gatsby-config.js @@ -1,4 +1,5 @@ const path = require('path') +const esmRequire = require('./esmRequire') const IS_EXEC_BUILD = process.env.gatsby_executing_command === 'build' const IS_PROD = process.env.PRODUCTION === 'true' || @@ -26,8 +27,6 @@ const mathRehype = IS_EXEC_BUILD ? [require('rehype-mathjax/chtml'), { fontURL }] : [require('rehype-mathjax/browser')] -const { remarkDetails } = require('./gatsby-config.esm') - module.exports = { plugins: [ { @@ -87,7 +86,7 @@ module.exports = { ], remarkPlugins: [ require('remark-math'), - remarkDetails, + esmRequire('remark-details').default, [require('@mgtd/remark-shiki').remarkShiki, { semantic: false, theme: 'css-variables',