Skip to content

Commit

Permalink
Removing wwwroot/index.global.d.ts and its generation as we no longer…
Browse files Browse the repository at this point in the history
… support typing restore and global namespaces mode scripts
  • Loading branch information
volkanceylan committed Sep 14, 2024
1 parent 1d44b39 commit 69736fe
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 7,436 deletions.
1 change: 1 addition & 0 deletions packages/corelib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dist/**/*.map",
"css/**/*.css",
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.css",
"src/**/tsconfig.json"
],
Expand Down
213 changes: 2 additions & 211 deletions packages/corelib/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import fs from 'fs';
import { basename, resolve } from "path";
import { dts } from "rollup-plugin-dts";
import { minify } from "terser";
import fs from 'fs';
import sourcemapsPlugin from './build/rollup-sourcemaps.js';
import typescript from '@rollup/plugin-typescript';

var externalPackages = ["@serenity-is/sleekgrid"];

Expand All @@ -13,174 +13,6 @@ var globals = {
'@serenity-is/sleekgrid': 'this.Slick = this.Slick || {}'
}

var dtsOutputs = [];

var rxNamespace = /^namespace\s?/;
var rxExport = /^export\s?/;
var rxClassTypeIntfEnum = /^(class|type|interface|enum)\s?/;
var rxDeclareGlobal = /^(declare\s+global.*\s*\{\r?\n)((^\s+.*\r?\n)*)?\}/gm;
var rxRemoveExports = /^export\s*\{[^\}]*\}\s*\;\s*\r?\n/gm;
var rxRemoveImports = /^\s*import\s*\{([\sA-Za-z0-9_,\$]*)\}\s*from\s*['"](.*)['"]\s*\;\s*\r?\n/gm;
var rxReExports = /^\s*export\s*\{([\sA-Za-z0-9_,\$]*)\}\s*from\s*['"](.*)['"]\s*\;\s*\r?\n/gm;
var rxModuleAugmentation = /^(declare\s+module\s*['"]([A-Za-z\/@\-]+)['"]\s*\{\r?\n)((^\s+.*\r?\n)*)?\}/gm;
var referenceTypeCommentsRegex = /^\/\/\/\s*\<reference\s*types\=\".*\r?\n/gm

const replaceTypeRef = function (src, name, rep) {
return src.replace(new RegExp('([<>:,]\\s*)' + name.replace('$', '\\$') + '([^A-Za-z0-9_\\$])', 'g'), (m, p1, p2) => p1 + rep + p2);
}

function moduleToGlobalName(fromModule) {
return fromModule == "@serenity-is/sleekgrid" ? 'Slick' :
((fromModule == "@serenity-is/base" || fromModule == "@serenity-is/base-ui") ? 'Serenity' : null);
}

const convertModularToGlobal = (src, ns, isTS) => {
src = src.replace('sourceItem?: PropertyItem', 'sourceItem?: Serenity.PropertyItem');

var refTypes = [];
src = src.replace(referenceTypeCommentsRegex, function (match) {
refTypes.push(match);
return '';
});

src = src.replace(/\r/g, '');
src = src.replace(rxRemoveExports, '');

src = src.replace(rxReExports, function (match, exportList, fromModule) {
var g = moduleToGlobalName(fromModule)
if (!g) {
return match;
}

var list = [];

for (var part of exportList.split(',')) {
var x = part.trim();
if (ns != g && x) {
var y = x.split(' as ');

var equalsToType = g + '.' + (y[1] || y[0]).trim();
var typeAlias = y[0].trim();

list.push('export import ' + typeAlias + ' = ' + equalsToType + ';')
}
}

return list.join('\n') + '\n';
});


var imports = {};

src = src.replace(rxRemoveImports, function (match, p1, p2) {
var g = moduleToGlobalName(p2);

if (!g) {
return match;
}

imports[g] = imports[g] || [];
for (var part of p1.split(',')) {
var x = part.trim();
if (ns != g && x) {
var y = x.split(' as ');
imports[g].push({ alias: (y[1] || y[0]).trim(), name: y[0].trim() });
}
}
return '';
});

for (var k in imports) {
if (k != ns) {
for (var i of imports[k]) {
src = replaceTypeRef(src, i.alias, k + '.' + i.name);
}
}
}

if (ns == 'Serenity') {
src = src.replace(': typeof executeOnceWhenVisible', ': typeof Q.executeOnceWhenVisible');
src = src.replace(': typeof executeEverytimeWhenVisible', ': typeof Q.executeEverytimeWhenVisible');
}

var moduleAugmentations = [];
src = src.replace(rxModuleAugmentation, function (match, p1, p2, p3, p4, offset, str) {
if (p2 == '@serenity-is/sleekgrid')
moduleAugmentations.push('declare namespace Slick {\n' + p3 + '}\n\n');
else
moduleAugmentations.push(match);
return '';
});

var globals = [];
src = src.replace(rxDeclareGlobal, function (x, y, m1) {
if (!m1)
return '';
var g = m1.replace(/(\r?\n)*$/, '').split('\n')
.map(s => s.length > 0 && s.charAt(0) == '\t' ? s.substring(1) : (s.substring(0, 4) == ' ' ? s.substring(4) : s))
.map(s => {
if (rxNamespace.test(s))
return ("declare " + s);
else if (rxExport.test(s))
return "declare " + s.substring(7);
else
return s;
})
.join('\n');
globals.push(g);
return '';
});

if (ns) {
src = 'declare namespace ' + ns + ' {\n' +
src.replace(/(\r?\n)*$/, '').split('\n').map(s => {
if (!s.length)
return '';
if (/^declare\s+/.test(s))
return ' ' + (isTS ? 'export ' : '') + s.substring(8);
if (/^export\sdeclare\s+/.test(s))
return ' ' + (isTS ? 'export ' : '') + s.substring(15);
if (/^export\sinterface\s+/.test(s))
return ' ' + (isTS ? 'export ' : '') + s.substring(7);
if (rxClassTypeIntfEnum.test(s))
return ' ' + (isTS ? 'export ' : '') + s;
return ' ' + s;
}).join('\n') + '\n}'
}

for (var i = 0; i < 2; i++)
src = src.replace(/^\r?\n\r?\n/gm, '\n');
src = refTypes.join('') + '\n' + moduleAugmentations.join('') + src + '\n' + globals.join('\n');
return src;
}

var toGlobal = function (ns, outFile, isTS) {
return {
name: 'toGlobal',
generateBundle(o, b) {
for (var fileName of Object.keys(b)) {
if (b[fileName].code && fileName.indexOf('bundle.d.ts') >= 0) {
var src = b[fileName].code;
src = convertModularToGlobal(src, ns, isTS);
if (outFile) {
fs.writeFileSync(outFile, src);
}
else {
dtsOutputs.push(src);

var toEmit = {
type: 'asset',
fileName: fileName.replace('bundle.d.ts', 'bundle.global.d.ts'),
source: src
};
this.emitFile(toEmit);
}
}
}
}
}
}

async function minifyScript(fileName) {
var minified = await minify({ [basename(fileName)]: fs.readFileSync(fileName, 'utf8') }, {
mangle: true,
Expand All @@ -198,17 +30,6 @@ async function minifyScript(fileName) {
fs.writeFileSync(fileName.replace(/\.js$/, '.min.js.map'), minified.map);
}

const mergeReferenceTypeComments = (src) => {
var refTypes = [];
src = src.replace(referenceTypeCommentsRegex, function (match, offset, str) {
refTypes.push(match);
return '';
});
refTypes = refTypes.filter((x, i) => refTypes.indexOf(x) == i);
src = refTypes.join('') + '\n' + src
return src;
}

const nodeResolvePlugin = () => nodeResolve({
resolveOnly: ['@serenity-is/base', '@serenity-is/base-ui', 'jsx-dom']
});
Expand Down Expand Up @@ -274,39 +95,12 @@ export default [
dts({
respectExternal: true
}),
toGlobal('Serenity'),
{
name: 'writeFinal',
writeBundle: {
sequential: true,
order: 'post',
async handler({ dir }) {
dtsOutputs.splice(0, 0, convertModularToGlobal(fs.readFileSync("./node_modules/@serenity-is/sleekgrid/dist/index.d.ts").toString(), 'Slick'));
dtsOutputs.push(`
import Q = Serenity;
declare namespace Slick {
namespace Data {
/** @obsolete use the type exported from @serenity-is/sleekgrid */
export import GroupItemMetadataProvider = Slick.GroupItemMetadataProvider;
}
export import AggregateFormatting = Serenity.AggregateFormatting;
export import Aggregators = Serenity.Aggregators;
export import CancellableViewCallback = Serenity.CancellableViewCallback;
export import Formatter = Serenity.Formatter;
export import GroupInfo = Serenity.GroupInfo;
export import PagerOptions = Serenity.PagerOptions;
export import PagingInfo = Serenity.PagingInfo;
export import PagingOptions = Serenity.PagingOptions;
export import RemoteView = Serenity.RemoteView;
export import RemoteViewAjaxCallback = Serenity.RemoteViewAjaxCallback;
export import RemoteViewFilter = Serenity.RemoteViewFilter;
export import RemoteViewOptions = Serenity.RemoteViewOptions;
export import RemoteViewProcessCallback = Serenity.RemoteViewProcessCallback;
export import SummaryOptions = Serenity.SummaryOptions;
}
`);

function writeIfDifferent(target, content) {
if (!fs.existsSync(target) ||
fs.readFileSync(target, 'utf8') != content) {
Expand All @@ -321,9 +115,6 @@ declare namespace Slick {
!fs.existsSync('./dist') && fs.mkdirSync('./dist');
copyIfDifferent('./out/index.bundle.d.ts', './dist/index.d.ts');
!fs.existsSync('./wwwroot') && fs.mkdirSync('./wwwroot');
var src = dtsOutputs.join('\n').replace(/\r/g, '');
src = mergeReferenceTypeComments(src);
writeIfDifferent('./wwwroot/index.global.d.ts', src);
copyIfDifferent('./out/index.global.js', './wwwroot/index.global.js');
copyIfDifferent('./out/index.global.js.map', './wwwroot/index.global.js.map');
await minifyScript('./out/index.global.js');
Expand Down
Loading

0 comments on commit 69736fe

Please sign in to comment.