Skip to content

Commit 8f509bb

Browse files
authored
add spike processed file outPath option (#204)
this allows plugins to change the output path of compiled files if they need to for any reason
1 parent 452f803 commit 8f509bb

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

lib/plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ module.exports = class SpikeWebpackPlugin {
6666
: JSON.parse(dep._source._value.replace(/^module\.exports = /, ''))
6767

6868
// calculate the output path
69-
let outputPath = this.util.getOutputPath(f.path).relative
69+
// this will use the special prop f.outPath as an override, which can
70+
// be set by plugins to create a custom output path
71+
let outputPath = this.util.getOutputPath(f.outPath || f.path).relative
7072

7173
// set the file's extension if necessary
7274
if (f.extension) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>my output path was rewritten by a plugin : (</p>

test/fixtures/plugins/plugin.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ module.exports = class JadeWebpackPlugin {
44
}
55

66
apply (compiler) {
7-
compiler.plugin('run', function (compilation, done) {
8-
setTimeout(function () {
7+
compiler.plugin('run', (compilation, done) => {
8+
setTimeout(() => {
99
compiler.options.test = 'bar'
1010
done()
1111
}, 300)
1212
})
13+
14+
compiler.plugin('emit', (compilation, done) => {
15+
const file = compiler.options.spike.files.process.find((f) => {
16+
if (f.path.match(/custom_output/)) { return f }
17+
})
18+
file.outPath = file.path.replace(/custom_output/, 'changed_output')
19+
done()
20+
})
1321
}
1422
}

test/plugins.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ const test = require('ava')
22
const path = require('path')
33
const {compileFixture, fs} = require('./_helpers')
44

5-
test('compiles a project with a custom plugin', (t) => {
5+
test('compiles a project with a custom plugin, plugins can change output path', (t) => {
66
return compileFixture(t, 'plugins')
77
.then(({res, publicPath}) => {
88
const index = path.join(publicPath, 'index.html')
9+
const outputChanged = path.join(publicPath, 'changed_output.html')
910
fs.statSync(index)
11+
fs.statSync(outputChanged)
1012
t.truthy(res.stats.compilation.options.test === 'bar')
1113
})
1214
})

0 commit comments

Comments
 (0)