From 7fb8bd2c57a3df99c185af1b3a9b78f379eb7acb Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 27 Dec 2023 17:21:49 +0200 Subject: [PATCH] chore: use module as default for svelte component --- components/svelte/build.js | 37 +++----- components/svelte/package.json | 15 ++- components/svelte/rollup.config.js | 48 ++++++++++ components/svelte/rollup.config.mjs | 94 ------------------- .../{vitest.config.mjs => vitest.config.js} | 0 5 files changed, 73 insertions(+), 121 deletions(-) create mode 100644 components/svelte/rollup.config.js delete mode 100644 components/svelte/rollup.config.mjs rename components/svelte/{vitest.config.mjs => vitest.config.js} (100%) diff --git a/components/svelte/build.js b/components/svelte/build.js index 4f8e53b..8703983 100644 --- a/components/svelte/build.js +++ b/components/svelte/build.js @@ -1,5 +1,5 @@ -const fs = require('fs'); -const child_process = require('child_process'); +import { readFileSync, writeFileSync, readdirSync } from 'node:fs'; +import { spawnSync } from 'node:child_process'; // List of commands to run 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 Object.keys(compile).forEach((key) => { if (!compile[key]) { @@ -89,8 +79,7 @@ Object.keys(compile).forEach((key) => { * Get all api-extractor.*.json files */ function apiFiles() { - return fs - .readdirSync(__dirname) + return readdirSync('.') .map((item) => { const parts = item.split('.'); if (parts.pop() !== 'json' || parts.shift() !== 'api-extractor') { @@ -117,10 +106,10 @@ function next() { } 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, stdio: 'inherit', }); @@ -138,13 +127,12 @@ next(); */ function cleanup() { // Merge TypeScript files - const sourceDir = __dirname + '/src/'; - const distDir = __dirname + '/dist/'; + const sourceDir = './src/'; + const distDir = './dist/'; function createTypes() { // Get Svelte file, split it. Import and content should be separated by empty line - const svelteParts = fs - .readFileSync(sourceDir + 'svelte.d.ts', 'utf8') + const svelteParts = readFileSync(sourceDir + 'svelte.d.ts', 'utf8') .trim() .replace(/\r/g, '') .split('\n\n'); @@ -179,20 +167,19 @@ function cleanup() { ].forEach((item) => { const content = svelteImport + - fs - .readFileSync(distDir + item.source, 'utf8') + readFileSync(distDir + item.source, 'utf8') .replace('export { }', '') .trim() + svelteContent; - fs.writeFileSync(distDir + item.target, content, 'utf8'); + writeFileSync(distDir + item.target, content, 'utf8'); console.log(`Created dist/${item.target}`); }); } function copyComponents() { ['Icon.svelte', 'OfflineIcon.svelte'].forEach((name) => { - const content = fs.readFileSync(sourceDir + name, 'utf8'); - fs.writeFileSync(distDir + name, content, 'utf8'); + const content = readFileSync(sourceDir + name, 'utf8'); + writeFileSync(distDir + name, content, 'utf8'); console.log(`Copied dist/${name}`); }); } diff --git a/components/svelte/package.json b/components/svelte/package.json index 0039198..61f9ccd 100644 --- a/components/svelte/package.json +++ b/components/svelte/package.json @@ -2,10 +2,11 @@ "name": "@iconify/svelte", "description": "Iconify icon component for Svelte.", "author": "Vjacheslav Trushkin", - "version": "4.0.0-beta.3", + "version": "4.0.0-beta.4", "publishConfig": { "tag": "next" }, + "type": "module", "license": "MIT", "bugs": "https://github.com/iconify/iconify/issues", "homepage": "https://github.com/iconify/iconify", @@ -30,6 +31,16 @@ "svelte": "./dist/OfflineIcon.svelte", "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": { @@ -37,7 +48,7 @@ "prebuild": "pnpm run cleanup", "build": "node build", "build:tsc": "tsc -b", - "build:bundles": "rollup -c rollup.config.mjs", + "build:bundles": "rollup -c rollup.config.js", "build:api": "node build --only-api", "test": "vitest" }, diff --git a/components/svelte/rollup.config.js b/components/svelte/rollup.config.js new file mode 100644 index 0000000..824f6b1 --- /dev/null +++ b/components/svelte/rollup.config.js @@ -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()], + }, +]; diff --git a/components/svelte/rollup.config.mjs b/components/svelte/rollup.config.mjs deleted file mode 100644 index 7d2888c..0000000 --- a/components/svelte/rollup.config.mjs +++ /dev/null @@ -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(), - ], - }, -]; diff --git a/components/svelte/vitest.config.mjs b/components/svelte/vitest.config.js similarity index 100% rename from components/svelte/vitest.config.mjs rename to components/svelte/vitest.config.js