Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@
"start": "dumi dev",
"build": "dumi build",
"compile": "father build && lessc assets/index.less assets/index.css",
"browser-field": "node scripts/update-browser-field.js",
"gh-pages": "npm run build && father doc deploy",
"prepublishOnly": "npm run compile && rc-np",
"prepublishOnly": "npm run compile && npm run browser-field && rc-np",
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
"lint:tsc": "tsc -p tsconfig.json --noEmit",
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
Expand Down
45 changes: 45 additions & 0 deletions scripts/update-browser-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');

const rootDir = path.resolve(__dirname, '..');
const pkgPath = path.join(rootDir, 'package.json');

const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

const browserEntries = [];

const addEntry = (from, to) => {
browserEntries.push([from, to]);
};

// Preserve previous behavior of preferring the ES build for the main entry.
addEntry('./lib/index.js', './es/index.js');

const addDirMappings = (dirPath, browserPrefix, targetPrefix) => {
if (!fs.existsSync(dirPath)) {
return;
}

fs.readdirSync(dirPath)
.filter((file) => file.endsWith('.js'))
.sort()
.forEach((file) => {
const name = path.basename(file, '.js');
const target = `${targetPrefix}/${file}`;

addEntry(`${browserPrefix}/${name}`, target);
addEntry(`${browserPrefix}/${name}.js`, target);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of addDirMappings doesn't check if a directory entry is a file. If a directory name happens to end with .js, it would be processed, leading to incorrect entries in the browser field.

To make this more robust, you can use the withFileTypes: true option in fs.readdirSync and filter for files only. This is more efficient than using fs.statSync in a loop and ensures only files are processed.

  fs.readdirSync(dirPath, { withFileTypes: true })
    .filter((dirent) => dirent.isFile() && dirent.name.endsWith('.js'))
    .map((dirent) => dirent.name)
    .sort()
    .forEach((file) => {
      const name = path.basename(file, '.js');
      const target = `${targetPrefix}/${file}`;

      addEntry(`${browserPrefix}/${name}`, target);
      addEntry(`${browserPrefix}/${name}.js`, target);
    });

};

addDirMappings(path.join(rootDir, 'es', 'locale'), './locale', './es/locale');
addDirMappings(path.join(rootDir, 'es', 'generate'), './generate', './es/generate');

const browser = Object.fromEntries(browserEntries.sort((a, b) => a[0].localeCompare(b[0])));

pkg.browser = browser;

fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);

console.log(`Updated browser field with ${browserEntries.length} entries.`);