Skip to content

Commit 3c87122

Browse files
committed
Allow specifying environment variables for runner in options
1 parent 13ba815 commit 3c87122

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Can be configured with [UseEntry#options](https://webpack.js.org/configuration/m
100100
| `engine` | `"erb"` | ERB Template engine, `"erubi"`, `"erubis"` and `"erb"` are supported. |
101101
| `runner` | `"./bin/rails runner"` | Command to run Ruby scripts, relative to webpack's working directory. |
102102
| `timeoutMs` | `0` | Timeout for the runner task in milliseconds. `0` is no timeout. Set this if you want a hanging runner to error out the build. |
103+
| `env` | `process.env` | Environment variables to be passed to runner. |
103104

104105
For example, if your webpack process is running in a subdirectory of your Rails project:
105106

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,19 @@ function parseDependencies (source, root) {
7070
* output transformed source.
7171
*/
7272
function transformSource (runner, config, source, map, callback) {
73+
var subprocessOptions = {
74+
stdio: ['pipe', 'pipe', process.stderr],
75+
env: config.env
76+
}
77+
7378
var child = spawn(
7479
runner.file,
7580
runner.arguments.concat(
7681
runnerPath,
7782
ioDelimiter,
7883
config.engine
7984
),
80-
{ stdio: ['pipe', 'pipe', process.stderr] }
85+
subprocessOptions
8186
)
8287
var timeoutId = config.timeoutMs
8388
? setTimeout(function () { child.kill() }, config.timeoutMs)
@@ -186,7 +191,8 @@ module.exports = function railsErbLoader (source, map) {
186191
var config = defaults({}, getOptions(loader), {
187192
dependenciesRoot: 'app',
188193
runner: './bin/rails runner',
189-
engine: 'erb'
194+
engine: 'erb',
195+
env: process.env
190196
})
191197

192198
if (config.timeout !== undefined) {

test.js

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ test('loads through a Rails-like runner', function (done) {
9191
})
9292
})
9393

94+
test('loads with env specified in option', function (done) {
95+
compile2({ file: 'runner.js.erb', runner: './test/runner', env: {ENV: 'custom'} }, done, function (stats) {
96+
expect(stats.compilation.errors).toEqual([])
97+
expectInOutput("var env = 'custom'")
98+
done()
99+
})
100+
})
101+
94102
test('does not error with large files', function (done) {
95103
compile2({ file: 'giant.js.erb' }, done, function (stats) {
96104
expect(stats.compilation.errors).toEqual([])

test/runner

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/env ruby
22

33
require 'ostruct'
4-
Runner = OpenStruct.new(env: 'test')
4+
Runner = OpenStruct.new(env: ENV["ENV"] || 'test')
55

66
def perform(file, *command_argv)
77
ARGV.replace(command_argv)

0 commit comments

Comments
 (0)