refactor: use path lib to make work across OS & add test

This commit is contained in:
Josh 2022-10-24 10:08:36 +01:00
parent 1c3c3c1071
commit 65351187e4
3 changed files with 77 additions and 7 deletions

View File

@ -0,0 +1,35 @@
'use strict';
const createPluginsExcludePath = require('../create-plugins-exclude-path');
describe('createPluginsExcludePath', () => {
test('given there are no plugins it should just return the node_modules regexp', () => {
const result = createPluginsExcludePath([]);
expect(result).toEqual(/node_modules/);
});
test('given there are only local plugins, it should just return the node_module regex', () => {
const result = createPluginsExcludePath(['strapi/packages/core/upload']);
expect(result).toEqual(/node_modules/);
});
test('given there are node_module plugins, it should return a regex with these included', () => {
const result = createPluginsExcludePath([
'/node_modules/strapi-plugin-custom-upload',
'/node_modules/strapi-plugin-custom-plugin',
]);
expect(result).toEqual(
/node_modules\/(?!(strapi-plugin-custom-upload|strapi-plugin-custom-plugin))/
);
});
test('given there are scoped node_module plugins, it should return a regex with these included', () => {
const result = createPluginsExcludePath([
'/node_modules/@scope/strapi-plugin-custom-upload',
'/node_modules/@scope/strapi-plugin-custom-plugin',
]);
expect(result).toEqual(
/node_modules\/(?!(@scope\/strapi-plugin-custom-upload|@scope\/strapi-plugin-custom-plugin))/
);
});
});

View File

@ -0,0 +1,40 @@
'use strict';
const { sep, join } = require('path');
const NODE_MODULES = 'node_modules';
/**
* @param {string[]} pluginsPath an array of paths to the plugins from the user's directory
* @returns {RegExp} a regex that will exclude _all_ node_modules except for the plugins in the pluginsPath array.
*/
const createPluginsExcludePath = (pluginsPath = []) => {
/**
* converts the full path to just the plugin path
* e.g. `/Users/username/strapi/node_modules/@scope/plugin-name`
* to `@scope/plugin-name`
*/
const tsxPlugins = pluginsPath.reduce((acc, curr) => {
const dirPaths = curr.split(sep);
const nodeModulePathIndex = dirPaths.findIndex((val) => val === NODE_MODULES);
if (nodeModulePathIndex > 0) {
const pluginNodeModulePath = dirPaths.slice(nodeModulePathIndex + 1);
return [...acc, join(...pluginNodeModulePath)];
}
return acc;
}, []);
/**
* If there aren't any plugins in the node_modules array, just return the node_modules regex
* without complicating it.
*/
if (tsxPlugins.length === 0) {
return /node_modules/;
}
return new RegExp(`${NODE_MODULES}/(?!(${tsxPlugins.join('|')}))`);
};
module.exports = createPluginsExcludePath;

View File

@ -13,6 +13,7 @@ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'
const alias = require('./webpack.alias');
const getClientEnvironment = require('./env');
const createPluginsExcludePath = require('./utils/create-plugins-exclude-path');
const EE_REGEX = /from.* ['"]ee_else_ce\//;
@ -49,13 +50,7 @@ module.exports = ({
]
: [];
/**
* converts the full path to just the plugin path
* e.g. `/Users/username/strapi/node_modules/@scope/plugin-name`
* to `@scope/plugin-name`
*/
const tsxPlugins = pluginsPath.map((dir) => dir.split('node_modules/')[1]).join('|');
const excludeRegex = new RegExp(`node_modules/(?!(${tsxPlugins}))`);
const excludeRegex = createPluginsExcludePath(pluginsPath);
return {
mode: isProduction ? 'production' : 'development',