Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit b892949

Browse files
authored
Merge pull request #19 from bekos/publicpath
Fix Respect `output.publicPath`
2 parents 18b26a2 + 502e777 commit b892949

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ class PreloadPlugin {
6868
})
6969
.map(chunk => chunk.files);
7070
}
71+
72+
const publicPath = compilation.outputOptions.publicPath || '';
7173
extractedChunks.forEach(entry => {
74+
entry = `${publicPath}${entry}`;
7275
if (options.rel === 'preload') {
7376
filesToInclude+= `<link rel="${options.rel}" href="${entry}" as="${options.as}">\n`;
7477
} else {

test/spec.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ describe('PreloadPlugin preloads or prefetches async chunks', function() {
4444
expect(err).toBeFalsy();
4545
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
4646
const html = result.compilation.assets['index.html'].source();
47-
expect(html).toContain('<link rel="preload" href="chunk.');
48-
expect(html).not.toContain('<link rel="preload" href="bundle.');
47+
expect(html).toContain('<link rel="preload" href="/chunk.');
48+
expect(html).not.toContain('<link rel="preload" href="/bundle.');
4949
done();
5050
});
5151
compiler.outputFileSystem = new MemoryFileSystem();
@@ -72,7 +72,33 @@ describe('PreloadPlugin preloads or prefetches async chunks', function() {
7272
expect(err).toBeFalsy();
7373
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
7474
const html = result.compilation.assets['index.html'].source();
75-
expect(html).toContain('<link rel="prefetch" href="chunk.');
75+
expect(html).toContain('<link rel="prefetch" href="/chunk.');
76+
done();
77+
});
78+
compiler.outputFileSystem = new MemoryFileSystem();
79+
});
80+
81+
it('respects publicPath', function(done) {
82+
const compiler = webpack({
83+
entry: {
84+
js: path.join(__dirname, 'fixtures', 'file.js')
85+
},
86+
output: {
87+
path: OUTPUT_DIR,
88+
filename: 'bundle.js',
89+
chunkFilename: 'chunk.[chunkhash].js',
90+
publicPath: 'http://mycdn.com/',
91+
},
92+
plugins: [
93+
new HtmlWebpackPlugin(),
94+
new PreloadPlugin()
95+
]
96+
}, function(err, result) {
97+
expect(err).toBeFalsy();
98+
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
99+
const html = result.compilation.assets['index.html'].source();
100+
expect(html).toContain('<link rel="preload" href="http://mycdn.com/chunk.');
101+
expect(html).not.toContain('<link rel="preload" href="http://mycdn.com/bundle.');
76102
done();
77103
});
78104
compiler.outputFileSystem = new MemoryFileSystem();
@@ -101,8 +127,8 @@ describe('PreloadPlugin preloads normal chunks', function() {
101127
expect(err).toBeFalsy();
102128
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
103129
const html = result.compilation.assets['index.html'].source();
104-
expect(html).toContain('<link rel="preload" href="chunk');
105-
expect(html).toContain('<link rel="preload" href="bundle.js"');
130+
expect(html).toContain('<link rel="preload" href="/chunk');
131+
expect(html).toContain('<link rel="preload" href="/bundle.js"');
106132
done();
107133
});
108134
compiler.outputFileSystem = new MemoryFileSystem();
@@ -157,8 +183,8 @@ describe('PreloadPlugin filters chunks', function() {
157183
expect(err).toBeFalsy();
158184
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
159185
const html = result.compilation.assets['index.html'].source();
160-
expect(html).toContain('<link rel="preload" href="home');
161-
expect(html).not.toContain('<link rel="preload" href="bundle.js"');
186+
expect(html).toContain('<link rel="preload" href="/home');
187+
expect(html).not.toContain('<link rel="preload" href="/bundle.js"');
162188
done();
163189
});
164190
compiler.outputFileSystem = new MemoryFileSystem();

0 commit comments

Comments
 (0)