add support for dynamic filenames

Add support for custom filename template for worker scripts.
pull/2748/head
James Diefenderfer 5 years ago
parent 1d1473de8e
commit 3bab84bf28
No known key found for this signature in database
GPG Key ID: EDE86934D2A0665C

@ -46,8 +46,8 @@ monaco.editor.create(document.getElementById('container'), {
Options can be passed in to `MonacoWebpackPlugin`. They can be used to generate a smaller editor bundle by selecting only certain languages or only certain editor features: Options can be passed in to `MonacoWebpackPlugin`. They can be used to generate a smaller editor bundle by selecting only certain languages or only certain editor features:
* `output` (`string`) - custom output path for worker scripts, relative to the main webpack `output.path`. * `filename` (`string`) - custom filename template for worker scripts, respects the same options as [loader-utils' interpolateName](https://github.com/webpack/loader-utils#interpolatename). Useful for adding content-based hashes so that files can be served with long-lived caching headers.
* default value: `''`. * default value: `'[name].worker.js'`.
* `languages` (`string[]`) - include only a subset of the languages supported. * `languages` (`string[]`) - include only a subset of the languages supported.
* default value: `['apex', 'azcli', 'bat', 'clojure', 'coffee', 'cpp', 'csharp', 'csp', 'css', 'dockerfile', 'fsharp', 'go', 'handlebars', 'html', 'ini', 'java', 'javascript', 'json', 'less', 'lua', 'markdown', 'msdax', 'mysql', 'objective', 'perl', 'pgsql', 'php', 'postiats', 'powerquery', 'powershell', 'pug', 'python', 'r', 'razor', 'redis', 'redshift', 'ruby', 'rust', 'sb', 'scheme', 'scss', 'shell', 'solidity', 'sql', 'st', 'swift', 'typescript', 'vb', 'xml', 'yaml']`. * default value: `['apex', 'azcli', 'bat', 'clojure', 'coffee', 'cpp', 'csharp', 'csp', 'css', 'dockerfile', 'fsharp', 'go', 'handlebars', 'html', 'ini', 'java', 'javascript', 'json', 'less', 'lua', 'markdown', 'msdax', 'mysql', 'objective', 'perl', 'pgsql', 'php', 'postiats', 'powerquery', 'powershell', 'pug', 'python', 'r', 'razor', 'redis', 'redshift', 'ruby', 'rust', 'sb', 'scheme', 'scss', 'shell', 'solidity', 'sql', 'st', 'swift', 'typescript', 'vb', 'xml', 'yaml']`.

12
index.d.ts vendored

@ -1,12 +1,6 @@
import { Plugin } from 'webpack' import { Plugin } from 'webpack'
interface IMonacoEditorWebpackPluginOpts { interface IMonacoEditorWebpackPluginOpts {
/**
* custom output path for worker scripts, relative to the main webpack `output.path`.
* Defaults to ''.
*/
output?: string;
/** /**
* Include only a subset of the languages supported. * Include only a subset of the languages supported.
*/ */
@ -17,6 +11,12 @@ interface IMonacoEditorWebpackPluginOpts {
* Use e.g. '!contextmenu' to exclude a certain feature. * Use e.g. '!contextmenu' to exclude a certain feature.
*/ */
features?: string[]; features?: string[];
/**
* Specify a filename template to use for generated files.
* Use e.g. '[name].worker.[contenthash].js' to include content-based hashes.
*/
filename?: string;
} }
declare class MonacoEditorWebpackPlugin extends Plugin { declare class MonacoEditorWebpackPlugin extends Plugin {

@ -1,5 +1,8 @@
const path = require('path'); const path = require('path');
const webpack = require('webpack'); const webpack = require('webpack');
const loaderUtils = require('loader-utils');
const fs = require('fs');
const AddWorkerEntryPointPlugin = require('./plugins/AddWorkerEntryPointPlugin'); const AddWorkerEntryPointPlugin = require('./plugins/AddWorkerEntryPointPlugin');
const INCLUDE_LOADER_PATH = require.resolve('./loaders/include'); const INCLUDE_LOADER_PATH = require.resolve('./loaders/include');
@ -9,7 +12,6 @@ const EDITOR_MODULE = {
worker: { worker: {
id: 'vs/editor/editor', id: 'vs/editor/editor',
entry: 'vs/editor/editor.worker', entry: 'vs/editor/editor.worker',
output: 'editor.worker.js',
fallback: undefined fallback: undefined
}, },
alias: undefined, alias: undefined,
@ -17,10 +19,22 @@ const EDITOR_MODULE = {
const LANGUAGES = require('./languages'); const LANGUAGES = require('./languages');
const FEATURES = require('./features'); const FEATURES = require('./features');
/**
* Return a resolved path for a given Monaco file.
*/
function resolveMonacoPath(filePath) { function resolveMonacoPath(filePath) {
return require.resolve(path.join('monaco-editor/esm', 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( const languagesById = fromPairs(
flatMap(toPairs(LANGUAGES), ([id, language]) => flatMap(toPairs(LANGUAGES), ([id, language]) =>
[id].concat(language.alias || []).map((label) => [label, mixin({ label }, language)]) [id].concat(language.alias || []).map((label) => [label, mixin({ label }, language)])
@ -53,23 +67,22 @@ class MonacoWebpackPlugin {
constructor(options = {}) { constructor(options = {}) {
const languages = options.languages || Object.keys(languagesById); const languages = options.languages || Object.keys(languagesById);
const features = getFeaturesIds(options.features || [], featuresById); const features = getFeaturesIds(options.features || [], featuresById);
const output = options.output || '';
this.options = { this.options = {
languages: languages.map((id) => languagesById[id]).filter(Boolean), languages: languages.map((id) => languagesById[id]).filter(Boolean),
features: features.map(id => featuresById[id]).filter(Boolean), features: features.map(id => featuresById[id]).filter(Boolean),
output, filename: options.filename || "[name].worker.js"
}; };
} }
apply(compiler) { apply(compiler) {
const { languages, features, output } = this.options; const { languages, features, filename } = this.options;
const publicPath = getPublicPath(compiler); const publicPath = getPublicPath(compiler);
const modules = [EDITOR_MODULE].concat(languages).concat(features); const modules = [EDITOR_MODULE].concat(languages).concat(features);
const workers = modules.map( const workers = modules.map(
({ label, alias, worker }) => worker && (mixin({ label, alias }, worker)) ({ label, alias, worker }) => worker && (mixin({ label, alias }, worker))
).filter(Boolean); ).filter(Boolean);
const rules = createLoaderRules(languages, features, workers, output, publicPath); const rules = createLoaderRules(languages, features, workers, publicPath, filename);
const plugins = createPlugins(workers, output); const plugins = createPlugins(workers, filename);
addCompilerRules(compiler, rules); addCompilerRules(compiler, rules);
addCompilerPlugins(compiler, plugins); addCompilerPlugins(compiler, plugins);
} }
@ -89,11 +102,11 @@ function getPublicPath(compiler) {
return compiler.options.output && compiler.options.output.publicPath || ''; 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 []; } if (!languages.length && !features.length) { return []; }
const languagePaths = flatArr(languages.map(({ entry }) => entry).filter(Boolean)); const languagePaths = flatArr(languages.map(({ entry }) => entry).filter(Boolean));
const featurePaths = flatArr(features.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']) { if (workerPaths['typescript']) {
// javascript shares the same worker // javascript shares the same worker
workerPaths['javascript'] = workerPaths['typescript']; workerPaths['javascript'] = workerPaths['typescript'];
@ -140,14 +153,14 @@ function createLoaderRules(languages, features, workers, outputPath, publicPath)
]; ];
} }
function createPlugins(workers, outputPath) { function createPlugins(workers, filename) {
return ( return (
[] []
.concat(uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) => .concat(uniqBy(workers, ({ id }) => id).map(({ id, entry }) =>
new AddWorkerEntryPointPlugin({ new AddWorkerEntryPointPlugin({
id, id,
entry: resolveMonacoPath(entry), entry: resolveMonacoPath(entry),
filename: path.join(outputPath, output), filename: getWorkerFilename(filename, entry),
plugins: [ plugins: [
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }), new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
], ],

@ -47,7 +47,6 @@ module.exports = {
worker: { worker: {
id: 'vs/language/css/cssWorker', id: 'vs/language/css/cssWorker',
entry: 'vs/language/css/css.worker', entry: 'vs/language/css/css.worker',
output: 'css.worker.js',
fallback: 'vs/language/css/cssWorker', fallback: 'vs/language/css/cssWorker',
}, },
alias: undefined, alias: undefined,
@ -80,7 +79,6 @@ module.exports = {
worker: { worker: {
id: 'vs/language/html/htmlWorker', id: 'vs/language/html/htmlWorker',
entry: 'vs/language/html/html.worker', entry: 'vs/language/html/html.worker',
output: 'html.worker.js',
fallback: 'vs/language/html/htmlWorker', fallback: 'vs/language/html/htmlWorker',
}, },
alias: undefined, alias: undefined,
@ -105,7 +103,6 @@ module.exports = {
worker: { worker: {
id: 'vs/language/json/jsonWorker', id: 'vs/language/json/jsonWorker',
entry: 'vs/language/json/json.worker', entry: 'vs/language/json/json.worker',
output: 'json.worker.js',
fallback: 'vs/language/json/jsonWorker', fallback: 'vs/language/json/jsonWorker',
}, },
alias: undefined, alias: undefined,
@ -258,7 +255,6 @@ module.exports = {
worker: { worker: {
id: 'vs/language/typescript/tsWorker', id: 'vs/language/typescript/tsWorker',
entry: 'vs/language/typescript/ts.worker', entry: 'vs/language/typescript/ts.worker',
output: 'typescript.worker.js',
fallback: 'vs/language/typescript/tsWorker', fallback: 'vs/language/typescript/tsWorker',
}, },
alias: undefined, alias: undefined,

Loading…
Cancel
Save