Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Support configuration inheritance #164

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"@paulcbetts/mime-types": "^2.1.10",
"btoa": "^1.1.2",
"debug": "^2.5.1",
"lodash.merge": "^4.6.0",
"lodash.omit": "^4.5.0",
"lru-cache": "^4.0.1",
"mkdirp": "^0.5.1",
"pify": "^2.3.0",
Expand Down
83 changes: 42 additions & 41 deletions src/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {pfs} from './promise';
import FileChangedCache from './file-change-cache';
import CompilerHost from './compiler-host';
import registerRequireExtension from './require-hook';
import omit from 'lodash.omit';
import merge from 'lodash.merge';

const d = require('debug')('electron-compile:config-parser');

Expand All @@ -26,6 +28,37 @@ function statSyncNoException(fsPath) {
}
}

/**
* Read compiler configuration from specified file.
* If configuration contains 'common' denominator with `env` object for environment
* specific configuration, returned object will inherit environment specific configuration
* from common configuration values.
*
* @param {string} file The path to a compiler configuration file
* @returns {Object} configuration objec from given file
*/
async function readCompilerConfig(file, env = 'development') {
const configuration = JSON.parse(await pfs.readFile(file, 'utf8'));
return readCompilerConfiguration(configuration, env);
}

function readCompilerConfigSync(file, env = 'development') {
const configuration = JSON.parse(fs.readFileSync(file, 'utf8'));
return readCompilerConfiguration(configuration, env);
}

export function readCompilerConfiguration(configObject, env) {
let config = configObject;

if ('babel' in configObject) {
d(`found 'babel' section in configuration, assume given object is package.json`);
config = configObject.babel;
}

return 'env' in config ?
merge({}, omit(config, 'env'), config.env[env]) :
config;
}

/**
* Initialize the global hooks (protocol hook for file:, node.js hook)
Expand Down Expand Up @@ -89,12 +122,7 @@ export function init(appRoot, mainModule, productionMode = null, cacheDir = null
compilerHost = CompilerHost.createReadonlyFromConfigurationSync(rootCacheDir, appRoot);
} else {
// if cacheDir was passed in, pass it along. Otherwise, default to a tempdir.
if (cacheDir) {
compilerHost = createCompilerHostFromProjectRootSync(appRoot, rootCacheDir);
} else {
compilerHost = createCompilerHostFromProjectRootSync(appRoot);
}

compilerHost = createCompilerHostFromProjectRootSync(appRoot, cacheDir ? rootCacheDir: null);
}

initializeGlobalHooks(compilerHost);
Expand Down Expand Up @@ -158,17 +186,8 @@ export function createCompilerHostFromConfiguration(info) {
* @return {Promise<CompilerHost>} A set-up compiler host
*/
export async function createCompilerHostFromBabelRc(file, rootCacheDir=null) {
let info = JSON.parse(await pfs.readFile(file, 'utf8'));

// package.json
if ('babel' in info) {
info = info.babel;
}

if ('env' in info) {
let ourEnv = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
info = info.env[ourEnv];
}
const environment = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
const info = await readCompilerConfig(file, environment);

// Are we still package.json (i.e. is there no babel info whatsoever?)
if ('name' in info && 'version' in info) {
Expand All @@ -188,7 +207,6 @@ export async function createCompilerHostFromBabelRc(file, rootCacheDir=null) {
});
}


/**
* Creates a compiler host from a .compilerc file. This method is usually called
* from {@link createCompilerHostFromProjectRoot} instead of used directly.
Expand All @@ -200,12 +218,8 @@ export async function createCompilerHostFromBabelRc(file, rootCacheDir=null) {
* @return {Promise<CompilerHost>} A set-up compiler host
*/
export async function createCompilerHostFromConfigFile(file, rootCacheDir=null) {
let info = JSON.parse(await pfs.readFile(file, 'utf8'));

if ('env' in info) {
let ourEnv = process.env.ELECTRON_COMPILE_ENV || process.env.NODE_ENV || 'development';
info = info.env[ourEnv];
}
const environment = process.env.ELECTRON_COMPILE_ENV || process.env.NODE_ENV || 'development';
const info = await readCompilerConfig(file, environment);

return createCompilerHostFromConfiguration({
appRoot: path.dirname(file),
Expand Down Expand Up @@ -251,17 +265,8 @@ export async function createCompilerHostFromProjectRoot(rootDir, rootCacheDir=nu
}

export function createCompilerHostFromBabelRcSync(file, rootCacheDir=null) {
let info = JSON.parse(fs.readFileSync(file, 'utf8'));

// package.json
if ('babel' in info) {
info = info.babel;
}

if ('env' in info) {
let ourEnv = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
info = info.env[ourEnv];
}
const environment = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
const info = readCompilerConfigSync(file, environment);

// Are we still package.json (i.e. is there no babel info whatsoever?)
if ('name' in info && 'version' in info) {
Expand All @@ -282,12 +287,8 @@ export function createCompilerHostFromBabelRcSync(file, rootCacheDir=null) {
}

export function createCompilerHostFromConfigFileSync(file, rootCacheDir=null) {
let info = JSON.parse(fs.readFileSync(file, 'utf8'));

if ('env' in info) {
let ourEnv = process.env.ELECTRON_COMPILE_ENV || process.env.NODE_ENV || 'development';
info = info.env[ourEnv];
}
const environment = process.env.ELECTRON_COMPILE_ENV || process.env.NODE_ENV || 'development';
const info = readCompilerConfigSync(file, environment);

return createCompilerHostFromConfiguration({
appRoot: path.dirname(file),
Expand Down
Loading