2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 15:44:05 +00:00

Change build process for Svelte components

This commit is contained in:
Vjacheslav Trushkin 2021-04-30 10:01:30 +03:00
parent e5e3e9a52e
commit ef35c558a7
10 changed files with 114 additions and 99 deletions

View File

@ -1,3 +1,9 @@
.DS_Store .DS_Store
rollup.config.js rollup.config.js
tsconfig.json
build.js
node_modules node_modules
build
src
lib
tests

View File

@ -10,7 +10,8 @@ const commands = [];
// Parse command line // Parse command line
const compile = { const compile = {
core: false, core: false,
dist: true, tsc: true,
bundles: true,
}; };
process.argv.slice(2).forEach((cmd) => { process.argv.slice(2).forEach((cmd) => {
if (cmd.slice(0, 2) !== '--') { if (cmd.slice(0, 2) !== '--') {

View File

@ -11,12 +11,13 @@
"url": "https://github.com/iconify/iconify.git", "url": "https://github.com/iconify/iconify.git",
"directory": "packages/svelte" "directory": "packages/svelte"
}, },
"svelte": "dist/component.mjs", "svelte": "dist/offline.js",
"module": "dist/index.mjs", "module": "dist/offline-bundle.mjs",
"main": "dist/index.js", "main": "dist/offline-bundle.js",
"scripts": { "scripts": {
"build": "node build", "build": "node build",
"build:dist": "rollup -c rollup.config.js" "build:tsc": "tsc -b",
"build:bundles": "rollup -c rollup.config.js"
}, },
"devDependencies": { "devDependencies": {
"@iconify/core": "^1.0.0-rc.3", "@iconify/core": "^1.0.0-rc.3",

View File

@ -4,38 +4,44 @@ import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs'; import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript'; import typescript from '@rollup/plugin-typescript';
import sveltePreprocess from 'svelte-preprocess'; import sveltePreprocess from 'svelte-preprocess';
import pkg from './package.json';
// Copy Icon.svelte // Directories
const rootDir = __dirname + '/';
const targetDir = 'dist';
const sourceDir = 'src';
const libDir = 'lib';
// Create dist
try { try {
fs.mkdirSync(__dirname + '/dist'); fs.mkdirSync(rootDir + targetDir);
} catch (err) {} } catch (err) {}
// Copy Svelte files
['OfflineIcon.svelte'].forEach((file) => { ['OfflineIcon.svelte'].forEach((file) => {
fs.writeFileSync( fs.writeFileSync(
__dirname + '/dist/' + file, rootDir + targetDir + '/' + file,
fs.readFileSync(__dirname + '/src/' + file) fs.readFileSync(rootDir + sourceDir + '/' + file)
); );
console.log('copied (original)', file);
}); });
// Create component.mjs // Copy compiled files (should not include anything that isn't bundled below)
fs.writeFileSync( ['offline.js'].forEach((file) => {
__dirname + '/dist/component.mjs', fs.writeFileSync(
fs.readFileSync(__dirname + '/src/index.ts') rootDir + targetDir + '/' + file,
); fs.readFileSync(rootDir + libDir + '/' + file)
);
console.log('copied (compiled)', file);
});
// Create bundle // Create bundle
const name = pkg.name
.replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3')
.replace(/^\w/, (m) => m.toUpperCase())
.replace(/-\w/g, (m) => m[1].toUpperCase());
export default [ export default [
// Bundle everything // Bundle everything
{ {
input: 'src/index.ts', input: sourceDir + '/offline.ts',
output: [ output: [
{ file: pkg.module, format: 'es' }, { file: targetDir + '/offline-bundle.mjs', format: 'es' },
{ file: pkg.main, format: 'umd', name }, { file: targetDir + '/offline-bundle.js', format: 'cjs' },
], ],
plugins: [ plugins: [
svelte({ svelte({
@ -48,12 +54,12 @@ export default [
commonjs(), commonjs(),
], ],
}, },
// Files included in OfflineIcon.svelte as bundles without dependencies // Files included in OfflineIcon.svelte as bundle
{ {
input: 'src/offline.ts', input: sourceDir + '/offline-functions.ts',
output: [ output: [
{ {
file: 'dist/offline.js', file: targetDir + '/offline-functions.js',
format: 'es', format: 'es',
}, },
], ],

View File

@ -1,5 +1,5 @@
<script> <script>
import { generateIcon } from './offline'; import { generateIcon } from './offline-functions';
// Generated data // Generated data
let data; let data;

View File

@ -1,2 +0,0 @@
export { default as Icon } from './OfflineIcon.svelte';
export { addIcon, addCollection } from './offline';

View File

@ -0,0 +1,68 @@
import type { IconifyIcon, IconifyJSON } from '@iconify/types';
import { fullIcon } from '@iconify/core/lib/icon';
import { parseIconSet } from '@iconify/core/lib/icon/icon-set';
import { render } from './render';
import type { RenderResult } from './render';
import type { IconProps } from './props';
/**
* Storage for icons referred by name
*/
const storage: Record<string, Required<IconifyIcon>> = Object.create(null);
/**
* Generate icon
*/
export function generateIcon(props: IconProps): RenderResult | null {
// Split properties
const icon =
typeof props.icon === 'string'
? storage[props.icon]
: typeof props.icon === 'object'
? fullIcon(props.icon)
: null;
// Validate icon object
if (
icon === null ||
typeof icon !== 'object' ||
typeof icon.body !== 'string'
) {
return null;
}
return render(icon, props);
}
/**
* Add icon to storage, allowing to call it by name
*
* @param name
* @param data
*/
export function addIcon(name: string, data: IconifyIcon): void {
storage[name] = fullIcon(data);
}
/**
* Add collection to storage, allowing to call icons by name
*
* @param data Icon set
* @param prefix Optional prefix to add to icon names, true (default) if prefix from icon set should be used.
*/
export function addCollection(
data: IconifyJSON,
prefix?: string | boolean
): void {
const iconPrefix: string =
typeof prefix === 'string'
? prefix
: prefix !== false && typeof data.prefix === 'string'
? data.prefix + ':'
: '';
parseIconSet(data, (name, icon) => {
if (icon !== null) {
storage[iconPrefix + name] = icon;
}
});
}

View File

@ -1,68 +1,2 @@
import type { IconifyIcon, IconifyJSON } from '@iconify/types'; export { default as Icon } from './OfflineIcon.svelte';
import { fullIcon } from '@iconify/core/lib/icon'; export { addIcon, addCollection } from './offline-functions';
import { parseIconSet } from '@iconify/core/lib/icon/icon-set';
import { render } from './render';
import type { RenderResult } from './render';
import type { IconProps } from './props';
/**
* Storage for icons referred by name
*/
const storage: Record<string, Required<IconifyIcon>> = Object.create(null);
/**
* Generate icon
*/
export function generateIcon(props: IconProps): RenderResult | null {
// Split properties
const icon =
typeof props.icon === 'string'
? storage[props.icon]
: typeof props.icon === 'object'
? fullIcon(props.icon)
: null;
// Validate icon object
if (
icon === null ||
typeof icon !== 'object' ||
typeof icon.body !== 'string'
) {
return null;
}
return render(icon, props);
}
/**
* Add icon to storage, allowing to call it by name
*
* @param name
* @param data
*/
export function addIcon(name: string, data: IconifyIcon): void {
storage[name] = fullIcon(data);
}
/**
* Add collection to storage, allowing to call icons by name
*
* @param data Icon set
* @param prefix Optional prefix to add to icon names, true (default) if prefix from icon set should be used.
*/
export function addCollection(
data: IconifyJSON,
prefix?: string | boolean
): void {
const iconPrefix: string =
typeof prefix === 'string'
? prefix
: prefix !== false && typeof data.prefix === 'string'
? data.prefix + ':'
: '';
parseIconSet(data, (name, icon) => {
if (icon !== null) {
storage[iconPrefix + name] = icon;
}
});
}

View File

@ -14,7 +14,7 @@ export type IconifyIconCustomisations = IconCustomisations & {
*/ */
export interface IconifyIconProps extends IconifyIconCustomisations { export interface IconifyIconProps extends IconifyIconCustomisations {
// Icon object // Icon object
icon: IconifyIcon; icon: IconifyIcon | string;
// Style // Style
color?: string; color?: string;

View File

@ -1,9 +1,10 @@
{ {
"compilerOptions": { "compilerOptions": {
"rootDir": "src", "rootDir": "src",
"outDir": "lib",
"target": "es2017", "target": "es2017",
"module": "esnext", "module": "esnext",
"declaration": false, "declaration": true,
"sourceMap": false, "sourceMap": false,
"strict": true, "strict": true,
"types": ["node", "svelte"], "types": ["node", "svelte"],