Skip to content

Commit

Permalink
fix: remove console.log (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
NomadBin committed Jul 2, 2022
1 parent a73562c commit 278baaa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
34 changes: 15 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export function resolveMonacoPath(filePath: string): string {
}

export function getWorks(options: IMonacoEditorOpts) {
let works: IWorkerDefinition[] = options.languageWorkers.map((work) => languageWorksByLabel[work]);
let works: IWorkerDefinition[] = options.languageWorkers.map(
(work) => languageWorksByLabel[work]
);

works.push(...options.customWorkers);

return works;

}

export interface IMonacoEditorOpts {
Expand Down Expand Up @@ -59,12 +60,11 @@ export default function monacoEditorPlugin(options: IMonacoEditorOpts = {}): Plu
const globalAPI = options.globalAPI || false;
const customWorkers = options.customWorkers || [];


options = {
languageWorkers,
publicPath,
globalAPI,
customWorkers
customWorkers,
};

let resolvedConfig: ResolvedConfig;
Expand All @@ -79,7 +79,6 @@ export default function monacoEditorPlugin(options: IMonacoEditorOpts = {}): Plu
return;
}


workerMiddleware(server.middlewares, resolvedConfig, options);
},
transformIndexHtml(html) {
Expand Down Expand Up @@ -126,16 +125,18 @@ export default function monacoEditorPlugin(options: IMonacoEditorOpts = {}): Plu

const works = getWorks(options);

const distPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, options.publicPath);
const distPath = path.join(
resolvedConfig.root,
resolvedConfig.build.outDir,
resolvedConfig.base,
options.publicPath
);

// write publicPath
if (!fs.existsSync(distPath)) {
fs.mkdirSync(
path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, options.publicPath),
{
recursive: true
}
);
fs.mkdirSync(distPath, {
recursive: true,
});
}

for (const work of works) {
Expand All @@ -147,13 +148,8 @@ export default function monacoEditorPlugin(options: IMonacoEditorOpts = {}): Plu
});
}
const contentBuffer = fs.readFileSync(cacheDir + getFilenameByEntry(work.entry));
const destPath = path.resolve(
resolvedConfig.root,
resolvedConfig.build.outDir,
options.publicPath,
getFilenameByEntry(work.entry)
);
fs.writeFileSync(destPath, contentBuffer);
const workDistPath = path.resolve(distPath, getFilenameByEntry(work.entry));
fs.writeFileSync(workDistPath, contentBuffer);
}
},
};
Expand Down
12 changes: 8 additions & 4 deletions src/workerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ import * as fs from 'fs';
import path = require('path');

export function getFilenameByEntry(entry: string) {
entry = path.basename(entry, "js");
entry = path.basename(entry, 'js');
return entry + '.bundle.js';
}

export const cacheDir = 'node_modules/.monaco/';

export function getWorkPath(works: IWorkerDefinition[], options: IMonacoEditorOpts, config: ResolvedConfig) {
export function getWorkPath(
works: IWorkerDefinition[],
options: IMonacoEditorOpts,
config: ResolvedConfig
) {
const workerPaths = {};

for (const work of works) {
if (isCDN(options.publicPath)) {
workerPaths[work.label] = options.publicPath + '/' + getFilenameByEntry(work.entry);
} else {
workerPaths[work.label] = config.base + options.publicPath + '/' + getFilenameByEntry(work.entry);
workerPaths[work.label] =
config.base + options.publicPath + '/' + getFilenameByEntry(work.entry);
}
}

Expand Down Expand Up @@ -54,7 +59,6 @@ export function workerMiddleware(
}

for (const work of works) {
console.log(config.base + options.publicPath + '/' + getFilenameByEntry(work.entry))
middlewares.use(
config.base + options.publicPath + '/' + getFilenameByEntry(work.entry),
function (req, res, next) {
Expand Down
1 change: 1 addition & 0 deletions test/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router';

const routes = [
{ path: '/', component: () => import('../mona/Editor.vue') },
{ path: '/sub', component: () => import('../mona/Editor.vue') },
{ path: '/home/test', component: () => import('../mona/Editor.vue') },
];

Expand Down
1 change: 1 addition & 0 deletions test/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineConfig({
vue(),
monacoEditorPlugin({
publicPath: 'a/monacoeditorwork',
// publicPath: 'https://unpkg.com/[email protected]/cdn',
customWorkers: [
{
label: "graphql",
Expand Down

0 comments on commit 278baaa

Please sign in to comment.