Skip to content

Commit

Permalink
feat: add script add-exports and add comments to add-exports-to-packa…
Browse files Browse the repository at this point in the history
…ge.json.js
  • Loading branch information
shinokada committed Mar 5, 2023
1 parent bc765e2 commit d03f5e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write .",
"write-package-json": "node ./scripts/package-json-writer.js",
"add-exports": "node ./scripts/add-exports-to-package-json.js",
"package:publish": "standard-version && git push --follow-tags origin main && npm run package && npm run write-package-json && npm publish"
},
"files": [
Expand Down
26 changes: 26 additions & 0 deletions scripts/add-exports-to-package-json.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
/*
This script reads the files in the dist directory and creates a new exports object in the package.json file with the appropriate svelte and types paths for each Svelte component file found in the dist directory, except for index.js and index.d.ts.
The resulting exports object is structured as follows:
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js"
},
"./<component-name>.svelte": {
"types": "./dist/<component-name>.svelte.d.ts",
"svelte": "./dist/<component-name>.svelte"
},
...
}
where <component-name> represents the name of each Svelte component file found in the dist directory.
*/

// Import the 'fs' module to access the file system
import fs from 'fs';

// Read the list of files in the "./dist" directory and filter out non-.svelte files.
const files = fs.readdirSync('./dist').filter((file) => file.endsWith('.svelte'));

// Create the initial "exports" object with an entry for "./dist/index.*".
const exports = {
'.': {
types: './dist/index.d.ts',
svelte: './dist/index.js',
},
};

// Iterate over each ".svelte" file in the "./dist" directory.
// For each file, add an entry to the "exports" object with the svelte and types paths updated.
files.forEach((file) => {
if (file !== 'index.svelte') {
const name = file.replace('.svelte', '');
Expand All @@ -20,6 +45,7 @@ files.forEach((file) => {
}
});

// Read the package.json file, update the "exports" field, and write the file back out.
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
packageJson.exports = exports;

Expand Down

0 comments on commit d03f5e2

Please sign in to comment.