|
19 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | 20 | * |
21 | 21 | */ |
22 | | -const { join, basename } = require('path') |
23 | | -const fs = require('fs/promises') |
24 | | -const gettextParser = require('gettext-parser') |
25 | 22 |
|
26 | 23 | // https://github.com/alexanderwallin/node-gettext#usage |
27 | 24 | // https://github.com/alexanderwallin/node-gettext#load-and-add-translations-from-mo-or-po-files |
28 | 25 | const parseFile = async (fileName) => { |
| 26 | + // We need to import dependencies dynamically to support this module to be imported by vite and to be required by Cypress |
| 27 | + // If we use require, vite will fail with 'Dynamic require of "path" is not supported' |
| 28 | + // If we convert it to an ES module, webpack and vite are fine but Cypress will fail because it can not handle ES imports in Typescript configs in commonjs packages |
| 29 | + const { basename } = await import('path') |
| 30 | + const { readFile } = await import('fs/promises') |
| 31 | + const gettextParser = await import('gettext-parser') |
| 32 | + |
29 | 33 | const locale = basename(fileName).slice(0, -'.pot'.length) |
30 | | - const po = await fs.readFile(fileName) |
| 34 | + const po = await readFile(fileName) |
31 | 35 |
|
32 | 36 | const json = gettextParser.po.parse(po) |
33 | 37 |
|
@@ -56,7 +60,9 @@ const parseFile = async (fileName) => { |
56 | 60 | } |
57 | 61 |
|
58 | 62 | const loadTranslations = async (baseDir) => { |
59 | | - const files = await fs.readdir(baseDir) |
| 63 | + const { join } = await import('path') |
| 64 | + const { readdir } = await import('fs/promises') |
| 65 | + const files = await readdir(baseDir) |
60 | 66 |
|
61 | 67 | const promises = files |
62 | 68 | .filter(name => name !== 'messages.pot' && name.endsWith('.pot')) |
|
0 commit comments