Skip to content

Commit 6a42a08

Browse files
fix: publicPath auto (#771)
BREAKING CHANGE: trailing slash at is no longer automatically added for the `publicPath` option, you need added them in configuration, i.e. `publicPath: "/public-path"` should be `publicPath: "/public-path/"`
1 parent 6c3a1a1 commit 6a42a08

File tree

39 files changed

+305
-24
lines changed

39 files changed

+305
-24
lines changed

src/index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import { validate } from 'schema-utils';
44

5+
import { getUndoPath } from 'webpack/lib/util/identifier';
6+
57
import schema from './plugin-options.json';
6-
import { MODULE_TYPE, compareModulesByIdentifier } from './utils';
8+
import {
9+
MODULE_TYPE,
10+
AUTO_PUBLIC_PATH,
11+
compareModulesByIdentifier,
12+
} from './utils';
713

814
export const pluginName = 'mini-css-extract-plugin';
915
export const pluginSymbol = Symbol(pluginName);
@@ -433,7 +439,12 @@ class MiniCssExtractPlugin {
433439
compilation,
434440
chunk,
435441
renderedModules,
436-
compilation.runtimeTemplate.requestShortener
442+
compilation.runtimeTemplate.requestShortener,
443+
filenameTemplate,
444+
{
445+
contentHashType: MODULE_TYPE,
446+
chunk,
447+
}
437448
),
438449
filenameTemplate,
439450
pathOptions: {
@@ -915,7 +926,15 @@ class MiniCssExtractPlugin {
915926
return usedModules;
916927
}
917928

918-
renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
929+
renderContentAsset(
930+
compiler,
931+
compilation,
932+
chunk,
933+
modules,
934+
requestShortener,
935+
filenameTemplate,
936+
pathData
937+
) {
919938
const usedModules = this.sortModules(
920939
compilation,
921940
chunk,
@@ -948,6 +967,15 @@ class MiniCssExtractPlugin {
948967
source.add(`@media ${m.media} {\n`);
949968
}
950969

970+
const { path: filename } = compilation.getPathWithInfo(
971+
filenameTemplate,
972+
pathData
973+
);
974+
975+
const undoPath = getUndoPath(filename, compiler.outputPath, false);
976+
977+
content = content.replace(new RegExp(AUTO_PUBLIC_PATH, 'g'), undoPath);
978+
951979
if (m.sourceMap) {
952980
source.add(
953981
new SourceMapSource(

src/loader.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import loaderUtils from 'loader-utils';
44
import { validate } from 'schema-utils';
55

6-
import { findModuleById, evalModuleCode } from './utils';
6+
import { findModuleById, evalModuleCode, AUTO_PUBLIC_PATH } from './utils';
77
import schema from './loader-options.json';
88

99
import MiniCssExtractPlugin, { pluginName, pluginSymbol } from './index';
@@ -174,18 +174,18 @@ export function pitch(request) {
174174
return callback(null, resultSource);
175175
};
176176

177-
const publicPath =
178-
typeof options.publicPath === 'string'
179-
? options.publicPath === 'auto'
180-
? ''
181-
: options.publicPath === '' || options.publicPath.endsWith('/')
182-
? options.publicPath
183-
: `${options.publicPath}/`
184-
: typeof options.publicPath === 'function'
185-
? options.publicPath(this.resourcePath, this.rootContext)
186-
: this._compilation.outputOptions.publicPath === 'auto'
187-
? ''
188-
: this._compilation.outputOptions.publicPath;
177+
let { publicPath } = this._compilation.outputOptions;
178+
179+
if (typeof options.publicPath === 'string') {
180+
// eslint-disable-next-line prefer-destructuring
181+
publicPath = options.publicPath;
182+
} else if (typeof options.publicPath === 'function') {
183+
publicPath = options.publicPath(this.resourcePath, this.rootContext);
184+
}
185+
186+
if (publicPath === 'auto') {
187+
publicPath = AUTO_PUBLIC_PATH;
188+
}
189189

190190
if (optionsFromPlugin.experimentalUseImportModule) {
191191
if (!this.importModule) {
@@ -300,8 +300,6 @@ export function pitch(request) {
300300
compilation.assets[childFilename] &&
301301
compilation.assets[childFilename].source();
302302

303-
// console.log(source);
304-
305303
// Remove all chunk assets
306304
compilation.chunks.forEach((chunk) => {
307305
chunk.files.forEach((file) => {

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import NativeModule from 'module';
22

33
const MODULE_TYPE = 'css/mini-extract';
4+
const AUTO_PUBLIC_PATH = '__MINI_CSS_EXTRACT_PLUGIN_PUBLIC_PATH__';
45

56
function findModuleById(compilation, id) {
67
const { modules, chunkGraph } = compilation;
@@ -51,6 +52,7 @@ function compareModulesByIdentifier(a, b) {
5152

5253
export {
5354
MODULE_TYPE,
55+
AUTO_PUBLIC_PATH,
5456
findModuleById,
5557
evalModuleCode,
5658
compareModulesByIdentifier,

test/cases/new-url-with-public-path/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
{
1414
loader: Self.loader,
1515
options: {
16-
publicPath: 'public',
16+
publicPath: 'public/',
1717
},
1818
},
1919
'./mockLoader',
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.a {
2+
background-image: url(../same_root.svg);
3+
}
4+
5+
.b {
6+
background-image: url(../styles/same_dir.svg);
7+
}
8+
9+
.c {
10+
background-image: url(../styles/nested/nested_dir.svg);
11+
}
12+
13+
.d {
14+
background-image: url(../../outer.svg);
15+
}
16+
17+
.e {
18+
background-image: url(../assets/img/react.svg);
19+
}
20+
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)