Webpacker: Question: env variable inside webpacker.yml

Created on 23 Mar 2018  路  7Comments  路  Source: rails/webpacker

How do I use environment variables inside webpacker.yml?

Tried this syntax and it takes it as a literal string

<%= ENV['x'] %>

Most helpful comment

I want to set webpacker public_output_path based on rails RAILS_RELATIVE_URL_ROOT

All 7 comments

@ratneshraval Webpacker.yml doesn't support erb since we use the same file on both JS and Ruby side. What's your use case?

I want to set webpacker public_output_path based on rails RAILS_RELATIVE_URL_ROOT

Ahh right, this is in todo but for now I guess you can append RAILS_RELATIVE_URL_ROOT in environment.js.

const { config, environment } = require('@rails/webpacker')
const ManifestPlugin = require('webpack-manifest-plugin')

const outputPath = `${config.outputPath}/${process.env.RAILS_RELATIVE_URL_ROOT}`
const publicPath = `${process.env.RAILS_RELATIVE_URL_ROOT}${config.publicPath}`

environment.plugins.append('Manifest', new ManifestPlugin({ publicPath, writeToFileEmit: true }))

environment.config.merge({
   output: {
    publicPath,
    path: outputPath
  }
})

module.exports = environment

I tried that with command
RAILS_RELATIVE_URL_ROOT=/xyz bundle exec rails assets:precompile
but it didn't work! The packs were still placed directly in public/packs instead of public/xyz/packs

@ratneshraval Updated example ^^

Will get this in next release 馃憤

Lets track this issue in #1357

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ankitrg picture ankitrg  路  3Comments

suhomozgy-andrey picture suhomozgy-andrey  路  3Comments

ijdickinson picture ijdickinson  路  3Comments

itay-grudev picture itay-grudev  路  3Comments

eriknygren picture eriknygren  路  3Comments