Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .brackets.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"path": {
"src/thirdparty/CodeMirror/**/*.js": {
"src/thirdparty/CodeMirror6/**/*.js": {
"spaceUnits": 2,
"linting.enabled": false
},
Expand All @@ -33,4 +33,4 @@
"livePreviewServerURL": "",
"livePreviewServerProjectPath": "/",
"livePreviewHotReloadSupported": false
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Thumbs.db

# ignore MCP server runtime files
/phoenix-builder-mcp/.mcp-server.pid
/phoenix-builder-mcp/.mcp-server-*.pid

# ignore chrome extension build artifacts
/phoenix-builder-mcp/chrome_extension/build/
Expand Down Expand Up @@ -52,7 +53,7 @@ src/phoenix/virtualfs.js.map
!/src/thirdparty/licences
/src/thirdparty/less.*
/src/thirdparty/emmet.*
/src/thirdparty/CodeMirror
/src/thirdparty/CodeMirror6
/src/thirdparty/acorn
/src/thirdparty/tern
/src/thirdparty/mustache
Expand Down
278 changes: 278 additions & 0 deletions build/build-codemirror6.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2026 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*/

import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { babel } from "@rollup/plugin-babel";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { rollup } from "rollup";
import codeMirror5Validation from "./validate-codemirror5.js";

const {
assertNoCodeMirror5Dependencies
} = codeMirror5Validation;

const SCRIPT_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
const REPOSITORY_ROOT = path.resolve(SCRIPT_DIRECTORY, "..");
const ENTRY_FILE = path.join(SCRIPT_DIRECTORY, "codemirror6-entry.js");
const OUTPUT_DIRECTORY = path.join(REPOSITORY_ROOT, "src/thirdparty/CodeMirror6");
const OUTPUT_FILE = path.join(OUTPUT_DIRECTORY, "codemirror6.js");
const LEGACY_OUTPUT_DIRECTORY = path.join(REPOSITORY_ROOT, "src/thirdparty/CodeMirror");
const LICENSE_FILE = path.join(REPOSITORY_ROOT, "src/thirdparty/licences/codemirror6.markdown");
const VIM_CORE_FILE = normalizePath(path.join(
REPOSITORY_ROOT,
"node_modules/@replit/codemirror-vim-core/vim.js"
));
const AMD_MODULE_ID = "thirdparty/CodeMirror6/codemirror6";
const NODE_MODULES_PATH_SEGMENT = "/node_modules/";

const DEDUPED_PACKAGES = [
"@codemirror/autocomplete",
"@codemirror/commands",
"@codemirror/lang-css",
"@codemirror/lang-html",
"@codemirror/lang-javascript",
"@codemirror/lang-json",
"@codemirror/lang-markdown",
"@codemirror/lang-php",
"@codemirror/lang-xml",
"@codemirror/language",
"@codemirror/legacy-modes",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/css",
"@lezer/highlight",
"@lezer/html",
"@lezer/javascript",
"@lezer/json",
"@lezer/lr",
"@lezer/markdown",
"@lezer/php",
"@lezer/xml",
"@marijn/find-cluster-break",
"crelt",
"style-mod",
"w3c-keyname"
];

const SINGLETON_PACKAGES = [
"@codemirror/language",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr"
];

const LICENSE_FILE_NAMES = [
"LICENSE",
"LICENSE.md",
"LICENSE.txt",
"LICENCE",
"LICENCE.md",
"LICENCE.txt"
];

function normalizePath(filePath) {
return filePath.replaceAll("\\", "/");
}

function getPackageDetails(moduleId) {
const normalizedId = normalizePath(moduleId.split("?")[0]);
const nodeModulesIndex = normalizedId.lastIndexOf(NODE_MODULES_PATH_SEGMENT);
if (nodeModulesIndex === -1) {
return null;
}

const packageRelativePath = normalizedId.slice(
nodeModulesIndex + NODE_MODULES_PATH_SEGMENT.length
);
const pathParts = packageRelativePath.split("/");
const packageName = pathParts[0].startsWith("@")
? `${pathParts[0]}/${pathParts[1]}`
: pathParts[0];
const packagePathPartCount = packageName.startsWith("@") ? 2 : 1;
const packageRoot = normalizedId.slice(
0,
nodeModulesIndex + NODE_MODULES_PATH_SEGMENT.length +
pathParts.slice(0, packagePathPartCount).join("/").length
);

return {
name: packageName,
root: packageRoot
};
}

function findLicenseFile(packageRoot) {
for (const fileName of LICENSE_FILE_NAMES) {
const candidate = path.join(packageRoot, fileName);
if (fs.existsSync(candidate)) {
return candidate;
}
}
return null;
}

function writeAggregateLicenseNotice(packageRoots) {
const sections = [
"# CodeMirror 6 bundle licenses",
"",
"This file is generated by `build/build-codemirror6.mjs` from the packages included in",
"`src/thirdparty/CodeMirror6/codemirror6.js`. Each package's license text is reproduced",
"below.",
""
];

for (const packageName of [...packageRoots.keys()].sort()) {
const packageRoot = [...packageRoots.get(packageName)][0];
const packageJSON = JSON.parse(
fs.readFileSync(path.join(packageRoot, "package.json"), "utf8")
);
const licensePath = findLicenseFile(packageRoot);
if (!licensePath) {
throw new Error(`No license file found for bundled package ${packageName}`);
}

sections.push(
`## ${packageName} ${packageJSON.version}`,
"",
fs.readFileSync(licensePath, "utf8").trim(),
""
);
}

fs.writeFileSync(LICENSE_FILE, `${sections.join("\n").trimEnd()}\n`, "utf8");
}

const packageRoots = new Map();

const validateBundlePlugin = {
name: "validate-codemirror6-bundle",

moduleParsed(moduleInfo) {
const packageDetails = getPackageDetails(moduleInfo.id);
if (!packageDetails) {
return;
}
if (packageDetails.name === "codemirror") {
throw new Error(
`CodeMirror 5 package "codemirror" is not allowed in the CodeMirror 6 bundle: ` +
moduleInfo.id
);
}

if (!packageRoots.has(packageDetails.name)) {
packageRoots.set(packageDetails.name, new Set());
}
packageRoots.get(packageDetails.name).add(packageDetails.root);
},

generateBundle(_outputOptions, outputBundle) {
const chunks = Object.values(outputBundle).filter(item => item.type === "chunk");
if (chunks.length !== 1) {
throw new Error(`CodeMirror 6 must build as one chunk, but Rollup emitted ${chunks.length}`);
}

const [chunk] = chunks;
if (chunk.imports.length || chunk.dynamicImports.length) {
throw new Error("CodeMirror 6 bundle contains external or dynamic imports");
}

for (const packageName of SINGLETON_PACKAGES) {
const roots = packageRoots.get(packageName);
if (!roots || roots.size !== 1) {
throw new Error(
`Expected exactly one bundled copy of ${packageName}, found ${roots ? roots.size : 0}`
);
}
}

for (const [packageName, roots] of packageRoots) {
if (roots.size > 1) {
throw new Error(
`Multiple bundled copies of ${packageName} were detected: ${[...roots].join(", ")}`
);
}
}
}
};

async function buildCodeMirror6() {
assertNoCodeMirror5Dependencies({
repositoryRoot: REPOSITORY_ROOT
});
fs.rmSync(LEGACY_OUTPUT_DIRECTORY, { recursive: true, force: true });
fs.rmSync(OUTPUT_DIRECTORY, { recursive: true, force: true });
fs.mkdirSync(OUTPUT_DIRECTORY, { recursive: true });

const bundle = await rollup({
input: ENTRY_FILE,
plugins: [
nodeResolve({
browser: true,
dedupe: DEDUPED_PACKAGES
}),
babel({
babelHelpers: "bundled",
babelrc: false,
configFile: false,
extensions: [".js"],
include: VIM_CORE_FILE,
plugins: ["@babel/plugin-transform-optional-chaining"]
}),
validateBundlePlugin
]
});

try {
await bundle.write({
amd: {
id: AMD_MODULE_ID
},
banner: "/*! DONT_STRIP_MINIFY: Third-party license notices: " +
"thirdparty/licences/codemirror6.markdown. */",
exports: "named",
file: OUTPUT_FILE,
format: "amd",
inlineDynamicImports: true,
sourcemap: true,
validate: true
});
} finally {
await bundle.close();
}

writeAggregateLicenseNotice(packageRoots);

const outputSizeKB = Math.round(fs.statSync(OUTPUT_FILE).size / 1024);
console.log(
`Built ${AMD_MODULE_ID} (${outputSizeKB} KB, ${packageRoots.size} runtime packages)`
);
}

buildCodeMirror6().catch(error => {
console.error(error);
process.exitCode = 1;
});
Loading
Loading