Skip to content

Commit

Permalink
fix: Replace esm comments before executing vscode code
Browse files Browse the repository at this point in the history
  • Loading branch information
CGNonofr committed Jul 5, 2022
1 parent 3a69275 commit 81da493
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function toggleEsmComments (fileContents: string): string {
}

const cache = new Map<string, Record<string, unknown>>()
function customRequire<T extends Record<string, unknown>> (_path: string, rootPaths: string[] = [], fromPath?: string): T | null {
function customRequire<T extends Record<string, unknown>> (_path: string, rootPaths: string[] = [], fromPath?: string, transform?: (code: string) => string): T | null {
const resolvedPath = resolve(_path, fromPath != null ? [...rootPaths, fromPath] : rootPaths)
if (resolvedPath == null) {
return null
Expand All @@ -314,7 +314,10 @@ function customRequire<T extends Record<string, unknown>> (_path: string, rootPa
return cache.get(resolvedPath) as T
}

const code = fs.readFileSync(resolvedPath).toString()
let code = fs.readFileSync(resolvedPath).toString()
if (transform != null) {
code = transform(code)
}

const transformedCode = babel.transform(code.replace(/@\w+/g, '') /* Remove annotations */, {
filename: resolvedPath,
Expand All @@ -340,7 +343,7 @@ function customRequire<T extends Record<string, unknown>> (_path: string, rootPa
if (_path.endsWith('.css') || _path.includes('!')) {
return null
}
const result = customRequire(_path, rootPaths, path.dirname(resolvedPath))
const result = customRequire(_path, rootPaths, path.dirname(resolvedPath), transform)
if (result == null) {
throw new Error('Module not found: ' + _path + ' from ' + resolvedPath)
}
Expand Down Expand Up @@ -395,7 +398,7 @@ function importMonaco (importee: string) {
const monacoPath = path.resolve(MONACO_EDITOR_DIR, 'esm', importee)
const vscodePath = path.resolve(VSCODE_DIR, importee)

const vscodeExports = customRequire(vscodePath, [VSCODE_DIR])!
const vscodeExports = customRequire(vscodePath, [VSCODE_DIR], undefined, toggleEsmComments)!

const monacoExports = customRequire(monacoPath, [path.resolve(MONACO_EDITOR_DIR, 'esm')])!
for (const monacoExport in monacoExports) {
Expand Down

0 comments on commit 81da493

Please sign in to comment.