Skip to content

Commit 4b84208

Browse files
authored
Update package:browser field on pre-publish to support webpack 4 (#954)
Co-authored-by: João Pedro Fernandes <[email protected]>
1 parent b987f8c commit 4b84208

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
"start": "dumi dev",
9898
"build": "dumi build",
9999
"compile": "father build && lessc assets/index.less assets/index.css",
100+
"browser-field": "node scripts/update-browser-field.js",
100101
"gh-pages": "npm run build && father doc deploy",
101-
"prepublishOnly": "npm run compile && rc-np",
102+
"prepublishOnly": "npm run compile && npm run browser-field && rc-np",
102103
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
103104
"lint:tsc": "tsc -p tsconfig.json --noEmit",
104105
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",

scripts/update-browser-field.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env node
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const rootDir = path.resolve(__dirname, '..');
6+
const pkgPath = path.join(rootDir, 'package.json');
7+
8+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
9+
10+
const browserEntries = [];
11+
12+
const addEntry = (from, to) => {
13+
browserEntries.push([from, to]);
14+
};
15+
16+
// Preserve previous behavior of preferring the ES build for the main entry.
17+
addEntry('./lib/index.js', './es/index.js');
18+
19+
const addDirMappings = (dirPath, browserPrefix, targetPrefix) => {
20+
if (!fs.existsSync(dirPath)) {
21+
return;
22+
}
23+
24+
fs.readdirSync(dirPath, { withFileTypes: true })
25+
.filter((dirent) => dirent.isFile() && dirent.name.endsWith('.js'))
26+
.map((dirent) => dirent.name)
27+
.sort()
28+
.forEach((file) => {
29+
const name = path.basename(file, '.js');
30+
const target = `${targetPrefix}/${file}`;
31+
32+
addEntry(`${browserPrefix}/${name}`, target);
33+
addEntry(`${browserPrefix}/${name}.js`, target);
34+
});
35+
};
36+
37+
addDirMappings(path.join(rootDir, 'es', 'locale'), './locale', './es/locale');
38+
addDirMappings(path.join(rootDir, 'es', 'generate'), './generate', './es/generate');
39+
40+
const browser = Object.fromEntries(browserEntries.sort((a, b) => a[0].localeCompare(b[0])));
41+
42+
pkg.browser = browser;
43+
44+
fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
45+
46+
console.log(`Updated browser field with ${browserEntries.length} entries.`);

0 commit comments

Comments
 (0)