Webpacker: Resolve alias to source_path

Created on 31 Oct 2017  路  6Comments  路  Source: rails/webpacker

I'm having a hard time finding a way to access the source_path. I'm able to find the public path just find with asset_host; but there doesn't seem to be a way to access the config values from webpacker.yml.

Here's what I'm trying to do:

// /app/javascripts/packs/main.js

import Signup from  '@components/signup.js'

I would like for webpack to resolve @components to /apps/javascripts/components (https://webpack.js.org/configuration/resolve/#resolve-alias).

I can hardcode the path via path.resolve(__dirname, '..', 'app', 'javascripts'); but I don't think that's an elegant path forward.

Most helpful comment

# config/webpack/custom.js
const path = require('path')

module.exports = {
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '..', '..', 'app/javascript/src'),
      '@views': path.resolve(__dirname, '..', '..', 'app/javascript/src/views'),
      '@components': path.resolve(__dirname, '..', '..', 'app/javascript/src/views/components')
    }
  }
}

# config/webpack/environment.js
const { environment } = require('@rails/webpacker')
const customConfig = require('./custom')

environment.config.merge(customConfig)

@gauravtiwari I tried your solution in #1063, and it solve the problem.

All 6 comments

You can get source_path from config:

const config = require('@rails/webpacker/package/config')

I setted config according to the docs:webpack:

# config/webpack/custom.js
module.exports = {
  resolve: {
    alias: {
      @: '/app/javascript/src',
      @views: '/app/javascript/src/views',
      @components: '/app/javascript/src/views/components'
    }
  }
}

# config/webpack/environment.js
const { environment } = require('@rails/webpacker')
const customConfig = require('./custom')

environment.config.merge(customConfig)

however, when it compiles, it said cannot find the module.

@BoleLee What happens if you do this?

module.exports = {
  resolve: {
    alias: {
      @: 'app/javascript/src', // (remove slash)
      @views: 'app/javascript/src/views',
      @components: 'app/javascript/src/views/components'
    }
  }

rest of the code is same as above

# config/webpack/custom.js
const path = require('path')

module.exports = {
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '..', '..', 'app/javascript/src'),
      '@views': path.resolve(__dirname, '..', '..', 'app/javascript/src/views'),
      '@components': path.resolve(__dirname, '..', '..', 'app/javascript/src/views/components')
    }
  }
}

# config/webpack/environment.js
const { environment } = require('@rails/webpacker')
const customConfig = require('./custom')

environment.config.merge(customConfig)

@gauravtiwari I tried your solution in #1063, and it solve the problem.

Great 馃憤 - should be either complete absolute path (more robust) or relative path (from node_modules dir)

  1. The fix is to export a map of routes?
  2. What file is this exported from
  3. What is the require('./custom')?
Was this page helpful?
0 / 5 - 0 ratings