diff --git a/package/__tests__/config.js b/package/__tests__/config.js index acdd99e58..ee722cf43 100644 --- a/package/__tests__/config.js +++ b/package/__tests__/config.js @@ -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', diff --git a/package/config.js b/package/config.js index 48ae7778a..e0dba2cec 100644 --- a/package/config.js +++ b/package/config.js @@ -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