diff --git a/package.json b/package.json index 6edad30..7ed6d3f 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.js b/src/index.js index 7923416..387ed3d 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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); } diff --git a/test/index.spec.js b/test/index.spec.js index d786e6a..d26a33a 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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' ] }); } @@ -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'); diff --git a/test/webpack.config.babel.js b/test/webpack.config.babel.js new file mode 100644 index 0000000..cc8d928 --- /dev/null +++ b/test/webpack.config.babel.js @@ -0,0 +1,10 @@ + +import path from 'path'; + +export default { + resolve: { + alias: { + 'my-absolute-test-lib': path.join(__dirname, 'assets/le-test-lib') + } + } +}; diff --git a/test/webpack.config.js b/test/webpack.config.js new file mode 100644 index 0000000..2fddeaf --- /dev/null +++ b/test/webpack.config.js @@ -0,0 +1,10 @@ + +var path = require('path'); + +module.exports = { + resolve: { + alias: { + 'my-absolute-test-lib': path.join(__dirname, 'assets/le-test-lib') + } + } +};