This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
index.js
207 lines (183 loc) · 6.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const path = require('path');
const webpack = require('webpack');
const AddWorkerEntryPointPlugin = require('./plugins/AddWorkerEntryPointPlugin');
const INCLUDE_LOADER_PATH = require.resolve('./loaders/include');
const EDITOR_MODULE = {
label: 'editorWorkerService',
entry: undefined,
worker: {
id: 'vs/editor/editor',
entry: 'vs/editor/editor.worker',
output: 'editor.worker.js',
fallback: undefined
},
alias: undefined,
};
const LANGUAGES = require('./languages');
const FEATURES = require('./features');
function resolveMonacoPath(filePath) {
return require.resolve(path.join('monaco-editor/esm', filePath));
}
const languagesById = fromPairs(
flatMap(toPairs(LANGUAGES), ([id, language]) =>
[id].concat(language.alias || []).map((label) => [label, mixin({ label }, language)])
)
);
const featuresById = mapValues(FEATURES, (feature, key) => mixin({ label: key }, feature))
function getFeaturesIds(userFeatures, predefinedFeaturesById) {
function notContainedIn(arr) {
return (element) => arr.indexOf(element) === -1;
}
let featuresIds;
if (userFeatures.length) {
const excludedFeatures = userFeatures.filter(f => f[0] === '!').map(f => f.slice(1));
if (excludedFeatures.length) {
featuresIds = Object.keys(predefinedFeaturesById).filter(notContainedIn(excludedFeatures))
} else {
featuresIds = userFeatures;
}
} else {
featuresIds = Object.keys(predefinedFeaturesById);
}
return featuresIds;
}
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,
};
}
apply(compiler) {
const { languages, features, output } = 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);
addCompilerRules(compiler, rules);
addCompilerPlugins(compiler, plugins);
}
}
function addCompilerRules(compiler, rules) {
const compilerOptions = compiler.options;
const moduleOptions = compilerOptions.module || (compilerOptions.module = {});
moduleOptions.rules = (moduleOptions.rules || []).concat(rules);
}
function addCompilerPlugins(compiler, plugins) {
plugins.forEach((plugin) => plugin.apply(compiler));
}
function getPublicPath(compiler) {
return compiler.options.output && compiler.options.output.publicPath || '';
}
function createLoaderRules(languages, features, workers, outputPath, publicPath) {
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)]));
if (workerPaths['typescript']) {
// javascript shares the same worker
workerPaths['javascript'] = workerPaths['typescript'];
}
if (workerPaths['css']) {
// scss and less share the same worker
workerPaths['less'] = workerPaths['css'];
workerPaths['scss'] = workerPaths['css'];
}
if (workerPaths['html']) {
// handlebars, razor and html share the same worker
workerPaths['handlebars'] = workerPaths['html'];
workerPaths['razor'] = workerPaths['html'];
}
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)});
return (pathPrefix ? stripTrailingSlash(pathPrefix) + '/' : '') + paths[label];
}
};
})(${JSON.stringify(workerPaths, null, 2)})`,
};
return [
{
test: /monaco-editor[/\\]esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/,
use: [
{
loader: INCLUDE_LOADER_PATH,
options: {
globals,
pre: featurePaths.map((importPath) => resolveMonacoPath(importPath)),
post: languagePaths.map((importPath) => resolveMonacoPath(importPath)),
},
},
],
},
];
}
function createPlugins(workers, outputPath) {
return (
[]
.concat(uniqBy(workers, ({ id }) => id).map(({ id, entry, output }) =>
new AddWorkerEntryPointPlugin({
id,
entry: resolveMonacoPath(entry),
filename: path.join(outputPath, output),
plugins: [
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
],
})
))
);
}
function flatMap(items, iteratee) {
return items.map(iteratee).reduce((acc, item) => [].concat(acc).concat(item), []);
}
function flatArr(items) {
return items.reduce((acc, item) => {
if (Array.isArray(item)) {
return [].concat(acc).concat(item);
}
return [].concat(acc).concat([item]);
}, []);
}
function toPairs(object) {
return Object.keys(object).map((key) => [key, object[key]]);
}
function fromPairs(values) {
return values.reduce((acc, [key, value]) => Object.assign(acc, { [key]: value }), {});
}
function mapValues(object, iteratee) {
return Object.keys(object).reduce(
(acc, key) => Object.assign(acc, { [key]: iteratee(object[key], key) }),
{}
);
}
function uniqBy(items, iteratee) {
const keys = {};
return items.reduce((acc, item) => {
const key = iteratee(item);
if (key in keys) { return acc; }
keys[key] = true;
acc.push(item);
return acc;
}, []);
}
function mixin(dest, src) {
for (let prop in src) {
if (Object.hasOwnProperty.call(src, prop)) {
dest[prop] = src[prop];
}
}
return dest;
}
module.exports = MonacoWebpackPlugin;