Skip to content

Commit

Permalink
fix: expand script that corrects types after build
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse committed Nov 5, 2024
1 parent f393e6d commit b640fde
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-toes-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

expand script that corrects types after build
4 changes: 2 additions & 2 deletions packages/ui/components/core/src/ScopedElementsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function supportsScopedRegistry() {
}

/**
* This file is combination of '@open-wc/scoped-elements@v3/lit-element.js' and '@open-wc/scoped-elements@v3/html-element.js'.
* Then on top of those, some code from '@open-wc/scoped-elements@v2' is brought to to make polyfill not mandatory.
* This file is a combination of '@open-wc/scoped-elements@v3/lit-element.js' and '@open-wc/scoped-elements@v3/html-element.js'.
* On top of those, some code from '@open-wc/scoped-elements@v2' is brought to to make polyfill not mandatory.
* This can be a great help for ssr scenarios, allowing elements to be consumed without needing knowledge about internall
* consumption.
* (N.B. at this point in time, this is limited to the scenario where there's one version of lion on the page).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ssrNonHydratedFixture,
ssrHydratedFixture,
csrFixture,
// @ts-expect-error
} from '@lit-labs/testing/fixtures.js';
import { LitElement, html } from 'lit';
import sinon from 'sinon';
Expand Down Expand Up @@ -71,7 +72,6 @@ describe('ScopedElementsMixin', () => {
).to.contain("<span>I'm a child</span>");
}

// @ts-expect-error
expect(el.registry.get('scoped-elements-child')).to.not.be.undefined;
}
});
Expand Down
56 changes: 51 additions & 5 deletions packages/ui/scripts/types-correct-after-build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
// /**
// * TS build uses jsdoc and TS files with "import { LitElement } from 'lit'"
// * - jsdoc compiles into "import { LitElement } from 'lit-element/lit-element.js'"
// * - TS output remains unchanged
// *
// * These different imports (although they should both ultimately resolve to "{ LitElement } from 'lit-element/lit-element.js'")
// * are incompatible (not considered the same class), which leads to errors for Subclassers.
// *
// * This script will make sure everything stays "import { LitElement } from 'lit'"
// * See: https://github.com/microsoft/TypeScript/issues/51622
// */

// import fs from 'fs';
// // eslint-disable-next-line import/no-extraneous-dependencies
// import { globby } from 'globby';
// import { fileURLToPath } from 'url';

// const packageRoot = fileURLToPath(new URL('../', import.meta.url));

// async function alignLitImports() {
// const fileNames = await globby([`${packageRoot}/dist-types`]);
// for (const fileName of fileNames) {
// // eslint-disable-next-line no-await-in-loop
// const contents = await fs.promises.readFile(fileName, 'utf-8');
// const replaced = contents.replace(
// /(LitElement.*\}) from "lit-element\/lit-element\.js/g,
// '$1 from "lit',
// );
// fs.promises.writeFile(fileName, replaced);
// }
// }

// alignLitImports();

/**
* TS build uses jsdoc and TS files with "import { LitElement } from 'lit'"
* - jsdoc compiles into "import { LitElement } from 'lit-element/lit-element.js'"
Expand All @@ -11,23 +45,35 @@
*/

import fs from 'fs';
// @ts-expect-error
// eslint-disable-next-line import/no-extraneous-dependencies
import { globby } from 'globby';
import { optimisedGlob as globby } from 'providence-analytics/utils.js';
import { fileURLToPath } from 'url';

const packageRoot = fileURLToPath(new URL('../', import.meta.url));

async function alignLitImports() {
async function alignLitImportsAndFixLocalpaths() {
const fileNames = await globby([`${packageRoot}/dist-types`]);
for (const fileName of fileNames) {
// eslint-disable-next-line no-await-in-loop
const contents = await fs.promises.readFile(fileName, 'utf-8');
const replaced = contents.replace(
const replaced1 = contents.replace(
/(LitElement.*\}) from "lit-element\/lit-element\.js/g,
'$1 from "lit',
);
fs.promises.writeFile(fileName, replaced);

// Now "unresolve" all paths that reference '../**/node_modules/**'
// These are outside of the bundled repo and therefore break in consuming context
// Also, they are resolved to their local context via the export map, this should be 'unwinded'

const re = /\("(..\/)*?node_modules\/@open-wc\/scoped-elements\/types\.js"\)/g;
const replaced2 = replaced1.replace(re, '("@open-wc/scoped-elements/lit-element.js")');

// For now, we did a quick and dirty fix with specific knowledge of this repo,
// because we expect https://github.com/microsoft/TypeScript/issues/51622 to be solved in the future.

fs.promises.writeFile(fileName, replaced2);
}
}

alignLitImports();
alignLitImportsAndFixLocalpaths();

0 comments on commit b640fde

Please sign in to comment.