microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.04k stars 3.57k forks source link

[Bug] ESM Code contains require code #4398

Open jogibear9988 opened 7 months ago

jogibear9988 commented 7 months ago

Reproducible in vscode.dev or in VS Code Desktop?

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

No response

Monaco Editor Playground Code

No response

Reproduction Steps

Look at the monaco editor npm package

Actual (Problematic) Behavior

The NPM package contains require/amd code in the ESM parts of the package.

For example, look at the file: editor.api.js in the esm/vs/editor folder

    if ((monacoEnvironment === null || monacoEnvironment === void 0 ? void 0 : monacoEnvironment.globalAPI) || (typeof define === 'function' && define.amd)) {
        globalThis.monaco = api;
    }
    if (typeof globalThis.require !== 'undefined' && typeof globalThis.require.config === 'function') {
        globalThis.require.config({
            ignoreDuplicateModules: [
                'vscode-languageserver-types',
                'vscode-languageserver-types/main',
                'vscode-languageserver-textdocument',
                'vscode-languageserver-textdocument/main',
                'vscode-nls',
                'vscode-nls/vscode-nls',
                'jsonc-parser',
                'jsonc-parser/main',
                'vscode-uri',
                'vscode-uri/index',
                'vs/basic-languages/typescript/typescript'
            ]
        });
    }

Expected Behavior

This code should not exist in the esm variant

Additional Context

No response

jogibear9988 commented 7 months ago

would it be okay to add a build step like this to build-monaco-editor.ts:

    /**
     * Removes all code between REMOVE_IN_ESM_START and REMOVE_IN_ESM_END comments
     */
    function ESM_removeRemoveInEsmCode(files: IFile[]) {
        for (const file of files) {
            let contents = file.contents.toString();

            if (!/^\/[\/\*].*REMOVE_IN_ESM_START/m.test(contents)) {
                continue;
            }

            contents = contents.replace(/^\/[\/\*].*REMOVE_IN_ESM_START[\s\S]*?\/[\/\*].*REMOVE_IN_ESM_END.*[\/\*]\/$/m, '')

            file.contents = Buffer.from(contents);
        }
    }

and then add following comments to "editor.api.ts" in vscode:

      /* REMOVE_IN_ESM_START (All code after this until the end marker is removed in ESM Code */
      interface IMonacoEnvironment {
          globalAPI?: boolean;
      }
      const monacoEnvironment: IMonacoEnvironment | undefined = (globalThis as any).MonacoEnvironment;
      if (monacoEnvironment?.globalAPI || (typeof define === 'function' && (<any>define).amd)) {
          globalThis.monaco = api;
      }

      if (typeof globalThis.require !== 'undefined' && typeof globalThis.require.config === 'function') {
          globalThis.require.config({
              ignoreDuplicateModules: [
                  'vscode-languageserver-types',
                  'vscode-languageserver-types/main',
                  'vscode-languageserver-textdocument',
                  'vscode-languageserver-textdocument/main',
                  'vscode-nls',
                  'vscode-nls/vscode-nls',
                  'jsonc-parser',
                  'jsonc-parser/main',
                  'vscode-uri',
                  'vscode-uri/index',
                  'vs/basic-languages/typescript/typescript'
              ]
          });
      }
      /* REMOVE_IN_ESM_END */