mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 19:42:56 +01:00
add support for dynamic filenames
Add support for custom filename template for worker scripts.
This commit is contained in:
parent
1d1473de8e
commit
3bab84bf28
4 changed files with 33 additions and 24 deletions
35
index.js
35
index.js
|
|
@ -1,5 +1,8 @@
|
|||
const path = require('path');
|
||||
const webpack = require('webpack');
|
||||
const loaderUtils = require('loader-utils');
|
||||
const fs = require('fs');
|
||||
|
||||
const AddWorkerEntryPointPlugin = require('./plugins/AddWorkerEntryPointPlugin');
|
||||
const INCLUDE_LOADER_PATH = require.resolve('./loaders/include');
|
||||
|
||||
|
|
@ -9,7 +12,6 @@ const EDITOR_MODULE = {
|
|||
worker: {
|
||||
id: 'vs/editor/editor',
|
||||
entry: 'vs/editor/editor.worker',
|
||||
output: 'editor.worker.js',
|
||||
fallback: undefined
|
||||
},
|
||||
alias: undefined,
|
||||
|
|
@ -17,10 +19,22 @@ const EDITOR_MODULE = {
|
|||
const LANGUAGES = require('./languages');
|
||||
const FEATURES = require('./features');
|
||||
|
||||
/**
|
||||
* Return a resolved path for a given Monaco file.
|
||||
*/
|
||||
function resolveMonacoPath(filePath) {
|
||||
return require.resolve(path.join('monaco-editor/esm', filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the interpolated final filename for a worker, respecting the file name template.
|
||||
*/
|
||||
function getWorkerFilename(filename, entry) {
|
||||
return loaderUtils.interpolateName({resourcePath: entry}, filename, {
|
||||
content: fs.readFileSync(resolveMonacoPath(entry))
|
||||
});
|
||||
}
|
||||
|
||||
const languagesById = fromPairs(
|
||||
flatMap(toPairs(LANGUAGES), ([id, language]) =>
|
||||
[id].concat(language.alias || []).map((label) => [label, mixin({ label }, language)])
|
||||
|
|
@ -53,23 +67,22 @@ class MonacoWebpackPlugin {
|
|||
constructor(options = {}) {
|
||||
const languages = options.languages || Object.keys(languagesById);
|
||||
const features = getFeaturesIds(options.features || [], featuresById);
|
||||
const output = options.output || '';
|
||||
this.options = {
|
||||
languages: languages.map((id) => languagesById[id]).filter(Boolean),
|
||||
features: features.map(id => featuresById[id]).filter(Boolean),
|
||||
output,
|
||||
filename: options.filename || "[name].worker.js"
|
||||
};
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
const { languages, features, output } = this.options;
|
||||
const { languages, features, filename } = this.options;
|
||||
const publicPath = getPublicPath(compiler);
|
||||
const modules = [EDITOR_MODULE].concat(languages).concat(features);
|
||||
const workers = modules.map(
|
||||
({ label, alias, worker }) => worker && (mixin({ label, alias }, worker))
|
||||
).filter(Boolean);
|
||||
const rules = createLoaderRules(languages, features, workers, output, publicPath);
|
||||
const plugins = createPlugins(workers, output);
|
||||
const rules = createLoaderRules(languages, features, workers, publicPath, filename);
|
||||
const plugins = createPlugins(workers, filename);
|
||||
addCompilerRules(compiler, rules);
|
||||
addCompilerPlugins(compiler, plugins);
|
||||
}
|
||||
|
|
@ -89,11 +102,11 @@ function getPublicPath(compiler) {
|
|||
return compiler.options.output && compiler.options.output.publicPath || '';
|
||||
}
|
||||
|
||||
function createLoaderRules(languages, features, workers, outputPath, publicPath) {
|
||||
function createLoaderRules(languages, features, workers, publicPath, filename) {
|
||||
if (!languages.length && !features.length) { return []; }
|
||||
const languagePaths = flatArr(languages.map(({ entry }) => entry).filter(Boolean));
|
||||
const featurePaths = flatArr(features.map(({ entry }) => entry).filter(Boolean));
|
||||
const workerPaths = fromPairs(workers.map(({ label, output }) => [label, path.join(outputPath, output)]));
|
||||
const workerPaths = fromPairs(workers.map(({ label, entry }) => [label, getWorkerFilename(filename, entry)]));
|
||||
if (workerPaths['typescript']) {
|
||||
// javascript shares the same worker
|
||||
workerPaths['javascript'] = workerPaths['typescript'];
|
||||
|
|
@ -140,14 +153,14 @@ function createLoaderRules(languages, features, workers, outputPath, publicPath)
|
|||
];
|
||||
}
|
||||
|
||||
function createPlugins(workers, outputPath) {
|
||||
function createPlugins(workers, filename) {
|
||||
return (
|
||||
[]
|
||||
.concat(uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) =>
|
||||
.concat(uniqBy(workers, ({ id }) => id).map(({ id, entry }) =>
|
||||
new AddWorkerEntryPointPlugin({
|
||||
id,
|
||||
entry: resolveMonacoPath(entry),
|
||||
filename: path.join(outputPath, output),
|
||||
filename: getWorkerFilename(filename, entry),
|
||||
plugins: [
|
||||
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue