UNPKG

@kkt/scope-plugin-options

Version:

This will modify the CRA ModuleScopePlugin plugin that prevents to import modules from outside the `src` directory.

37 lines (36 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.disableScopePlugin = void 0; const regexp = /(ModuleScopePlugin)/; function disableScopePlugin(conf) { conf.resolve.plugins = conf.resolve?.plugins ?.map((plugin) => { if (plugin && plugin.constructor && plugin.constructor.name && regexp.test(plugin.constructor.name)) { return undefined; } return plugin; }) .filter(Boolean); return conf; } exports.disableScopePlugin = disableScopePlugin; function scopePluginOptions(conf, env, options) { if (!conf) { throw Error('KKT:@kkt/scope-plugin-options: there is no config found'); } const { allowedFiles, appSrcs, allowedPaths } = options || {}; const moduleScopePlugin = conf.resolve?.plugins?.find((plugin) => plugin?.constructor && plugin.constructor.name && regexp.test(plugin.constructor.name)); if (moduleScopePlugin && typeof moduleScopePlugin === 'object') { if (allowedFiles && Array.isArray(allowedFiles)) { allowedFiles.forEach((keyName) => moduleScopePlugin.allowedFiles.add(keyName)); } if (appSrcs && Array.isArray(appSrcs)) { appSrcs.forEach((keyName) => moduleScopePlugin.appSrcs.push(keyName)); } if (allowedPaths && Array.isArray(allowedPaths)) { allowedPaths.forEach((keyName) => moduleScopePlugin.allowedPaths.push(keyName)); } } return conf; } exports.default = scopePluginOptions;