Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better separate type declarations #5695

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bundled declaration for ace-builds
mkslanc committed Dec 10, 2024
commit 8b99bfd3bb2be251738c94d18e8570b6d42eaad6
29 changes: 6 additions & 23 deletions Makefile.dryice.js
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ var build = require('architect-build/build');
var {
updateDeclarationModuleNames,
generateDeclaration,
SEPARATE_MODULES
bundleDtsFiles,
} = require('./tool/ace_declaration_generator');

var ACE_HOME = __dirname;
@@ -179,25 +179,16 @@ function ace() {
}
}

function correctDeclarationsForBuild(path, additionalDeclarations) {
function correctDeclarationsForBuild(path) {
var definitions = fs.readFileSync(path, 'utf8');
var newDefinitions = updateDeclarationModuleNames(definitions);
if (additionalDeclarations) {
newDefinitions = newDefinitions + '\n' + additionalDeclarations;
}
fs.writeFileSync(path, newDefinitions);
fs.writeFileSync(path, newDefinitions, "utf-8");
}

function buildTypes() {
// ace-builds package has different structure and can't use mode types defined for the ace-code.
var paths = fs.readdirSync(BUILD_DIR + '/src-noconflict');

var typeDir = BUILD_DIR + "/types";

if (!fs.existsSync(typeDir)) {
fs.mkdirSync(typeDir);
}

fs.readdirSync(BUILD_DIR + '/src-noconflict/snippets').forEach(function(path) {
paths.push("snippets/" + path);
});
@@ -215,20 +206,11 @@ function buildTypes() {
}
}).filter(Boolean)).join("\n") + "\n";

fs.copyFileSync(ACE_HOME + '/ace-internal.d.ts', BUILD_DIR + '/ace.d.ts');
generateDeclaration(BUILD_DIR + '/ace.d.ts');
generateDeclaration();
fs.copyFileSync(ACE_HOME + '/ace-modes.d.ts', BUILD_DIR + '/ace-modes.d.ts');
correctDeclarationsForBuild(BUILD_DIR + '/ace.d.ts', pathModules);
correctDeclarationsForBuild(BUILD_DIR + '/ace-modes.d.ts');
const finalDeclaration = bundleDtsFiles() + "\n" + pathModules;

let allModules = SEPARATE_MODULES;
allModules.push("modules"); // core modules
allModules.forEach(function (key) {
let fileName = '/ace-' + key + '.d.ts';
fs.copyFileSync(ACE_HOME + '/types' + fileName, BUILD_DIR + '/types' + fileName);
correctDeclarationsForBuild(BUILD_DIR + '/types' + fileName);
});

var esmUrls = [];

