Skip to content

Commit

Permalink
Merge pull request highcharts#22498 from highcharts/tools/webpack-ext…
Browse files Browse the repository at this point in the history
…ernals-json

Tools: Webpack with externals as JSON
  • Loading branch information
TorsteinHonsi authored Jan 15, 2025
2 parents 61b4b97 + 60f326f commit 7851177
Show file tree
Hide file tree
Showing 9 changed files with 4,323 additions and 2,225 deletions.
5,664 changes: 3,827 additions & 1,837 deletions package-lock.json

Large diffs are not rendered by default.

120 changes: 108 additions & 12 deletions tools/gulptasks/scripts-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,71 @@ function removeHighcharts() {
fsLib.deleteDirectory('js/masters', true);
}

/**
* Replaces product placeholders with date and given data.
*
* @param {string} fileOrFolder
* File or folder of files to replace in. (recursive)
*
* @param {string} productName
* Official product name to replace with.
*
* @param {string} version
* Version string to replace with.
*
* @return {Promise<void>}
* Promise to keep.
*/
async function replaceProductPlaceholders(
fileOrFolder,
productName,
productVersion,
productDate = new Date()
) {
const fsp = require('node:fs/promises');
const fsLib = require('../libs/fs');

if (fsLib.isFile(fileOrFolder)) {
const fileContent = await fsp.readFile(fileOrFolder, 'utf8');

if (!fileContent.includes('@product.')) {
return;
}

await fsp.writeFile(fileOrFolder, fileContent.replace(
/@product\.(\w+)@/gsu,
(match, group1) => {
switch (group1) {
case 'assetPrefix':
return 'dashboards';
case 'date':
return productDate.toISOString().substring(0, 10);
case 'name':
return productName;
case 'version':
return productVersion;
default:
return match;
}
}
), 'utf8');
}

if (fsLib.isDirectory(fileOrFolder)) {
for (const file of fsLib.getFilePaths(fileOrFolder)) {
if (file.endsWith('.src.js')) {
await replaceProductPlaceholders(
file,
productName,
productVersion,
productDate
);
}
}
}

}

/**
* Builds files of `/ts` folder into `/js` folder.
*
Expand Down Expand Up @@ -71,49 +136,80 @@ async function scriptsTS(argv) {
// fsLib.deleteDirectory(fsLib.path('code/datagrid'), true);
}

fsLib.deleteDirectory('js/', true);
fsLib.deleteDirectory('js', true);

fsLib.copyAllFiles(
'ts',
argv.assembler ? 'js' : 'code/es-modules/',
argv.assembler ? 'js' : fsLib.path(['code', 'es-modules']),
true,
sourcePath => sourcePath.endsWith('.d.ts')
);

if (argv.dashboards) {
await processLib
.exec(`npx tsc -p ${typeScriptFolder} --outDir js/`);
.exec(`npx tsc -p ${typeScriptFolder} --outDir js`);
} else if (argv.datagrid) {
await processLib
.exec(`npx tsc -p ${typeScriptFolderDatagrid} --outDir js/`);
.exec(`npx tsc -p ${typeScriptFolderDatagrid} --outDir js`);
} else if (argv.assembler) {
await processLib
.exec('npx tsc -p ts --outDir js/');
.exec('npx tsc -p ts --outDir js');
} else {
await processLib
.exec('npx tsc -p ts');
await processLib
.exec('npx tsc -p ts/masters-es5');
.exec('npx tsc -p ' + fsLib.path(['ts', 'masters-es5']));

const buildPropertiesJSON =
fsLib.getFile('build-properties.json', true);
const packageJSON =
fsLib.getFile('package.json', true);
const esmFiles =
fsLib.getFilePaths(fsLib.path(['code', 'es-modules']), true);

for (const file of esmFiles) {
if (
file.includes('dashboards') ||
file.includes('datagrid')
) {
continue;
}
await replaceProductPlaceholders(
file,
'Highcharts',
(
argv.release ||
buildPropertiesJSON.version ||
packageJSON.version
)
);
}
}

if (argv.dashboards) {
removeHighcharts();

// Remove DataGrid
fsLib.deleteDirectory('js/datagrid/', true);
fsLib.deleteDirectory('js/DataGrid/', true);
fsLib.deleteDirectory(fsLib.path(['js', 'datagrid']), true);
fsLib.deleteDirectory(fsLib.path(['js', 'DataGrid']), true);

// Fix masters
fs.renameSync('js/masters-dashboards/', 'js/masters/');
fs.renameSync(
fsLib.path(['js', 'masters-dashboards']),
fsLib.path(['js', 'masters'])
);
} else if (argv.datagrid) {
removeHighcharts();

// Fix masters
fs.renameSync('js/masters-datagrid/', 'js/masters/');
fs.renameSync(
fsLib.path(['js', 'masters-datagrid']),
fsLib.path(['js', 'masters'])
);
} else {
// Remove Dashboards
fsLib.deleteDirectory('js/Dashboards/', true);
fsLib.deleteDirectory('js/DataGrid/', true);
fsLib.deleteDirectory(fsLib.path(['js', 'Dashboards']), true);
fsLib.deleteDirectory(fsLib.path(['js', 'DataGrid']), true);
}

processLib.isRunning('scripts-ts', false);
Expand Down
44 changes: 43 additions & 1 deletion tools/libs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,28 @@ function getDirectoryPaths(directoryPath, includeSubDirectories) {
return directoryPaths;
}

/**
* Get file content.
*
* @param {string} filePath
* File path to read from.
*
* @param {boolean} [asJSON]
* Whether to parse as JSON.
*
* @return {string|*}
* UTF-8 content or JSON.
*/
function getFile(filePath, asJSON) {
let data = FS.readFileSync(filePath, 'utf8');

if (asJSON) {
data = JSON.parse(data);
}

return data;
}

