Files
ionicons/scripts/collection-copy.ts
T
Adam Bradley 66556ea45c feat(custom-elements): export ionicons/components
Also provide ionicons as a custom element which can be defined using `customElements.define()`

```
import { IonIcon } from "ionicons/components/ion-icon";
customElements.define("ion-icon", IonIcon);
```

The custom element build works the same as the lazy load build, and is also able to lazy load svgs on demand, either by giving it an exact url in the `src` attribute `<ion-icon src="./path/to/airplane.svg"></ion-icon>`, or by providing the name of the ionicon to load `<ion-icon name="airplane"></ion-icon>`.

If using the `name` attribute, you'll also need to set where to find the svg directory using `setAssetPath()`

```
import { setAssetPath } from "ionicons/components";
setAssetPath(location.href);
```
2021-02-01 15:28:33 -06:00

43 lines
1.5 KiB
TypeScript

import fs from 'fs-extra';
import { join } from 'path';
async function collectionCopy(rootDir: string) {
// move optimized svgs to correct collection location
const optimizedSrc = join(rootDir, 'dist', 'ionicons', 'svg');
const collectionDest = join(rootDir, 'dist', 'collection', 'components', 'icon', 'svg');
await fs.copy(optimizedSrc, collectionDest);
// we don't to copy the src svgs to collection
await fs.remove(join(rootDir, 'dist', 'collection', 'svg'));
const cePackageDir = join(rootDir, 'components');
const cePackageJsonPath = join(cePackageDir, 'package.json');
const ceCjsPath = join(cePackageDir, 'index.cjs.js');
const emptyCjs = `/*empty cjs*/`;
await fs.writeFile(ceCjsPath, emptyCjs);
const cePackageJson = {
name: 'ionicons/components',
description: 'Ionicons custom element.',
main: './index.cjs.js',
module: './index.js',
types: './index.d.ts',
private: true,
};
await fs.writeFile(cePackageJsonPath, JSON.stringify(cePackageJson, null, 2));
// this is temporary!!!!
// removing the `type` from the d.ts export
// to make it easier for users migrating between
// of older versions of angular and typescript
// to the newer verisons, where the `type` keyword
// is used. This is a megahack, no doubt.
const typesDist = join(rootDir, 'dist', 'types', 'index.d.ts');
let types = await fs.readFile(typesDist, 'utf8');
types = types.replace('export type', 'export');
await fs.writeFile(typesDist, types);
}
collectionCopy(join(__dirname, '..'));