var loader = paths.map(function(path) {
@@ -243,6 +225,7 @@ function buildTypes() {
}).join('\n');
var esmLoader = esmUrls.join('\n');

fs.writeFileSync(BUILD_DIR + '/ace.d.ts', finalDeclaration, "utf8");
fs.writeFileSync(BUILD_DIR + '/webpack-resolver.js', loader, "utf8");
fs.writeFileSync(BUILD_DIR + '/esm-resolver.js', esmLoader, "utf8");
}
134 changes: 0 additions & 134 deletions ace.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -46,7 +46,6 @@
"lint": "eslint \"src/**/*.js\" \"*.js\"",
"fix": "npm run lint -- --fix",
"typecheck": "tsc -p tsconfig.json",
"typecheck-prod": "tsc -p tsconfig-prod.json",
"update-types": "node ./tool/ace_declaration_generator.js",
"changelog": "standard-version",
"prepack": "node tool/esm_resolver_generator.js && node tool/ace_declaration_generator.js && node Makefile.dryice.js css --target build-styles && rm -rf styles && mv build-styles/css styles"
79 changes: 75 additions & 4 deletions tool/ace_declaration_generator.js
Original file line number Diff line number Diff line change
@@ -379,9 +379,9 @@ function correctIndividualDeclaration(sourceFile, content, aceNamespacePath) {
let output = content;
// copy the ace.d.ts file to the root of the project with ACE namespace for backwards compatibility
if (sourceFile.fileName.endsWith("/ace.d.ts")) {
const ace = cloneAceNamespace(aceNamespacePath);
const ace = cloneAceNamespace(aceNamespacePath, true);
const mainDeclaration = AUTO_GENERATED_HEADER + ace + `\nexport * from "./src/ace";`;
fs.writeFileSync("../ace.d.ts", mainDeclaration);
fs.writeFileSync(__dirname + "/../ace.d.ts", mainDeclaration);
} else
if (sourceFile.fileName.endsWith("/snippets.d.ts")) {
output = output.replace(/(interface SnippetManager)/, "declare $1");
@@ -651,9 +651,10 @@ function collectStatements(aceNamespacePath) {

/**
* @param {string} aceNamespacePath
* @param {boolean} [withModeReference]
* @return {string}
*/
function cloneAceNamespace(aceNamespacePath) {
function cloneAceNamespace(aceNamespacePath, withModeReference) {
const program = ts.createProgram([aceNamespacePath], {
noEmit: true
});
@@ -666,7 +667,11 @@ function cloneAceNamespace(aceNamespacePath) {
const node = sourceFile.statements[i];
if (ts.isModuleDeclaration(node) && node.name.text == "Ace") {
let aceModule = printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);
aceModule = MODE_REFERENCE + '\n\n' + aceModule + '\n';
if (withModeReference) {
aceModule = MODE_REFERENCE + '\n\n' + aceModule + '\n';
} else {
aceModule = aceModule + '\n';
}
return aceModule;
}
}
@@ -716,6 +721,71 @@ function cleanDeclarationFiles() {
});
}

function bundleDtsFiles(buildPath) {
const aceNamespacePath = __dirname + "/../ace-internal.d.ts";
if (!buildPath) {
buildPath = __dirname + '/../';
}

const directoryPath = path.resolve(buildPath, 'src');
const files = getAllFiles(directoryPath);
const dtsFiles = files.filter(file => file.endsWith('.d.ts'));
dtsFiles.push(path.join(buildPath, '/interfaces.d.ts'));

let combinedDefinitions = "";
dtsFiles.forEach((filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
const moduleParts = filePath.replace(/\\/g,"/").replace(/\.d\.ts/, "").split("src");

let moduleName;
if (moduleParts.length > 1) {
moduleName = "src" + moduleParts[1];
if (moduleName == "src/ace") {
content = cloneAceNamespace(aceNamespacePath) + content;
}

const partOfPath = moduleParts[1].split("/").slice(0, -1).join("/");
content = content.replace(/(['"])((?:\.\.?\/)+)([^'"]+)/g, (match, quote, relPath, importedFile) => {
const upLevels = (relPath.match(/\.\.\//g) || []).length;
importedFile = importedFile.replace("src/", "");

if (moduleName == "src/ext/menu_tools/get_editor_keyboard_shortcuts") {
console.log(moduleName)
}

let dirs = partOfPath ? partOfPath.split('/') : [];

if (upLevels > 0) {
dirs = dirs.slice(0, dirs.length - upLevels);
}

const newPath = dirs.length > 0 ? dirs.join('/') + '/' + importedFile : importedFile;
return `${quote}ace-code/src/${newPath}`.replace(/\/+/g, "/");
});
}
else {
moduleName = 'interfaces';
content = content.replace(/"\.\/src/g, "\"ace-code/src");
}

content = content.replace(/declare\s+/g, "");

combinedDefinitions += `declare module "ace-code/${moduleName}" {\n ${content}\n}\n`;

});

combinedDefinitions = updateDeclarationModuleNames(combinedDefinitions);
const regex = new RegExp(AUTO_GENERATED_HEADER.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
combinedDefinitions = combinedDefinitions.replace(regex, "");
combinedDefinitions = combinedDefinitions.replace(/ace\-builds\-internal\/ace/g, "ace-builds");

combinedDefinitions = formatDts("test.d.ts", combinedDefinitions);

combinedDefinitions = AUTO_GENERATED_HEADER + MODE_REFERENCE + "\n" + combinedDefinitions;

return combinedDefinitions;
}


if (!module.parent) {
require("./modes-declaration-generator");
@@ -724,4 +794,5 @@ if (!module.parent) {
else {
exports.generateDeclaration = generateDeclaration;
exports.updateDeclarationModuleNames = updateDeclarationModuleNames;
exports.bundleDtsFiles = bundleDtsFiles;
}
29 changes: 0 additions & 29 deletions tsconfig-prod.json

This file was deleted.

3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@
"include": [
"./src/**/*.js",
"./interfaces.d.ts",
"./ace-internal.d.ts",
"tool/ace_declaration_generator.js"
"./ace-internal.d.ts"
]
}