From 8503067fcf7cb701595418e2d821807452c39875 Mon Sep 17 00:00:00 2001 From: Owen Yamauchi Date: Wed, 15 Nov 2023 17:44:57 -0500 Subject: [PATCH] I think I figured it out I think the generated files being modified by `lit-localize build` was causing Parcel to rebuild in an infinite loop. There seems to be no real way to exclude files from Parcel's watcher in `serve` mode, so I'm doing so in a very hacky way: putting the generated strings files in Parcel's cache directory. --- .gitignore | 3 --- lit-localize.json | 4 ++-- scripts/parcel-resolver-locales.mjs | 21 +++++++++++++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index c407b0e..8db0cde 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,3 @@ yarn-error.log* # cypress cypress/videos/*.mp4 cypress/screenshots/* - -# lit-localize generated -generated diff --git a/lit-localize.json b/lit-localize.json index 8ddfc62..9d089ee 100644 --- a/lit-localize.json +++ b/lit-localize.json @@ -5,8 +5,8 @@ "tsConfig": "./tsconfig.json", "output": { "mode": "runtime", - "localeCodesModule": "generated/locales.ts", - "outputDir": "generated/strings" + "localeCodesModule": ".parcel-cache/locales.ts", + "outputDir": ".parcel-cache/strings" }, "interchange": { "format": "xliff", diff --git a/scripts/parcel-resolver-locales.mjs b/scripts/parcel-resolver-locales.mjs index 155127d..a436600 100644 --- a/scripts/parcel-resolver-locales.mjs +++ b/scripts/parcel-resolver-locales.mjs @@ -15,19 +15,32 @@ async function allXlfFiles(projectRoot) { * Resolves import specifiers starting with `locales:` by running lit-localize * to generate strings files, and pointing Parcel at the generated files. * + * The generated files are in Parcel's cache directory (configured in + * lit-localize.json) so that they don't trigger Parcel's watcher, which could + * result in an infinite loop of rebuilding. Yes this is silly, but Parcel does + * not have an official way to make the watcher ignore some files. + * * NB: this is not a Typescript file! (Parcel doesn't support plugins written * in TS.) No type checking! */ export default new Resolver({ async resolve({ specifier, options: { projectRoot } }) { if (specifier.startsWith('locales:')) { - await promisify(exec)('npx lit-localize build'); - const locale = specifier.substring('locales:'.length); + + const litConfig = JSON.parse( + await promisify(fs.readFile)( + path.join(projectRoot, 'lit-localize.json'), + 'utf-8', + ), + ); + const filePath = locale === 'config' - ? path.join(projectRoot, 'generated/locales.ts') - : path.join(projectRoot, `generated/strings/${locale}.ts`); + ? path.join(projectRoot, litConfig.output.localeCodesModule) + : path.join(projectRoot, litConfig.output.outputDir, `${locale}.ts`); + + await promisify(exec)('npx lit-localize build\n'); return { // Rebuild if an XLIFF file changes, or the lit-localize config.