Skip to content

Commit

Permalink
fix: 🐛 baseUrl is now not in the result config, if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimck committed Apr 9, 2021
1 parent 50d51ac commit 500901d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/lib/yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const argv = yargs
},
[CLIArgs.BASE_URL]: {
alias: 'b',
default: '.',
description: 'Custom base url used for paths generation',
type: 'string'
},
Expand Down
35 changes: 28 additions & 7 deletions src/parser/__tests__/webpackParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
}
);

Expand All @@ -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'
}
],
[
Expand All @@ -69,8 +72,11 @@ describe('extractPaths()', () => {
}
},
{
'shared/*': ['./=>packages/shared/src//*'],
'src/*': ['./=>src//*']
paths: {
'shared/*': ['./=>packages/shared/src//*'],
'src/*': ['./=>src//*']
},
baseUrl: './'
}
]
])(
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 1 addition & 3 deletions src/parser/templateParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ async function templateParser({ params, config }) {
...params,
template: extractTemplate(require(packageJsonPath)) || params.template
},
config: {
...config
}
config
};
}

Expand Down
57 changes: 31 additions & 26 deletions src/parser/webpackParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ 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<string, Array<string>>} 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])}/*`;

acc[pathKey] = [pathVal];

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) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 500901d

Please sign in to comment.