forked from usabilityhub/rails-erb-loader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
39 lines (32 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const getOptions = require('loader-utils').getOptions
const defaults = require('lodash.defaults')
const parseDependencies = require('./lib/parse_dependencies')
const addDependencies = require('./lib/add_dependencies')
const runner = require('./lib/runner')
module.exports = function railsViewLoader (source, map) {
const loader = this
// Mark loader cacheable. Must be called explicitly in webpack 1.
// see: https://webpack.js.org/guides/migrating/#cacheable
loader.cacheable()
// Get options passed in the loader query, or use defaults.
// Modifying the return value of `getOptions` is not permitted.
const config = defaults({}, getOptions(loader), {
dependenciesRoot: 'app',
runner: './bin/rails runner',
runnerOptions: {}
})
// Dependencies are only useful in development, so don't bother searching the
// file for them otherwise.
const dependencies = process.env.NODE_ENV === 'development'
? parseDependencies(loader, source, config.dependenciesRoot)
: []
const callback = loader.async()
// Register watchers for any dependencies.
addDependencies(loader, dependencies, (error) => {
if (error) {
callback(error)
} else {
runner(loader, config, map, callback)
}
})
}