Skip to content

Commit 843d1b0

Browse files
committed
Use JS timeout instead of Ruby timeout
Closes #54
1 parent 8fc11f7 commit 843d1b0

File tree

5 files changed

+5252
-37
lines changed

5 files changed

+5252
-37
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Can be configured with [UseEntry#options](https://webpack.js.org/configuration/m
9999
| `dependenciesRoot` | `"app"` | The root of your Rails project, relative to webpack's working directory. |
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. |
102-
| `timeout` | `0` | Timeout for the runner task in seconds. `0` is no timeout. Set this if you want a hanging runner to error out the build.
102+
| `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. |
103103

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

erb_transformer.rb

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
require 'timeout'
2-
3-
delimiter, engine, timeout = ARGV
4-
timeout = Float(timeout)
5-
1+
delimiter = ARGV[0]
2+
engine = ARGV[1]
63
handler = case engine
74
when 'erubi'
85
require 'erubi'
@@ -16,16 +13,8 @@
1613
else raise "Unknown templating engine `#{engine}`"
1714
end
1815

19-
begin
20-
Timeout.timeout(timeout) do
21-
source = STDIN.read
22-
23-
if engine == 'erubi'
24-
puts "#{delimiter}#{eval(handler.new(source).src)}#{delimiter}"
25-
else
26-
puts "#{delimiter}#{handler.new(source).result}#{delimiter}"
27-
end
28-
end
29-
rescue Timeout::Error
30-
raise "rails-erb-loader took longer than the specified #{timeout} second timeout"
16+
if engine == 'erubi'
17+
puts "#{delimiter}#{eval(handler.new(STDIN.read).src)}#{delimiter}"
18+
else
19+
puts "#{delimiter}#{handler.new(STDIN.read).result}#{delimiter}"
3120
end

index.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var execFile = require('child_process').execFile
33
var path = require('path')
44
var getOptions = require('loader-utils').getOptions
55
var defaults = require('lodash.defaults')
6+
var util = require('util')
67

78
function pushAll (dest, src) {
89
Array.prototype.push.apply(dest, src)
@@ -74,16 +75,27 @@ function transformSource (runner, config, source, map, callback) {
7475
runner.arguments.concat(
7576
runnerPath,
7677
ioDelimiter,
77-
config.engine,
78-
config.timeout
78+
config.engine
7979
),
80+
{ timeout: config.timeoutMs },
8081
function (error, stdout, stderr) {
8182
// Output is delimited to filter out unwanted warnings or other output
8283
// that we don't want in our files.
8384
var sourceRegex = new RegExp(ioDelimiter + '([\\s\\S]+)' + ioDelimiter)
8485
var matches = stdout.match(sourceRegex)
8586
var transformedSource = matches && matches[1]
86-
callback(error, transformedSource, map)
87+
if (error && error.signal === 'SIGTERM') {
88+
if (config.timeoutMs) {
89+
callback(new Error(
90+
'rails-erb-loader took longer than the specified ' + config.timeoutMs +
91+
'ms timeout'
92+
))
93+
} else {
94+
callback(error)
95+
}
96+
} else {
97+
callback(error, transformedSource, map)
98+
}
8799
}
88100
)
89101
child.stdin.on('error', function (error) {
@@ -132,6 +144,17 @@ function addDependencies (loader, paths, callback) {
132144
})
133145
}
134146

147+
var setTimeoutMsFromTimeoutInPlace = util.deprecate(function (config) {
148+
if (config.timeoutMs != null) {
149+
throw new TypeError(
150+
'Both options `timeout` and `timeoutMs` were set -- please just use ' +
151+
'`timeoutMs`'
152+
)
153+
}
154+
config.timeoutMs = config.timeout * 1000
155+
delete config.timeout
156+
}, 'rails-erb-loader `timeout` option is deprecated in favor of `timeoutMs`')
157+
135158
module.exports = function railsErbLoader (source, map) {
136159
var loader = this
137160

@@ -144,10 +167,13 @@ module.exports = function railsErbLoader (source, map) {
144167
var config = defaults({}, getOptions(loader), {
145168
dependenciesRoot: 'app',
146169
runner: './bin/rails runner',
147-
engine: 'erb',
148-
timeout: 0
170+
engine: 'erb'
149171
})
150172

173+
if (config.timeout !== undefined) {
174+
setTimeoutMsFromTimeoutInPlace(config)
175+
}
176+
151177
// Dependencies are only useful in development, so don't bother searching the
152178
// file for them otherwise.
153179
var dependencies = process.env.NODE_ENV === 'development'

0 commit comments

Comments
 (0)