Have the webpack plugin tests use the /release/ folder

This commit is contained in:
Alex Dima 2021-11-11 19:27:59 +01:00
parent 8af6bd5f8a
commit 8430c538b4
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
5 changed files with 54 additions and 28 deletions

View file

@ -785,12 +785,6 @@
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
}, },
"monaco-editor": {
"version": "0.30.0",
"resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.30.0.tgz",
"integrity": "sha512-/k++/ofRmwnwWTpOWYOMGVcqBrqrlt3MP0Mt/cRTQojW7A9fnekcvPQ2iIFA0YSZdPWPN9yYXrYq0xqiUuxT/A==",
"dev": true
},
"neo-async": { "neo-async": {
"version": "2.6.2", "version": "2.6.2",
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz", "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",

View file

@ -35,7 +35,6 @@
"css-loader": "^5.1.1", "css-loader": "^5.1.1",
"file-loader": "^6.2.0", "file-loader": "^6.2.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"monaco-editor": "^0.30.0",
"style-loader": "^2.0.0", "style-loader": "^2.0.0",
"typescript": "^4.2.3", "typescript": "^4.2.3",
"webpack": "^5.24.3", "webpack": "^5.24.3",

View file

@ -27,24 +27,32 @@ featuresArr.forEach((feature) => (featuresById[feature.label] = feature));
/** /**
* Return a resolved path for a given Monaco file. * Return a resolved path for a given Monaco file.
*/ */
function resolveMonacoPath(filePath: string): string { function resolveMonacoPath(filePath: string, monacoEditorPath: string | undefined): string {
if (monacoEditorPath) {
return require.resolve(path.join(monacoEditorPath, 'esm', filePath));
}
try { try {
return require.resolve(path.join('monaco-editor/esm', filePath)); return require.resolve(path.join('monaco-editor/esm', filePath));
} catch (err) { } catch (err) {}
try { try {
return require.resolve(path.join(process.cwd(), 'node_modules/monaco-editor/esm', filePath)); return require.resolve(path.join(process.cwd(), 'node_modules/monaco-editor/esm', filePath));
} catch (err) { } catch (err) {}
return require.resolve(filePath); return require.resolve(filePath);
} }
}
}
/** /**
* Return the interpolated final filename for a worker, respecting the file name template. * Return the interpolated final filename for a worker, respecting the file name template.
*/ */
function getWorkerFilename(filename: string, entry: string): string { function getWorkerFilename(
filename: string,
entry: string,
monacoEditorPath: string | undefined
): string {
return loaderUtils.interpolateName(<any>{ resourcePath: entry }, filename, { return loaderUtils.interpolateName(<any>{ resourcePath: entry }, filename, {
content: fs.readFileSync(resolveMonacoPath(entry)) content: fs.readFileSync(resolveMonacoPath(entry, monacoEditorPath))
}); });
} }
@ -92,6 +100,12 @@ interface IMonacoEditorWebpackPluginOpts {
*/ */
filename?: string; filename?: string;
/**
* The absolute file system path to the monaco-editor npm module.
* Use e.g. `C:\projects\my-project\node-modules\monaco-editor`
*/
monacoEditorPath?: string;
/** /**
* Override the public path from which files generated by this plugin will be served. * 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- * This wins out over Webpack's dynamic runtime path and can be useful to avoid attempting to load workers cross-
@ -114,6 +128,7 @@ interface IInternalMonacoEditorWebpackPluginOpts {
languages: IFeatureDefinition[]; languages: IFeatureDefinition[];
features: IFeatureDefinition[]; features: IFeatureDefinition[];
filename: string; filename: string;
monacoEditorPath: string | undefined;
publicPath: string; publicPath: string;
globalAPI: boolean; globalAPI: boolean;
} }
@ -129,13 +144,14 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
languages: coalesce(languages.map((id) => languagesById[id])).concat(customLanguages), languages: coalesce(languages.map((id) => languagesById[id])).concat(customLanguages),
features: coalesce(features.map((id) => featuresById[id])), features: coalesce(features.map((id) => featuresById[id])),
filename: options.filename || '[name].worker.js', filename: options.filename || '[name].worker.js',
monacoEditorPath: options.monacoEditorPath,
publicPath: options.publicPath || '', publicPath: options.publicPath || '',
globalAPI: options.globalAPI || false globalAPI: options.globalAPI || false
}; };
} }
apply(compiler: webpack.Compiler): void { apply(compiler: webpack.Compiler): void {
const { languages, features, filename, publicPath, globalAPI } = this.options; const { languages, features, filename, monacoEditorPath, publicPath, globalAPI } = this.options;
const compilationPublicPath = getCompilationPublicPath(compiler); const compilationPublicPath = getCompilationPublicPath(compiler);
const modules = [EDITOR_MODULE].concat(languages).concat(features); const modules = [EDITOR_MODULE].concat(languages).concat(features);
const workers: ILabeledWorkerDefinition[] = []; const workers: ILabeledWorkerDefinition[] = [];
@ -153,11 +169,12 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
features, features,
workers, workers,
filename, filename,
monacoEditorPath,
publicPath, publicPath,
compilationPublicPath, compilationPublicPath,
globalAPI globalAPI
); );
const plugins = createPlugins(compiler, workers, filename); const plugins = createPlugins(compiler, workers, filename, monacoEditorPath);
addCompilerRules(compiler, rules); addCompilerRules(compiler, rules);
addCompilerPlugins(compiler, plugins); addCompilerPlugins(compiler, plugins);
} }
@ -199,6 +216,7 @@ function createLoaderRules(
features: IFeatureDefinition[], features: IFeatureDefinition[],
workers: ILabeledWorkerDefinition[], workers: ILabeledWorkerDefinition[],
filename: string, filename: string,
monacoEditorPath: string | undefined,
pluginPublicPath: string, pluginPublicPath: string,
compilationPublicPath: string, compilationPublicPath: string,
globalAPI: boolean globalAPI: boolean
@ -209,7 +227,7 @@ function createLoaderRules(
const languagePaths = flatArr(coalesce(languages.map((language) => language.entry))); const languagePaths = flatArr(coalesce(languages.map((language) => language.entry)));
const featurePaths = flatArr(coalesce(features.map((feature) => feature.entry))); const featurePaths = flatArr(coalesce(features.map((feature) => feature.entry)));
const workerPaths = fromPairs( const workerPaths = fromPairs(
workers.map(({ label, entry }) => [label, getWorkerFilename(filename, entry)]) workers.map(({ label, entry }) => [label, getWorkerFilename(filename, entry, monacoEditorPath)])
); );
if (workerPaths['typescript']) { if (workerPaths['typescript']) {
// javascript shares the same worker // javascript shares the same worker
@ -266,14 +284,14 @@ function createLoaderRules(
}; };
return [ return [
{ {
test: /monaco-editor[/\\]esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/, test: /esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/,
use: [ use: [
{ {
loader: INCLUDE_LOADER_PATH, loader: INCLUDE_LOADER_PATH,
options: { options: {
globals, globals,
pre: featurePaths.map((importPath) => resolveMonacoPath(importPath)), pre: featurePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath)),
post: languagePaths.map((importPath) => resolveMonacoPath(importPath)) post: languagePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath))
} }
} }
] ]
@ -284,7 +302,8 @@ function createLoaderRules(
function createPlugins( function createPlugins(
compiler: webpack.Compiler, compiler: webpack.Compiler,
workers: ILabeledWorkerDefinition[], workers: ILabeledWorkerDefinition[],
filename: string filename: string,
monacoEditorPath: string | undefined
): AddWorkerEntryPointPlugin[] { ): AddWorkerEntryPointPlugin[] {
const webpack = compiler.webpack ?? require('webpack'); const webpack = compiler.webpack ?? require('webpack');
@ -293,8 +312,8 @@ function createPlugins(
({ id, entry }) => ({ id, entry }) =>
new AddWorkerEntryPointPlugin({ new AddWorkerEntryPointPlugin({
id, id,
entry: resolveMonacoPath(entry), entry: resolveMonacoPath(entry, monacoEditorPath),
filename: getWorkerFilename(filename, entry), filename: getWorkerFilename(filename, entry, monacoEditorPath),
plugins: [new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })] plugins: [new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })]
}) })
) )

View file

@ -12,6 +12,11 @@ module.exports = {
filename: 'app.js', filename: 'app.js',
publicPath: ASSET_PATH publicPath: ASSET_PATH
}, },
resolve: {
alias: {
'monaco-editor': path.resolve(__dirname, '../../release'),
},
},
module: { module: {
rules: [ rules: [
{ {
@ -24,5 +29,7 @@ module.exports = {
} }
] ]
}, },
plugins: [new MonacoWebpackPlugin()] plugins: [new MonacoWebpackPlugin({
monacoEditorPath: path.resolve(__dirname, '../../release')
})]
}; };

View file

@ -9,6 +9,11 @@ module.exports = {
path: path.resolve(__dirname, 'dist'), path: path.resolve(__dirname, 'dist'),
filename: 'app.js' filename: 'app.js'
}, },
resolve: {
alias: {
'monaco-editor': path.resolve(__dirname, '../../release'),
},
},
module: { module: {
rules: [ rules: [
{ {
@ -21,5 +26,7 @@ module.exports = {
} }
] ]
}, },
plugins: [new MonacoWebpackPlugin()] plugins: [new MonacoWebpackPlugin({
monacoEditorPath: path.resolve(__dirname, '../../release')
})]
}; };