Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ const emitHook = function emit(
publicPath: true
});

const publicPath = options.publicPath !== null ? options.publicPath : stats.publicPath;
// Webpack v5 supports `output.publicPath: 'auto'`, which resolves the publicPath
// at runtime based on the current script/asset location. In that mode, the
// string value 'auto' should not be serialized into the manifest. Treat it as
// an "unset" publicPath so manifest values remain unprefixed and consumers can
// resolve at runtime. See https://webpack.js.org/guides/public-path/#automatic-publicpath
const resolvedPublicPath = options.publicPath !== null ? options.publicPath : stats.publicPath;
const publicPath = resolvedPublicPath === 'auto' ? '' : resolvedPublicPath;
const { basePath, removeKeyHash } = options;

emitCountMap.set(manifestFileName, emitCount);
Expand Down
64 changes: 64 additions & 0 deletions test/unit/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,70 @@ test('prefixes paths with a public path', async (t) => {
});
});

test("does not prefix paths when webpack's publicPath is 'auto'", async (t) => {
const config = {
context: __dirname,
entry: {
one: '../fixtures/file.js'
},
output: {
filename: `[name].${hashLiteral}.js`,
path: join(outputPath, 'public-auto'),
// Webpack will resolve the publicPath at runtime; the manifest should not contain 'auto'.
publicPath: 'auto'
}
} as any;
const { manifest, stats } = await compile(config, t);

t.deepEqual(manifest, {
'one.js': `one.${(stats as any).hash}.js`
});
});

test("prefixes definitions with a base path when webpack's publicPath is 'auto'", async (t) => {
const config = {
context: __dirname,
entry: {
one: '../fixtures/file.js'
},
output: {
filename: `[name].${hashLiteral}.js`,
path: join(outputPath, 'public-auto-with-basePath'),
publicPath: 'auto'
}
} as any;

const { manifest, stats } = await compile(config, t, {
basePath: '/app/'
});

t.deepEqual(manifest, {
'/app/one.js': `one.${(stats as any).hash}.js`
});
});

test("uses plugin 'publicPath' override when webpack's publicPath is 'auto'", async (t) => {
const config = {
context: __dirname,
entry: {
one: '../fixtures/file.js'
},
output: {
filename: `[name].${hashLiteral}.js`,
path: join(outputPath, 'public-auto-with-override'),
publicPath: 'auto'
}
} as any;

const { manifest, stats } = await compile(config, t, {
publicPath: '/cdn/'
});

t.deepEqual(manifest, {
'one.js': `/cdn/one.${(stats as any).hash}.js`
});
});

test(`prefixes paths with a public path and handle ${hashLiteral} from public path`, async (t) => {
const config = {
context: __dirname,
Expand Down
Loading