mirror of
https://github.com/flarum/core.git
synced 2025-08-04 15:37:51 +02:00
chore: yarn format
This commit is contained in:
@@ -1,27 +1,27 @@
|
|||||||
const path = require("path");
|
const path = require('path');
|
||||||
const extensionId = require("./extensionId.cjs");
|
const extensionId = require('./extensionId.cjs');
|
||||||
const {Compilation} = require("webpack");
|
const { Compilation } = require('webpack');
|
||||||
|
|
||||||
class RegisterAsyncChunksPlugin {
|
class RegisterAsyncChunksPlugin {
|
||||||
static registry = {};
|
static registry = {};
|
||||||
|
|
||||||
apply(compiler) {
|
apply(compiler) {
|
||||||
compiler.hooks.thisCompilation.tap("RegisterAsyncChunksPlugin", (compilation) => {
|
compiler.hooks.thisCompilation.tap('RegisterAsyncChunksPlugin', (compilation) => {
|
||||||
let alreadyOptimized = false;
|
let alreadyOptimized = false;
|
||||||
|
|
||||||
compilation.hooks.unseal.tap("RegisterAsyncChunksPlugin", () => {
|
compilation.hooks.unseal.tap('RegisterAsyncChunksPlugin', () => {
|
||||||
alreadyOptimized = false;
|
alreadyOptimized = false;
|
||||||
RegisterAsyncChunksPlugin.registry = {};
|
RegisterAsyncChunksPlugin.registry = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
compilation.hooks.finishModules.tap("RegisterAsyncChunksPlugin", () => {
|
compilation.hooks.finishModules.tap('RegisterAsyncChunksPlugin', () => {
|
||||||
alreadyOptimized = false;
|
alreadyOptimized = false;
|
||||||
RegisterAsyncChunksPlugin.registry = {};
|
RegisterAsyncChunksPlugin.registry = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
compilation.hooks.processAssets.tap(
|
compilation.hooks.processAssets.tap(
|
||||||
{
|
{
|
||||||
name: "RegisterAsyncChunksPlugin",
|
name: 'RegisterAsyncChunksPlugin',
|
||||||
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
|
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
@@ -37,14 +37,14 @@ class RegisterAsyncChunksPlugin {
|
|||||||
modulesToCheck[chunk.id] = modulesToCheck[chunk.id] || [];
|
modulesToCheck[chunk.id] = modulesToCheck[chunk.id] || [];
|
||||||
|
|
||||||
// A normal module.
|
// A normal module.
|
||||||
if (module?.resource && module.resource.split(path.sep).includes('src') && module._source?._value.includes("webpackChunkName: ")) {
|
if (module?.resource && module.resource.split(path.sep).includes('src') && module._source?._value.includes('webpackChunkName: ')) {
|
||||||
modulesToCheck[chunk.id].push(module);
|
modulesToCheck[chunk.id].push(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A ConcatenatedModule.
|
// A ConcatenatedModule.
|
||||||
if (module?.modules) {
|
if (module?.modules) {
|
||||||
module.modules.forEach((module) => {
|
module.modules.forEach((module) => {
|
||||||
if (module.resource && module.resource.split(path.sep).includes('src') && module._source?._value.includes("webpackChunkName: ")) {
|
if (module.resource && module.resource.split(path.sep).includes('src') && module._source?._value.includes('webpackChunkName: ')) {
|
||||||
modulesToCheck[chunk.id].push(module);
|
modulesToCheck[chunk.id].push(module);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -53,73 +53,76 @@ class RegisterAsyncChunksPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const sourceChunkId in modulesToCheck) {
|
for (const sourceChunkId in modulesToCheck) {
|
||||||
for (const module of modulesToCheck[sourceChunkId]) {
|
for (const module of modulesToCheck[sourceChunkId]) {
|
||||||
// If the module source has an async webpack chunk, add the chunk id to flarum.reg
|
// If the module source has an async webpack chunk, add the chunk id to flarum.reg
|
||||||
// at the end of the module source.
|
// at the end of the module source.
|
||||||
|
|
||||||
const reg = [];
|
const reg = [];
|
||||||
|
|
||||||
// Each line that has a webpackChunkName comment.
|
// Each line that has a webpackChunkName comment.
|
||||||
[...module._source._value.matchAll(/.*\/\* webpackChunkName: .* \*\/.*/gm)].forEach(([match]) => {
|
[...module._source._value.matchAll(/.*\/\* webpackChunkName: .* \*\/.*/gm)].forEach(([match]) => {
|
||||||
[...match.matchAll(/(.*?) webpackChunkName: '([^']*)'.*? \*\/ '([^']+)'.*?/gm)]
|
[...match.matchAll(/(.*?) webpackChunkName: '([^']*)'.*? \*\/ '([^']+)'.*?/gm)].forEach(([match, _, urlPath, importPath]) => {
|
||||||
.forEach(([match, _, urlPath, importPath]) => {
|
// Import path is relative to module.resource, so we need to resolve it
|
||||||
// Import path is relative to module.resource, so we need to resolve it
|
const importPathResolved = path.resolve(path.dirname(module.resource), importPath);
|
||||||
const importPathResolved = path.resolve(path.dirname(module.resource), importPath);
|
const thisComposerJson = require(path.resolve(process.cwd(), '../composer.json'));
|
||||||
const thisComposerJson = require(path.resolve(process.cwd(), '../composer.json'));
|
const namespace = extensionId(thisComposerJson.name);
|
||||||
const namespace = extensionId(thisComposerJson.name);
|
|
||||||
|
|
||||||
const chunkModules = (c) => Array.from(compilation.chunkGraph.getChunkModulesIterable(c));
|
const chunkModules = (c) => Array.from(compilation.chunkGraph.getChunkModulesIterable(c));
|
||||||
|
|
||||||
const relevantChunk = chunks.find(
|
const relevantChunk = chunks.find((chunk) =>
|
||||||
(chunk) => chunkModules(chunk)?.find(
|
chunkModules(chunk)?.find(
|
||||||
(module) => module.resource?.split('.')[0] === importPathResolved || module.rootModule?.resource?.split('.')[0] === importPathResolved
|
(module) =>
|
||||||
)
|
module.resource?.split('.')[0] === importPathResolved || module.rootModule?.resource?.split('.')[0] === importPathResolved
|
||||||
);
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (! relevantChunk) {
|
if (!relevantChunk) {
|
||||||
console.error(`Could not find chunk for ${importPathResolved}`);
|
console.error(`Could not find chunk for ${importPathResolved}`);
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
let concatenatedModule = chunkModules(relevantChunk)[0];
|
let concatenatedModule = chunkModules(relevantChunk)[0];
|
||||||
const moduleId = compilation.chunkGraph.getModuleId(concatenatedModule);
|
const moduleId = compilation.chunkGraph.getModuleId(concatenatedModule);
|
||||||
const registrableModulesUrlPaths = new Map();
|
const registrableModulesUrlPaths = new Map();
|
||||||
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
|
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
|
||||||
|
|
||||||
if (concatenatedModule?.rootModule) {
|
if (concatenatedModule?.rootModule) {
|
||||||
// This is a chunk with many modules, we need to register all of them.
|
// This is a chunk with many modules, we need to register all of them.
|
||||||
concatenatedModule.modules?.forEach((module) => {
|
concatenatedModule.modules?.forEach((module) => {
|
||||||
if (! module.resource.includes(`${path.sep}src${path.sep}`)) {
|
if (!module.resource.includes(`${path.sep}src${path.sep}`)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The path right after the src/ directory, without the extension.
|
// The path right after the src/ directory, without the extension.
|
||||||
const regPathSep = `\\${path.sep}`;
|
const regPathSep = `\\${path.sep}`;
|
||||||
const urlPath = module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1');
|
const urlPath = module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1');
|
||||||
|
|
||||||
if (! registrableModulesUrlPaths.has(urlPath)) {
|
if (!registrableModulesUrlPaths.has(urlPath)) {
|
||||||
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
|
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registrableModulesUrlPaths.forEach(([chunkId, moduleId, namespace, urlPath]) => {
|
registrableModulesUrlPaths.forEach(([chunkId, moduleId, namespace, urlPath]) => {
|
||||||
chunkModuleMemory[sourceChunkId] = chunkModuleMemory[sourceChunkId] || [];
|
chunkModuleMemory[sourceChunkId] = chunkModuleMemory[sourceChunkId] || [];
|
||||||
|
|
||||||
if (! chunkModuleMemory[sourceChunkId].includes(urlPath) && ! RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`]?.includes(urlPath)) {
|
if (
|
||||||
reg.push(`flarum.reg.addChunkModule('${chunkId}', '${moduleId}', '${namespace}', '${urlPath}');`);
|
!chunkModuleMemory[sourceChunkId].includes(urlPath) &&
|
||||||
chunkModuleMemory[sourceChunkId].push(urlPath);
|
!RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`]?.includes(urlPath)
|
||||||
RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`] ||= [];
|
) {
|
||||||
RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`].push(urlPath);
|
reg.push(`flarum.reg.addChunkModule('${chunkId}', '${moduleId}', '${namespace}', '${urlPath}');`);
|
||||||
}
|
chunkModuleMemory[sourceChunkId].push(urlPath);
|
||||||
});
|
RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`] ||= [];
|
||||||
|
RegisterAsyncChunksPlugin.registry[`${chunkId}:${moduleId}:${namespace}`].push(urlPath);
|
||||||
return match;
|
}
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module._source._value += reg.join('\n');
|
return match;
|
||||||
}
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
module._source._value += reg.join('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user