tshy-after
Version:
Auto set package.json after tshy run
86 lines • 4.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const cwd = process.cwd() + node_path_1.default.sep;
const pkg = JSON.parse((0, node_fs_1.readFileSync)(node_path_1.default.join(cwd, 'package.json'), 'utf-8'));
// make sure commonjs *.d.ts can import types
pkg.types = pkg.exports['.'].require.types;
(0, node_fs_1.writeFileSync)('package.json', JSON.stringify(pkg, null, 2) + '\n');
(0, node_fs_1.writeFileSync)('dist/package.json', JSON.stringify({
name: pkg.name,
version: pkg.version,
}, null, 2) + '\n');
// Replace all `( import.meta.url )` into `('import_meta_url_placeholder_by_tshy_after')` on commonjs.
const IMPORT_META_URL = '(' + 'import.meta.url' + ')';
const IMPORT_META_URL_PLACE_HOLDER = '(\'import_meta_url_placeholder_by_tshy_after\')';
// Replace all `import.meta.resolve (xxx)` into `require.resolve(xxx)` on commonjs.
const IMPORT_META_RESOLVE = 'import.meta.resolve' + '(';
const IMPORT_META_RESOLVE_PLACE_HOLDER = 'require.resolve(';
function replaceImportMetaUrl(baseDir) {
const names = (0, node_fs_1.readdirSync)(baseDir);
for (const name of names) {
const filepath = node_path_1.default.join(baseDir, name);
const stat = (0, node_fs_1.statSync)(filepath);
if (stat.isDirectory()) {
replaceImportMetaUrl(filepath);
continue;
}
if (!filepath.endsWith('.js')) {
continue;
}
let content = (0, node_fs_1.readFileSync)(filepath, 'utf-8');
let changed = false;
if (content.includes(IMPORT_META_URL)) {
changed = true;
content = content.replaceAll(IMPORT_META_URL, IMPORT_META_URL_PLACE_HOLDER);
console.log('Auto fix "import.meta.url" on %s', filepath.replace(cwd, ''));
}
if (content.includes(IMPORT_META_RESOLVE)) {
changed = true;
content = content.replaceAll(IMPORT_META_RESOLVE, IMPORT_META_RESOLVE_PLACE_HOLDER);
console.log('Auto fix "import.meta.resolve" on %s', filepath.replace(cwd, ''));
}
if (changed) {
(0, node_fs_1.writeFileSync)(filepath, content);
}
}
}
replaceImportMetaUrl(node_path_1.default.join(cwd, 'dist/commonjs'));
// Copy image/json/web files
const fileExts = [
'.jpg', '.jpeg', '.png', '.gif', '.webp', '.ico',
'.json', '.html', '.htm', '.css',
];
const sourceDir = node_path_1.default.join(cwd, 'src');
const commonjsDir = node_path_1.default.join(cwd, 'dist/commonjs');
const esmDir = node_path_1.default.join(cwd, 'dist/esm');
function copyFiles(baseDir) {
const names = (0, node_fs_1.readdirSync)(baseDir);
for (const name of names) {
const filepath = node_path_1.default.join(baseDir, name);
const stat = (0, node_fs_1.statSync)(filepath);
if (stat.isDirectory()) {
copyFiles(filepath);
continue;
}
const extname = node_path_1.default.extname(filepath);
if (!fileExts.includes(extname)) {
continue;
}
let targetFilepath = filepath.replace(sourceDir, commonjsDir);
(0, node_fs_1.mkdirSync)(node_path_1.default.dirname(targetFilepath), { recursive: true });
(0, node_fs_1.copyFileSync)(filepath, targetFilepath);
console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, ''));
targetFilepath = filepath.replace(sourceDir, esmDir);
(0, node_fs_1.mkdirSync)(node_path_1.default.dirname(targetFilepath), { recursive: true });
(0, node_fs_1.copyFileSync)(filepath, targetFilepath);
console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, ''));
}
}
copyFiles(sourceDir);
//# sourceMappingURL=index.js.map