Skip to content

Commit

Permalink
Include RAILS_RELATIVE_URL_ROOT environment variable in `publicPath…
Browse files Browse the repository at this point in the history
…`. (#1428)
  • Loading branch information
jpickwell authored and gauravtiwari committed Apr 29, 2018
1 parent bef618f commit 6d30671
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 17 additions & 1 deletion package/__tests__/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
/* global test expect, describe */

const { chdirTestApp, chdirCwd } = require('../utils/helpers')
const { chdirCwd, chdirTestApp } = require('../utils/helpers')

chdirTestApp()

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

describe('Config', () => {
beforeEach(() => jest.resetModules())
afterAll(chdirCwd)

test('public path', () => {
process.env.RAILS_ENV = 'development'
delete process.env.RAILS_RELATIVE_URL_ROOT
const config = require('../config')
expect(config.publicPath).toEqual('/packs/')
})

// also tests removal of extra slashes
test('public path with relative root', () => {
process.env.RAILS_ENV = 'development'
process.env.RAILS_RELATIVE_URL_ROOT = '/foo'
const config = require('../config')
expect(config.publicPath).toEqual('/foo/packs/')
})

test('should return extensions as listed in app config', () => {
expect(config.extensions).toEqual([
'.js',
Expand Down
10 changes: 9 additions & 1 deletion package/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions

const config = deepMerge(defaults, app)
config.outputPath = resolve('public', config.public_output_path)
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')

let publicPath = `/${config.public_output_path}/`
// Add prefix to publicPath.
if (process.env.RAILS_RELATIVE_URL_ROOT) {
publicPath = `/${process.env.RAILS_RELATIVE_URL_ROOT}${publicPath}`
}

// Remove extra slashes.
config.publicPath = publicPath.replace(/(^\/|[^:]\/)\/+/g, '$1')

module.exports = config

0 comments on commit 6d30671

Please sign in to comment.