Skip to content

Commit dabf4e4

Browse files
jpickwellgauravtiwari
authored andcommitted
Include RAILS_RELATIVE_URL_ROOT environment variable in publicPath. (#1428)
1 parent 182fab0 commit dabf4e4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

package/__tests__/config.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
/* global test expect, describe */
22

3-
const { chdirTestApp, chdirCwd } = require('../utils/helpers')
3+
const { chdirCwd, chdirTestApp } = require('../utils/helpers')
44

55
chdirTestApp()
66

77
const config = require('../config')
88

99
describe('Config', () => {
10+
beforeEach(() => jest.resetModules())
1011
afterAll(chdirCwd)
1112

13+
test('public path', () => {
14+
process.env.RAILS_ENV = 'development'
15+
delete process.env.RAILS_RELATIVE_URL_ROOT
16+
const config = require('../config')
17+
expect(config.publicPath).toEqual('/packs/')
18+
})
19+
20+
// also tests removal of extra slashes
21+
test('public path with relative root', () => {
22+
process.env.RAILS_ENV = 'development'
23+
process.env.RAILS_RELATIVE_URL_ROOT = '/foo'
24+
const config = require('../config')
25+
expect(config.publicPath).toEqual('/foo/packs/')
26+
})
27+
1228
test('should return extensions as listed in app config', () => {
1329
expect(config.extensions).toEqual([
1430
'.js',

package/config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions
2020

2121
const config = deepMerge(defaults, app)
2222
config.outputPath = resolve('public', config.public_output_path)
23-
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')
23+
24+
let publicPath = `/${config.public_output_path}/`
25+
// Add prefix to publicPath.
26+
if (process.env.RAILS_RELATIVE_URL_ROOT) {
27+
publicPath = `/${process.env.RAILS_RELATIVE_URL_ROOT}${publicPath}`
28+
}
29+
30+
// Remove extra slashes.
31+
config.publicPath = publicPath.replace(/(^\/|[^:]\/)\/+/g, '$1')
2432

2533
module.exports = config

0 commit comments

Comments
 (0)