/**
* Calculates the SHA256 hash of a files content.
*
Expand Down Expand Up @@ -610,6 +632,24 @@ function path(
return path;
}

/**
* Set file content.
*
* @param {string} filePath
* File path to write to.
*
* @param {string|*} [data]
* UTF-8 content or JSON.
*/
function setFile(filePath, data) {

if (typeof data !== 'string') {
data = JSON.stringify(data, null, 4);
}

FS.writeFileSync(filePath, data, 'utf8');

}

/* *
*
Expand All @@ -625,6 +665,7 @@ module.exports = {
deleteFile,
getDirectoryHash,
getDirectoryPaths,
getFile,
getFileHash,
getFilePaths,
gzipFile,
Expand All @@ -636,5 +677,6 @@ module.exports = {
moveAllFiles,
normalizePath,
parentPath,
path
path,
setFile
};
149 changes: 149 additions & 0 deletions tools/webpacks/externals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
[
{
"files": [
"Core/Axis/Color/ColorAxis",
"Series/ColorMapComposition"
],
"included": [
"modules/coloraxis",
"modules/heatmap",
"modules/map",
"modules/sunburst",
"modules/treemap"
],
"namespacePath": ".{name}"
},
{
"files": ["Core/HttpUtilities"],
"included": [
"modules/data",
"modules/exporting"
],
"namespacePath": ".{name}"
},
{
"files": ["Extensions/Annotations/NavigationBindings"],
"included": [
"modules/annotations",
"modules/annotations-advanced",
"modules/stock-tools"
],
"namespacePath": ".{name}"
},
{
"files": ["Extensions/DataGrouping/ApproximationRegistry"],
"included": [
"modules/datagrouping",
"modules/stock"
],
"namespacePath": ".dataGrouping.approximations"
},
{
"files": ["Gantt/Pathfinder"],
"included": [
"modules/gantt",
"modules/pathfinder"
],
"namespacePath": ".{name}"
},
{
"files": [
"Stock/Navigator/Navigator",
"Stock/Scrollbar/Scrollbar"
],
"included": [
"modules/accessibility",
"modules/gantt",
"modules/navigator",
"modules/stock"
],
"namespacePath": ".{name}"
},
{
"files": ["Stock/RangeSelector/RangeSelector"],
"included": [
"modules/accessibility",
"modules/gantt",
"modules/stock"
],
"namespacePath": ".{name}"
},
{
"files": [
"Core/Animation/AnimationUtilities",
"Core/Defaults",
"Core/Globals",
"Core/Renderer/RendererUtilities",
"Core/Utilities"
],
"included": [],
"namespacePath": ""
},
{
"files": [
"Core/Animation/Fx",
"Core/Axis/Axis",
"Core/Axis/PlotLineOrBand/PlotLineOrBand",
"Core/Axis/Stacking/StackItem",
"Core/Axis/Tick",
"Core/Chart/Chart",
"Core/Color/Color",
"Core/Legend/Legend",
"Core/Legend/LegendSymbol",
"Core/Pointer",
"Core/Renderer/HTML/AST",
"Core/Renderer/SVG/SVGElement",
"Core/Renderer/SVG/SVGRenderer",
"Core/Renderer/RendererRegistry",
"Core/Series/DataLabel",
"Core/Series/Point",
"Core/Series/Series",
"Core/Series/SeriesRegistry",
"Core/Templating",
"Core/Time",
"Core/Tooltip"
],
"included": [],
"namespacePath": ".{name}"
},
{
"files": ["Series/Area/AreaSeries"],
"included": [],
"namespacePath": ".Series.types.area"
},
{
"files": ["Series/Area/AreaSplineSeries"],
"included": [],
"namespacePath": ".Series.types.areaspline"
},
{
"files": ["Series/Area/BarSeries"],
"included": [],
"namespacePath": ".Series.types.bar"
},
{
"files": ["Series/Area/ColumnSeries"],
"included": [],
"namespacePath": ".Series.types.column"
},
{
"files": ["Series/Area/LineSeries"],
"included": [],
"namespacePath": ".Series.types.line"
},
{
"files": ["Series/Area/PieSeries"],
"included": [],
"namespacePath": ".Series.types.pie"
},
{
"files": ["Series/Area/ScatterSeries"],
"included": [],
"namespacePath": ".Series.types.scatter"
},
{
"files": ["Series/Area/SplineSeries"],
"included": [],
"namespacePath": ".Series.types.spline"
}
]
Loading

0 comments on commit 7851177

Please sign in to comment.