Skip to content

Commit 41db697

Browse files
authored
add modifyOutputPath method (#25)
1 parent 41022f1 commit 41db697

7 files changed

Lines changed: 5060 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ With it initialized, you can use any of the following functions:
3838
- `util.isFileIgnored(file)` - given a path to a file in a spike project, returns a boolean for whether the file is ignored or not.
3939
- `util.runAll(compiler, cb)` - run the given function when webpack's compiler initializes, bound to both the `run` and `run-watch` events.
4040
- `util.pathsToRegex(paths)` - given an array of file paths, builds a regex that will match only those paths
41+
- `util.modifyOutputPath(file, outPath)` - Given an absolute or relative (to the project root) path to a file that's being processed by spike, changes the output path to absolute or relative (recommended) path provided. Recommended to use in the `emit` plugin stage.
4142

4243
And some static methods:
4344
- `SpikeUtils.filePathFromLoader(loaderContext)` - pass `this` inside a loader and it will return a file object with the absolute and relative paths to the current file being processed.

lib/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ module.exports = class SpikeUtils {
5353
return file
5454
}
5555

56+
/**
57+
* Given a source file path and a desired output path, sets spike to write
58+
* the given file to the specified output path
59+
* @param {String} f - path to the source file, relative or absolute
60+
* @param {String} outPath - desired output path
61+
*/
62+
modifyOutputPath (f, outPath) {
63+
const match = this.conf.spike.files.process.find((x) => {
64+
return x.path === new File(this.conf.context, f).absolute
65+
})
66+
match.outPath = new File(this.conf.context, outPath).absolute
67+
}
68+
5669
/**
5770
* Given a file's output path, returns the path to the source file.
5871
* @param {String} f - path to an output file, relative or absolute

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"coveralls": "2.x",
2424
"nyc": "^10.0.0",
2525
"source-loader": "^0.2.0",
26-
"spike-core": "^0.13.0",
26+
"spike-core": "^0.13.1",
2727
"standard": "^8.0.0"
2828
},
2929
"engines": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>change my path!</p>

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ test.cb('EVERYTHING WORKS', (t) => {
6363
} catch (err) {
6464
t.truthy(err.code === 'ENOENT')
6565
}
66+
fs.accessSync(path.join(fixturePath, 'public/changed.html'))
6667
t.end()
6768
})
6869

test/plugin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ module.exports = class TestPlugin extends EventEmitter {
2929
done()
3030
})
3131
})
32+
33+
compiler.plugin('emit', (compilation, done) => {
34+
this.util.modifyOutputPath('views/changepath.html', 'views/changed.html')
35+
done()
36+
})
3237
}
3338

3439
run (compilation, done) {

0 commit comments

Comments
 (0)