mirror of
https://github.com/vega-org/vega-providers.git
synced 2026-04-17 15:41:45 +00:00
refactor: clean up code formatting and improve readability in build-bundled.js
This commit is contained in:
@@ -11,7 +11,9 @@ const providerDirs = fs
|
|||||||
.readdirSync(providersDir, { withFileTypes: true })
|
.readdirSync(providersDir, { withFileTypes: true })
|
||||||
.filter(
|
.filter(
|
||||||
(dirent) =>
|
(dirent) =>
|
||||||
dirent.isDirectory() && !dirent.name.startsWith(".") && dirent.name !== "extractors",
|
dirent.isDirectory() &&
|
||||||
|
!dirent.name.startsWith(".") &&
|
||||||
|
dirent.name !== "extractors",
|
||||||
)
|
)
|
||||||
.map((dirent) => dirent.name);
|
.map((dirent) => dirent.name);
|
||||||
|
|
||||||
@@ -45,8 +47,8 @@ async function buildProvider(providerName) {
|
|||||||
format: "cjs",
|
format: "cjs",
|
||||||
target: "es2015",
|
target: "es2015",
|
||||||
write: false,
|
write: false,
|
||||||
external: [], // Bundle everything
|
external: [],
|
||||||
minify: false, // We'll minify separately if needed
|
minify: false,
|
||||||
keepNames: true,
|
keepNames: true,
|
||||||
treeShaking: true,
|
treeShaking: true,
|
||||||
outfile: `${moduleName}.js`,
|
outfile: `${moduleName}.js`,
|
||||||
@@ -57,53 +59,51 @@ async function buildProvider(providerName) {
|
|||||||
// Post-process the code for React Native compatibility
|
// Post-process the code for React Native compatibility
|
||||||
// Remove require statements for built-in modules that aren't available
|
// Remove require statements for built-in modules that aren't available
|
||||||
code = code.replace(/require\(['"]node:.*?['"]\)/g, "{}");
|
code = code.replace(/require\(['"]node:.*?['"]\)/g, "{}");
|
||||||
|
|
||||||
// Fix CommonJS exports for React Native's ProviderManager execution context
|
|
||||||
// The ProviderManager only has `exports` object, not `module`
|
|
||||||
|
|
||||||
// Extract what's being exported by looking at the __export call
|
|
||||||
// Pattern: __export(xxx_exports, { funcName: () => funcName, ... });
|
|
||||||
const exportMatch = code.match(/__export\((\w+),\s*\{([^}]+)\}\);/);
|
const exportMatch = code.match(/__export\((\w+),\s*\{([^}]+)\}\);/);
|
||||||
|
|
||||||
if (exportMatch) {
|
if (exportMatch) {
|
||||||
const exportsVar = exportMatch[1];
|
const exportsVar = exportMatch[1];
|
||||||
const exportsContent = exportMatch[2];
|
const exportsContent = exportMatch[2];
|
||||||
|
|
||||||
// Parse the export entries like "funcName: () => funcName"
|
// Parse the export entries like "funcName: () => funcName"
|
||||||
const exportEntries = exportsContent
|
const exportEntries = exportsContent
|
||||||
.split(',')
|
.split(",")
|
||||||
.map(entry => {
|
.map((entry) => {
|
||||||
const match = entry.trim().match(/(\w+):\s*\(\)\s*=>\s*(\w+)/);
|
const match = entry.trim().match(/(\w+):\s*\(\)\s*=>\s*(\w+)/);
|
||||||
return match ? match[1] : null;
|
return match ? match[1] : null;
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
// Replace module.exports pattern
|
// Replace module.exports pattern
|
||||||
code = code.replace(
|
code = code.replace(
|
||||||
/module\.exports\s*=\s*__toCommonJS\((\w+)\);/g,
|
/module\.exports\s*=\s*__toCommonJS\((\w+)\);/g,
|
||||||
''
|
"",
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add direct exports assignments at the end
|
// Add direct exports assignments at the end
|
||||||
const directExports = exportEntries
|
const directExports = exportEntries
|
||||||
.map(name => `exports.${name} = ${name};`)
|
.map((name) => `exports.${name} = ${name};`)
|
||||||
.join('\n');
|
.join("\n");
|
||||||
|
|
||||||
// Add the exports before the final comment
|
// Add the exports before the final comment
|
||||||
code = code.replace(
|
code = code.replace(
|
||||||
/\/\/ Annotate the CommonJS export names for ESM import in node:/,
|
/\/\/ Annotate the CommonJS export names for ESM import in node:/,
|
||||||
`${directExports}\n// Annotate the CommonJS export names for ESM import in node:`
|
`${directExports}\n// Annotate the CommonJS export names for ESM import in node:`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also handle the "0 && (module.exports = {...})" pattern at the end
|
// Also handle the "0 && (module.exports = {...})" pattern at the end
|
||||||
code = code.replace(/0\s*&&\s*\(module\.exports\s*=\s*\{[^}]*\}\);?/g, "");
|
code = code.replace(
|
||||||
|
/0\s*&&\s*\(module\.exports\s*=\s*\{[^}]*\}\);?/g,
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
|
||||||
// Minify if not skipped
|
// Minify if not skipped
|
||||||
if (!SKIP_MINIFY) {
|
if (!SKIP_MINIFY) {
|
||||||
const minified = await minify(code, {
|
const minified = await minify(code, {
|
||||||
compress: {
|
compress: {
|
||||||
drop_console: false,
|
drop_console: true,
|
||||||
passes: 2,
|
passes: 2,
|
||||||
},
|
},
|
||||||
mangle: {
|
mangle: {
|
||||||
@@ -132,7 +132,10 @@ async function buildProvider(providerName) {
|
|||||||
`✓ ${providerName}/${moduleName}.js (${(code.length / 1024).toFixed(1)}kb)`,
|
`✓ ${providerName}/${moduleName}.js (${(code.length / 1024).toFixed(1)}kb)`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`✗ Error building ${providerName}/${moduleName}:`, error.message);
|
console.error(
|
||||||
|
`✗ Error building ${providerName}/${moduleName}:`,
|
||||||
|
error.message,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +144,9 @@ async function buildProvider(providerName) {
|
|||||||
|
|
||||||
async function buildAll() {
|
async function buildAll() {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
console.log(`Building providers${SKIP_MINIFY ? " (without minification)" : ""}...\n`);
|
console.log(
|
||||||
|
`Building providers${SKIP_MINIFY ? " (without minification)" : ""}...\n`,
|
||||||
|
);
|
||||||
|
|
||||||
// Clear dist directory
|
// Clear dist directory
|
||||||
const distDir = path.join(__dirname, "dist");
|
const distDir = path.join(__dirname, "dist");
|
||||||
|
|||||||
Reference in New Issue
Block a user