fix: new webpack config doesn't work on windows (#4030)

This commit is contained in:
2024-09-28 21:04:50 +08:00 committed by GitHub
parent 29bb477efe
commit c0d3d976fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View File

@ -6,6 +6,14 @@ const ConcatenatedModule = require('webpack/lib/optimize/ConcatenatedModule');
class RegisterAsyncChunksPlugin {
static registry = {};
processUrlPath(urlPath) {
if (path.sep == "\\") {
// separator on windows is "\", this will cause escape issues when used in url path.
return urlPath.replace(/\\/g, '/');
}
return urlPath;
}
apply(compiler) {
compiler.hooks.thisCompilation.tap('RegisterAsyncChunksPlugin', (compilation) => {
let alreadyOptimized = false;
@ -63,6 +71,8 @@ class RegisterAsyncChunksPlugin {
// Each line that has a webpackChunkName comment.
[...module._source._value.matchAll(/.*\/\* webpackChunkName: .* \*\/.*/gm)].forEach(([match]) => {
[...match.matchAll(/(.*?) webpackChunkName: '([^']*)'.*? \*\/ '([^']+)'.*?/gm)].forEach(([match, _, urlPath, importPath]) => {
urlPath = this.processUrlPath(urlPath);
// Import path is relative to module.resource, so we need to resolve it
const importPathResolved = path.resolve(path.dirname(module.resource), importPath);
const thisComposerJson = require(path.resolve(process.cwd(), '../composer.json'));
@ -118,7 +128,7 @@ class RegisterAsyncChunksPlugin {
// The path right after the src/ directory, without the extension.
const regPathSep = `\\${path.sep}`;
const urlPath = module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1');
const urlPath = this.processUrlPath(module.resource.replace(new RegExp(`.*${regPathSep}src${regPathSep}([^.]+)\..+`), '$1'));
if (!registrableModulesUrlPaths.has(urlPath)) {
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);

View File

@ -63,7 +63,12 @@ module.exports = function autoChunkNameLoader(source) {
let chunkPath = relativePathToImport;
if (absolutePathToImport.includes('src')) {
chunkPath = absolutePathToImport.split('src/')[1];
chunkPath = absolutePathToImport.split(`src${path.sep}`)[1];
}
if (path.sep == "\\") {
// separator on windows is '\', the resolver only works with '/'.
chunkPath = chunkPath.replace(/\\/g, '/');
}
const webpackCommentOptions = {