90 lines
2.6 KiB
JavaScript

import { readFileSync } from 'fs';
import { join } from 'path';
import { resolveInnerDep } from '@fesjs/utils';
import { name } from '../package.json';
const namespace = 'plugin-monaco-editor';
export default (api) => {
const {
utils: { Mustache },
} = api;
api.describe({
key: 'monacoEditor',
config: {
schema(joi) {
return joi.object().keys({
filename: joi.string(),
publicPath: joi.string(),
languages: joi.array(),
features: joi.array(),
globalAPI: joi.boolean(),
});
},
},
default: {},
});
const absoluteFilePath = join(namespace, 'core.js');
const absRuntimeFilePath = join(namespace, 'runtime.js');
const absLoaderFilePath = join(namespace, 'loader.js');
const absEditorFilePath = join(namespace, 'editor.vue');
api.onGenerateFiles(() => {
// 文件写出
api.writeTmpFile({
path: absoluteFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/core.tpl'), 'utf-8'), {}),
});
api.writeTmpFile({
path: absRuntimeFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8')),
});
api.writeTmpFile({
path: absLoaderFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/loader.tpl'), 'utf-8'), {
MONACO_EDITOR: resolveInnerDep('monaco-editor', api.builder),
}),
});
api.writeTmpFile({
path: absEditorFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/editor.tpl'), 'utf-8'), {
LODASH_ES: resolveInnerDep('lodash-es', api.builder),
}),
});
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl'],
});
});
api.addPluginExports(() => [
{
specifiers: ['monaco', 'MonacoEditor'],
source: absoluteFilePath,
},
]);
api.addRuntimePluginKey(() => 'monacoEditor');
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
api.chainWebpack((webpackConfig) => {
webpackConfig.plugin('monaco-editor').use(require('monaco-editor-webpack-plugin'), [api.config?.monacoEditor || {}]);
return webpackConfig;
});
api.addConfigType(() => ({
source: name,
build: ['MonacoEditorBuildConfig'],
}));
};