Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Added tests for default config files and support es6 webpack configur…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
adriantoine committed Aug 8, 2016
1 parent 4a19841 commit ec9a154
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"dependencies": {
"babel-types": "^6.5.2",
"find-up": "^1.1.2",
"lodash.some": "^4.5.1",
"lodash.template": "^4.3.0"
}
}
43 changes: 31 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@
import { join, resolve, relative, isAbsolute, dirname } from 'path';
import { StringLiteral } from 'babel-types';
import template from 'lodash.template';
import some from 'lodash.some';
import findUp from 'find-up';

function getConfig(configPath, findConfig) {
// Compile config using environment variables
const compiledConfigPath = template(configPath)(process.env);
const DEFAULT_CONFIG_NAMES = ['webpack.config.js', 'webpack.config.babel.js'];

var conf;
if(!findConfig) {
// Get webpack config
conf = require(resolve(process.cwd(), compiledConfigPath));
} else {
conf = require(findUp.sync(compiledConfigPath));
function getConfig(configPaths, findConfig) {
let conf = null;

// Try all config paths and return for the first found one
some(configPaths, (configPath) => {
if(!configPath) return;

// Compile config using environment variables
const compiledConfigPath = template(configPath)(process.env);

if(!findConfig) {
// Get webpack config
conf = require(resolve(process.cwd(), compiledConfigPath));
} else {
conf = require(findUp.sync(compiledConfigPath));
}

return conf;
});

// In the case the webpack config is an es6 config, we need to get the default
if (conf && conf.__esModule && conf.default) {
conf = conf.default;
}

return conf;
Expand All @@ -22,13 +38,16 @@ function getConfig(configPath, findConfig) {
export default function({ types: t }) {
return {
visitor: {
CallExpression(path, { file: { opts: { filename: filename } }, opts: { config: configPath = 'webpack.config.js', findConfig: findConfig = false } = {} }) {
CallExpression(path, { file: { opts: { filename: filename } }, opts: { config: configPath, findConfig: findConfig = false } = {} }) {

// Get webpack config
const conf = getConfig(configPath, findConfig);
const conf = getConfig(
configPath ? [configPath, ...DEFAULT_CONFIG_NAMES] : DEFAULT_CONFIG_NAMES,
findConfig
);

// If the config comes back as null, we didn't find it, so throw an exception.
if(conf === null) {
if(!conf) {
throw new Error('Cannot find configuration file: ' + configPath);
}

Expand Down
16 changes: 14 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import test from 'ava';
import { readFileSync } from 'fs';
import { resolve } from 'path';

function transformFile(path, configuration = { config: './runtime.webpack.config.js' }) {
function transformFile(path, configuration) {
return babel.transformFileSync(resolve(__dirname, path), {
plugins: [
['../../src/index.js', configuration]
configuration ? ['../../src/index.js', configuration] : '../../src/index.js'
]
});
}
Expand All @@ -16,6 +16,18 @@ function read(path) {
return readFileSync(resolve(__dirname, path), 'utf8');
}

test('basic require with default webpack name', t => {
const actual = transformFile('fixtures/basic.absolute.js').code;
const expected = read('fixtures/basic.expected.js');
t.is(actual, expected);
});

test('basic require with es6 webpack config', t => {
const actual = transformFile('fixtures/basic.absolute.js', {config: './webpack.config.babel.js'}).code;
const expected = read('fixtures/basic.expected.js');
t.is(actual, expected);
});

test('basic require with the absolute resolve path', t => {
const actual = transformFile('fixtures/basic.absolute.js', {config: './runtime.webpack.config.js'}).code;
const expected = read('fixtures/basic.expected.js');
Expand Down
10 changes: 10 additions & 0 deletions test/webpack.config.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import path from 'path';

export default {
resolve: {
alias: {
'my-absolute-test-lib': path.join(__dirname, 'assets/le-test-lib')
}
}
};
10 changes: 10 additions & 0 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

var path = require('path');

module.exports = {
resolve: {
alias: {
'my-absolute-test-lib': path.join(__dirname, 'assets/le-test-lib')
}
}
};

0 comments on commit ec9a154

Please # to comment.