2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-17 10:05:09 +00:00

chore: use module as default for svelte component

This commit is contained in:
Vjacheslav Trushkin 2023-12-27 17:21:49 +02:00
parent be70a27dad
commit 7fb8bd2c57
5 changed files with 73 additions and 121 deletions

View File

@ -1,5 +1,5 @@
const fs = require('fs'); import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
const child_process = require('child_process'); import { spawnSync } from 'node:child_process';
// List of commands to run // List of commands to run
const commands = []; const commands = [];
@ -47,16 +47,6 @@ process.argv.slice(2).forEach((cmd) => {
} }
}); });
// Check if required modules in same monorepo are available
const fileExists = (file) => {
try {
fs.statSync(file);
} catch (e) {
return false;
}
return true;
};
// Compile packages // Compile packages
Object.keys(compile).forEach((key) => { Object.keys(compile).forEach((key) => {
if (!compile[key]) { if (!compile[key]) {
@ -89,8 +79,7 @@ Object.keys(compile).forEach((key) => {
* Get all api-extractor.*.json files * Get all api-extractor.*.json files
*/ */
function apiFiles() { function apiFiles() {
return fs return readdirSync('.')
.readdirSync(__dirname)
.map((item) => { .map((item) => {
const parts = item.split('.'); const parts = item.split('.');
if (parts.pop() !== 'json' || parts.shift() !== 'api-extractor') { if (parts.pop() !== 'json' || parts.shift() !== 'api-extractor') {
@ -117,10 +106,10 @@ function next() {
} }
if (item.cwd === void 0) { if (item.cwd === void 0) {
item.cwd = __dirname; item.cwd = '.';
} }
const result = child_process.spawnSync(item.cmd, item.args, { const result = spawnSync(item.cmd, item.args, {
cwd: item.cwd, cwd: item.cwd,
stdio: 'inherit', stdio: 'inherit',
}); });
@ -138,13 +127,12 @@ next();
*/ */
function cleanup() { function cleanup() {
// Merge TypeScript files // Merge TypeScript files
const sourceDir = __dirname + '/src/'; const sourceDir = './src/';
const distDir = __dirname + '/dist/'; const distDir = './dist/';
function createTypes() { function createTypes() {
// Get Svelte file, split it. Import and content should be separated by empty line // Get Svelte file, split it. Import and content should be separated by empty line
const svelteParts = fs const svelteParts = readFileSync(sourceDir + 'svelte.d.ts', 'utf8')
.readFileSync(sourceDir + 'svelte.d.ts', 'utf8')
.trim() .trim()
.replace(/\r/g, '') .replace(/\r/g, '')
.split('\n\n'); .split('\n\n');
@ -179,20 +167,19 @@ function cleanup() {
].forEach((item) => { ].forEach((item) => {
const content = const content =
svelteImport + svelteImport +
fs readFileSync(distDir + item.source, 'utf8')
.readFileSync(distDir + item.source, 'utf8')
.replace('export { }', '') .replace('export { }', '')
.trim() + .trim() +
svelteContent; svelteContent;
fs.writeFileSync(distDir + item.target, content, 'utf8'); writeFileSync(distDir + item.target, content, 'utf8');
console.log(`Created dist/${item.target}`); console.log(`Created dist/${item.target}`);
}); });
} }
function copyComponents() { function copyComponents() {
['Icon.svelte', 'OfflineIcon.svelte'].forEach((name) => { ['Icon.svelte', 'OfflineIcon.svelte'].forEach((name) => {
const content = fs.readFileSync(sourceDir + name, 'utf8'); const content = readFileSync(sourceDir + name, 'utf8');
fs.writeFileSync(distDir + name, content, 'utf8'); writeFileSync(distDir + name, content, 'utf8');
console.log(`Copied dist/${name}`); console.log(`Copied dist/${name}`);
}); });
} }

View File

@ -2,10 +2,11 @@
"name": "@iconify/svelte", "name": "@iconify/svelte",
"description": "Iconify icon component for Svelte.", "description": "Iconify icon component for Svelte.",
"author": "Vjacheslav Trushkin", "author": "Vjacheslav Trushkin",
"version": "4.0.0-beta.3", "version": "4.0.0-beta.4",
"publishConfig": { "publishConfig": {
"tag": "next" "tag": "next"
}, },
"type": "module",
"license": "MIT", "license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues", "bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://github.com/iconify/iconify", "homepage": "https://github.com/iconify/iconify",
@ -30,6 +31,16 @@
"svelte": "./dist/OfflineIcon.svelte", "svelte": "./dist/OfflineIcon.svelte",
"types": "./dist/offline.d.ts" "types": "./dist/offline.d.ts"
}, },
"./dist/functions": {
"types": "./lib/dist/functions.d.ts",
"require": "./lib/dist/functions.cjs",
"import": "./lib/dist/functions.js"
},
"./dist/offline-functions": {
"types": "./lib/dist/offline-functions.d.ts",
"require": "./lib/dist/offline-functions.cjs",
"import": "./lib/dist/offline-functions.js"
},
"./*": "./*" "./*": "./*"
}, },
"scripts": { "scripts": {
@ -37,7 +48,7 @@
"prebuild": "pnpm run cleanup", "prebuild": "pnpm run cleanup",
"build": "node build", "build": "node build",
"build:tsc": "tsc -b", "build:tsc": "tsc -b",
"build:bundles": "rollup -c rollup.config.mjs", "build:bundles": "rollup -c rollup.config.js",
"build:api": "node build --only-api", "build:api": "node build --only-api",
"test": "vitest" "test": "vitest"
}, },

View File

@ -0,0 +1,48 @@
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
// Directories
const targetDir = 'dist';
const sourceDir = 'src';
const mjsExt = '.js';
const cjsExt = '.cjs';
const resolveParams = {
extensions: ['.ts', '.mjs', '.js', '.cjs', '.svelte'],
dedupe: ['svelte'],
};
// Create bundle
export default [
// Files included in Icon.svelte as bundle
{
input: `${sourceDir}/functions.ts`,
output: [
{
file: `${targetDir}/functions${mjsExt}`,
format: 'es',
},
{
file: `${targetDir}/functions${cjsExt}`,
format: 'cjs',
},
],
plugins: [resolve(resolveParams), typescript()],
},
// Files included in OfflineIcon.svelte as bundle
{
input: `${sourceDir}/offline-functions.ts`,
output: [
{
file: `${targetDir}/offline-functions${mjsExt}`,
format: 'es',
},
{
file: `${targetDir}/offline-functions${cjsExt}`,
format: 'cjs',
},
],
plugins: [resolve(resolveParams), typescript()],
},
];

View File

@ -1,94 +0,0 @@
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import sveltePreprocess from 'svelte-preprocess';
// Directories
const targetDir = 'dist';
const sourceDir = 'src';
// Create bundle
export default [
// Bundle everything
/*
{
input: sourceDir + '/Icon.svelte',
output: [
{ file: targetDir + '/index.mjs', format: 'es' },
{ file: targetDir + '/index.js', format: 'cjs' },
],
plugins: [
svelte({
preprocess: sveltePreprocess(),
}),
resolve({
browser: true,
extensions: ['.ts', '.mjs', '.js', '.svelte'],
dedupe: ['svelte'],
}),
typescript(),
],
},
{
input: sourceDir + '/OfflineIcon.svelte',
output: [
{ file: targetDir + '/offline.mjs', format: 'es' },
{ file: targetDir + '/offline.js', format: 'cjs' },
],
plugins: [
svelte({
preprocess: sveltePreprocess(),
}),
resolve({
browser: true,
extensions: ['.ts', '.mjs', '.js', '.svelte'],
dedupe: ['svelte'],
}),
typescript(),
],
},
*/
// Files included in Icon.svelte as bundle
{
input: sourceDir + '/functions.ts',
output: [
{
file: targetDir + '/functions.mjs',
format: 'es',
},
{
file: targetDir + '/functions.js',
format: 'cjs',
},
],
plugins: [
resolve({
extensions: ['.ts', '.mjs', '.js', '.svelte'],
dedupe: ['svelte'],
}),
typescript(),
],
},
// Files included in OfflineIcon.svelte as bundle
{
input: sourceDir + '/offline-functions.ts',
output: [
{
file: targetDir + '/offline-functions.mjs',
format: 'es',
},
{
file: targetDir + '/offline-functions.js',
format: 'cjs',
},
],
plugins: [
resolve({
extensions: ['.ts', '.mjs', '.js', '.svelte'],
dedupe: ['svelte'],
}),
typescript(),
],
},
];