Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Add support for plugin-specific public path #81

Merged
merged 3 commits into from Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -48,9 +48,11 @@ Options can be passed in to `MonacoWebpackPlugin`. They can be used to generate

* `output` (`string`) - custom output path for worker scripts, relative to the main webpack `output.path`.
* default value: `''`.
* `publicPath` (`string`) - custom public path for worker scripts, overrides the public path from which files generated by this plugin will be served. This wins out over Webpack's dynamic runtime path and can be useful to avoid attempting to load workers cross-origin when using a CDN for other static resources. Use e.g. '/' if you want to load your resources from the current origin..
* default value: `''`.
* `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']`.

Some languages share the same web worker. If one of the following languages is included, you must also include the language responsible for instantiating their shared worker:

| Language | Instantiator |
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Expand Up @@ -17,6 +17,14 @@ interface IMonacoEditorWebpackPluginOpts {
* Use e.g. '!contextmenu' to exclude a certain feature.
*/
features?: string[];

/**
* Override the public path from which files generated by this plugin will be served.
* This wins out over Webpack's dynamic runtime path and can be useful to avoid attempting to load workers cross-
* origin when using a CDN for other static resources.
* Use e.g. '/' if you want to load your resources from the current origin.
*/
publicPath?: string;
}

declare class MonacoEditorWebpackPlugin extends Plugin {
Expand Down
26 changes: 18 additions & 8 deletions index.js
Expand Up @@ -53,22 +53,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,
output: options.output || '',
publicPath: options.publicPath || ''
};
}

apply(compiler) {
const { languages, features, output } = this.options;
const publicPath = getPublicPath(compiler);
const { languages, features, output, publicPath } = this.options;
const compilationPublicPath = getCompilationPublicPath(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 rules = createLoaderRules(languages, features, workers, output, publicPath, compilationPublicPath);
const plugins = createPlugins(workers, output);
addCompilerRules(compiler, rules);
addCompilerPlugins(compiler, plugins);
Expand All @@ -85,11 +85,11 @@ function addCompilerPlugins(compiler, plugins) {
plugins.forEach((plugin) => plugin.apply(compiler));
}

function getPublicPath(compiler) {
function getCompilationPublicPath(compiler) {
return compiler.options.output && compiler.options.output.publicPath || '';
}

function createLoaderRules(languages, features, workers, outputPath, publicPath) {
function createLoaderRules(languages, features, workers, outputPath, pluginPublicPath, compilationPublicPath) {
if (!languages.length && !features.length) { return []; }
const languagePaths = flatArr(languages.map(({ entry }) => entry).filter(Boolean));
const featurePaths = flatArr(features.map(({ entry }) => entry).filter(Boolean));
Expand All @@ -110,14 +110,24 @@ function createLoaderRules(languages, features, workers, outputPath, publicPath)
workerPaths['razor'] = workerPaths['html'];
}

// Determine the public path from which to load worker JS files. In order of precedence:
// 1. Plugin-specific public path.
// 2. Dynamic runtime public path.
// 3. Compilation public path.
const pathPrefix = Boolean(pluginPublicPath)
? JSON.stringify(pluginPublicPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugin public path wins out over dynamic public path if specified.

: `typeof __webpack_public_path__ === 'string' ` +
`? __webpack_public_path__ ` +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including the fix from #63 here since I'm touching the same code – this should be referenced as a free variable and not off of window as documented here.

`: ${JSON.stringify(compilationPublicPath)}`

const globals = {
'MonacoEnvironment': `(function (paths) {
function stripTrailingSlash(str) {
return str.replace(/\\/$/, '');
}
return {
getWorkerUrl: function (moduleId, label) {
var pathPrefix = (typeof window.__webpack_public_path__ === 'string' ? window.__webpack_public_path__ : ${JSON.stringify(publicPath)});
var pathPrefix = ${pathPrefix};
return (pathPrefix ? stripTrailingSlash(pathPrefix) + '/' : '') + paths[label];
}
};
Expand Down