From 500901dafa6666f563a5cc7eb4fc4746a4629524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0ime=C4=8Dek?= Date: Sat, 10 Apr 2021 00:11:36 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20baseUrl=20is=20now=20not?= =?UTF-8?q?=20in=20the=20result=20config,=20if=20not=20provided?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/yargs.js | 1 - src/parser/__tests__/webpackParser.test.js | 35 ++++++++++--- src/parser/templateParser.js | 4 +- src/parser/webpackParser.js | 57 ++++++++++++---------- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/src/lib/yargs.js b/src/lib/yargs.js index 42c8e12..556d428 100644 --- a/src/lib/yargs.js +++ b/src/lib/yargs.js @@ -26,7 +26,6 @@ const argv = yargs }, [CLIArgs.BASE_URL]: { alias: 'b', - default: '.', description: 'Custom base url used for paths generation', type: 'string' }, diff --git a/src/parser/__tests__/webpackParser.test.js b/src/parser/__tests__/webpackParser.test.js index 35ae73e..f662eda 100644 --- a/src/parser/__tests__/webpackParser.test.js +++ b/src/parser/__tests__/webpackParser.test.js @@ -44,9 +44,9 @@ describe('extractPaths()', () => { {} ] ])( - 'should return null for %j webpack conf and %j config', + 'should return {} for %j webpack conf and %j config', (webpackConf, config) => { - expect(extractPaths(webpackConf, config)).toBeNull(); + expect(extractPaths(webpackConf, config)).toStrictEqual({}); } ); @@ -58,8 +58,11 @@ describe('extractPaths()', () => { } }, { - 'shared/*': ['./lib/src=>packages/shared/src//*'], - 'src/*': ['./lib/src=>src//*'] + paths: { + 'shared/*': ['./lib/src=>packages/shared/src//*'], + 'src/*': ['./lib/src=>src//*'] + }, + baseUrl: './lib/src' } ], [ @@ -69,8 +72,11 @@ describe('extractPaths()', () => { } }, { - 'shared/*': ['./=>packages/shared/src//*'], - 'src/*': ['./=>src//*'] + paths: { + 'shared/*': ['./=>packages/shared/src//*'], + 'src/*': ['./=>src//*'] + }, + baseUrl: './' } ] ])( @@ -139,6 +145,21 @@ describe('parseWebpackConf()', () => { } ); + it('should not append baseUrl when there are no paths', async () => { + expect( + await parseWebpackConf( + { resolve: {} }, + { + webpackConfigLocation: 'webpack.conf.js' + }, + {} + ) + ).toStrictEqual({ + config: {}, + params: { webpackConfigLocation: 'webpack.conf.js' } + }); + }); + it('should correctly resolve duplicate paths with array of webpack configs', async () => { expect( await parseWebpackConfFactory([ @@ -173,7 +194,7 @@ describe('parseWebpackConf()', () => { ['function', () => ({})], ['array', [{}, { resolve: {} }, { resolve: { alias: {} } }]] ])( - 'should options without paths when there are no aliases defined for %s-type webpack config', + 'should work when there are no aliases defined for %s-type webpack config', async (type, webpackConf) => { expect(await parseWebpackConfFactory(webpackConf)).toStrictEqual({ config: { diff --git a/src/parser/templateParser.js b/src/parser/templateParser.js index 654a3e2..ae22b8b 100644 --- a/src/parser/templateParser.js +++ b/src/parser/templateParser.js @@ -52,9 +52,7 @@ async function templateParser({ params, config }) { ...params, template: extractTemplate(require(packageJsonPath)) || params.template }, - config: { - ...config - } + config }; } diff --git a/src/parser/webpackParser.js b/src/parser/webpackParser.js index d6b5488..67872f0 100644 --- a/src/parser/webpackParser.js +++ b/src/parser/webpackParser.js @@ -6,20 +6,19 @@ const MOCK_ARGS = [process?.env?.NODE_ENV ?? 'development', '']; /** * Generates path aliases from provided webpack config. * - * @param {object} webpackConf Webpack config. - * @param {object} config Config object. - * @return {object>} Object with - * defined path aliases. + * @param {Object} webpackConf Webpack config. + * @param {Object} config Config object. + * @return {Object} Paths object and baseUrl. */ function extractPaths(webpackConf, config) { - const { baseUrl } = config?.compilerOptions ?? {}; + const baseUrl = config?.compilerOptions?.baseUrl ?? '.'; const { alias } = webpackConf?.resolve ?? {}; if (!baseUrl || !alias || Object.keys(alias).length === 0) { - return null; + return {}; } - return Object.keys(alias).reduce((acc, cur) => { + const paths = Object.keys(alias).reduce((acc, cur) => { const pathKey = `${cur}/*`; const pathVal = `${path.relative(baseUrl, alias[cur])}/*`; @@ -27,14 +26,16 @@ function extractPaths(webpackConf, config) { return acc; }, {}); + + return { paths, baseUrl }; } /** * Extracts path aliases from provided webpack config. * - * @param {object} webpackConf Webpack config. - * @param {object} params Params object. - * @param {object} config Config object. + * @param {Object} webpackConf Webpack config. + * @param {Object} params Params object. + * @param {Object} config Config object. * @return {Promise<{ params, config }>} Modified params and config objects. */ async function parseWebpackConf(webpackConf, params, config) { @@ -62,31 +63,35 @@ async function parseWebpackConf(webpackConf, params, config) { ); } - const paths = Array.isArray(parsedWebpackConf) - ? parsedWebpackConf.reduce( - (acc, currentConfig) => ({ - ...acc, - ...extractPaths(currentConfig, config) - }), - {} - ) + const { paths, baseUrl } = Array.isArray(parsedWebpackConf) + ? parsedWebpackConf.reduce((acc, currentConfig) => { + const extractedPaths = extractPaths(currentConfig, config); + + return { + paths: { + ...acc.paths, + ...extractedPaths.paths + }, + baseUrl: extractedPaths.baseUrl + }; + }, {}) : extractPaths(parsedWebpackConf, config); - const result = { + if (!paths || Object.keys(paths).length === 0) { + return { params, config }; + } + + return { params, config: { ...config, compilerOptions: { - ...config.compilerOptions + ...config.compilerOptions, + baseUrl, + paths } } }; - - if (paths && Object.keys(paths).length > 0) { - result.config.compilerOptions.paths = paths; - } - - return result; } /**