Ben*_*eek 5 optimization webpack monaco-editor vue-cli-3
当我尝试在 vue-cli-3 项目中包含优化选项(该项目在某些页面中使用 monaco-editor)时,我在控制台中收到以下错误:
languageFeatures.js?ff36:85 Error: Unexpected usage
at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js?ccf6:540)
at eval (webWorker.js?af50:54)
Run Code Online (Sandbox Code Playgroud)
这是我的 vue.config.js 文件:
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const PurgecssPlugin = require("purgecss-webpack-plugin");
module.exports = {
transpileDependencies: ["vuex-persist", "vuex-persistedstate"],
configureWebpack: {
devtool: false,
optimization: {
splitChunks: {
minSize: 10000,
maxSize: 250000,
},
nodeEnv: "production",
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: 'false',
parallel: true,
}),
],
removeEmptyChunks: true,
removeAvailableModules: true,
mergeDuplicateChunks: true
},
plugins: [
new MonacoWebpackPlugin({
languages: ['javascript', 'css', 'html', 'typescript', 'json'],
features: ['!gotoSymbol'],
}),
new PurgecssPlugin({paths: glob.sync(`${PATHS.src}/**/*`, {nodir: true})}),
],
}
};
Run Code Online (Sandbox Code Playgroud)
那么问题来了,如何避免出现这个错误呢?
小智 3
在尝试导入editor.main.js之前,应定义 Monaco 环境的基本 URL,并导入主工作线程。您可以尝试通过在尝试加载编辑器之前执行以下代码来解决您的问题。
(window as any).MonacoEnvironment = {
getWorkerUrl: function (workerId, label) {
return `data:text/javascript;charset=utf-8,${encodeURIComponent(`
self.MonacoEnvironment = { baseUrl: '${window.location.origin}/' };
importScripts('${window.location.origin}/vs/base/worker/workerMain.js');
`)}`;
}
};
Run Code Online (Sandbox Code Playgroud)
还需要注意的是,您应该将 URL 更改为托管 vs 文件的位置。
| 归档时间: |
|
| 查看次数: |
6859 次 |
| 最近记录: |