Skip to content

Commit

Permalink
filePathFromLoader function (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante authored Jun 16, 2016
1 parent f7d75d5 commit 641737d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ apply (compiler) {

With it initialized, you can use any of the following functions:

- `addFilesAsWebpackEntries(compilation, files)` - adds one or more files to webpack's pipeline so that it is processed without having to be `require`'d in an entry.
- `getOutputPath(path)` - given a relative or absolute path to a file in a spike project, return it's output path relative to the project root.
- `removeAssets(compilation, files)` - removes assets from webpack's pipeline so that they are not written as entries.
- `resolveRelativeSourcePath(path)` - resolves a relative output path from a spike project to an absolute path to the source file.
- `isFileIgnored(file)` - given a path to a file in a spike project, returns a boolean for whether the file is ignored or not.
- `runAll(compiler, cb)` - run the given function when webpack's compiler initializes, bound to both the `run` and `run-watch` events.
- `util.addFilesAsWebpackEntries(compilation, files)` - adds one or more files to webpack's pipeline so that it is processed without having to be `require`'d in an entry.
- `util.getOutputPath(path)` - given a relative or absolute path to a file in a spike project, return it's output path relative to the project root.
- `util.removeAssets(compilation, files)` - removes assets from webpack's pipeline so that they are not written as entries.
- `util.resolveRelativeSourcePath(path)` - resolves a relative output path from a spike project to an absolute path to the source file.
- `util.isFileIgnored(file)` - given a path to a file in a spike project, returns a boolean for whether the file is ignored or not.
- `util.runAll(compiler, cb)` - run the given function when webpack's compiler initializes, bound to both the `run` and `run-watch` events.

And some static methods:
- `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.

For more details on any given function, check out the source! I would never say this for any other library, but this is only one simple file, with fairly small functions that are extremely thoroughly documented, so I think it's worth jumping in.

Expand Down
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @module SpikeUtils
*/

const url = require('url')
const glob = require('glob')
const path = require('path')
const micromatch = require('micromatch')
Expand Down Expand Up @@ -140,4 +141,15 @@ module.exports = class SpikeUtils {
matchGlobs () {
return micromatch.apply(micromatch, arguments)
}

/**
* Given a loader context, resolves the path of the current file and returns
* a `File` object containing both the absolute and relative paths.
* @param {Object} context - `this` from a webpack loader
* @return {File} file object containing absolute and relative paths
*/
static filePathFromLoader (loaderContext) {
const f = url.parse(loaderContext.request.split('!').slice(-1)[0]).pathname
return new File(loaderContext.options.context, f)
}
}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
"verbose": "true",
"files": [
"test/*.js",
"!test/plugin.js"
"!test/plugin.js",
"!test/loader.js"
]
},
"bugs": "https://github.com/static-dev/spike-utils/issues",
"dependencies": {
"filewrap": "0.1.0",
"glob": "^7.0.3",
"glob": "^7.0.4",
"micromatch": "^2.3.8",
"when": "^3.7.7"
},
"devDependencies": {
"ava": "0.14.x",
"ava": "^0.15.2",
"coveralls": "2.x",
"nyc": "6.x",
"source-loader": "0.0.1",
"spike-core": "^0.4.0",
"standard": "7.x"
"nyc": "^6.6.1",
"source-loader": "^0.2.0",
"spike-core": "^0.7.0",
"standard": "^7.1.2"
},
"engines": {
"node": ">= 6.0.0"
Expand Down
21 changes: 15 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const fs = require('fs')
const Spike = require('spike-core')
const TestPlugin = require('./plugin')
const EventEmitter = require('events')

const fixtures = path.join(__dirname, 'fixtures')

Expand All @@ -11,8 +12,9 @@ test.cb('EVERYTHING WORKS', (t) => {
const plugin = new TestPlugin({
injectFile: path.join(fixturePath, 'views/index.txt')
})
const loaderEmitter = new EventEmitter()

t.plan(10)
t.plan(12)

plugin.on('addFilesAsWebpackEntries', (comp) => {
const mod = comp.modules.find((m) => m.rawRequest === './views/index.txt')
Expand All @@ -34,19 +36,26 @@ test.cb('EVERYTHING WORKS', (t) => {
t.falsy(p.absolute.match('public'))
})

loaderEmitter.on('filePathFromLoader', (f) => {
t.regex(f.absolute, /basic\/views\/index.txt/)
t.truthy(f.relative === 'views/index.txt')
})

const project = new Spike({
root: fixturePath,
entry: { main: ['./entry.js'] },
module: { loaders: [
{ test: /\.txt$/, loader: 'source' }
]},
resolveLoader: { alias: { test: path.join(__dirname, 'loader.js') } },
module: {
loaders: [{ test: /\.txt$/, loader: 'source!test', skipSpikeProcessing: true }]
},
loaderEmitter: loaderEmitter,
ignore: ['**/views/ignoreme.txt'],
plugins: [plugin],
devtool: 'source-map'
})

project.on('error', t.fail)
project.on('warning', t.fail)
project.on('error', console.log)
project.on('warning', console.log)
project.on('compile', (res) => {
try {
fs.accessSync(path.join(fixturePath, 'public/index.txt.js'))
Expand Down
7 changes: 7 additions & 0 deletions test/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Util = require('../lib')

module.exports = function (source) {
const emitter = this.options.loaderEmitter
emitter.emit('filePathFromLoader', Util.filePathFromLoader(this))
return source
}

0 comments on commit 641737d

Please sign in to comment.