Sha256: bcf898bd996cfbb60aa3a860f0558675c50db736e57aafda1cd49db319e1d803

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

/**
 * Allow defaults for the config/webpacker.yml. Thee values in this file MUST match values
 * in https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml
 *
 * NOTE: for HMR reloading, env.WEBPACKER_HMR value will override any config value. This env value
 * should be set to TRUE to turn this on.
 */

const { join, resolve } = require('path');
const { env } = require('process');
const { safeLoad } = require('js-yaml');
const { readFileSync } = require('fs');


function removeOuterSlashes(string) {
  return string.replace(/^\/*/, '').replace(/\/*$/, '');
}

function formatPublicPath(host = '', path = '') {
  let formattedHost = removeOuterSlashes(host);
  if (formattedHost && !/^http/i.test(formattedHost)) {
    formattedHost = `//${formattedHost}`;
  }
  const formattedPath = removeOuterSlashes(path);
  return `${formattedHost}/${formattedPath}/`;
}

/**
 * @param configPath, location where webpacker.yml will be found
 * Return values are consistent with Webpacker's js helpers
 * @returns {{
     settings,
     resolvedModules,
     output: { path, publicPath, publicPathWithHosts }
   }}
 */
const configLoader = (configPath) => {
  // Some test environments might not have the NODE_ENV set, so we'll have fallbacks.
  const configEnv = (process.env.NODE_ENV || process.env.RAILS_ENV || 'development');
  const ymlConfigPath = join(configPath, 'webpacker.yml');
  const settings = safeLoad(readFileSync(ymlConfigPath, 'utf8'))[configEnv];

  // NOTE: Rails path is hard coded to `/public`
  const output = {
    // Next line differs from the webpacker definition as we use the configPath to create
    // the relative path.
    path: resolve(configPath, '..', 'public', settings.public_output_path),
    publicPath: `/${settings.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1'),
    publicPathWithHost: formatPublicPath(env.ASSET_HOST, settings.public_output_path),
  };

  return {
    settings,
    output,
  };
};

module.exports = configLoader;

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
react_on_rails-9.0.0.beta.9 webpackConfigLoader.js
react_on_rails-9.0.0.beta.8 webpackConfigLoader.js
react_on_rails-9.0.0.beta.7 webpackConfigLoader.js