source: frontend/node_modules/react-dev-utils/ModuleScopePlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[9af201e]1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10const chalk = require('chalk');
11const path = require('path');
12const os = require('os');
13
14class ModuleScopePlugin {
15 constructor(appSrc, allowedFiles = []) {
16 this.appSrcs = Array.isArray(appSrc) ? appSrc : [appSrc];
17 this.allowedFiles = new Set(allowedFiles);
18 this.allowedPaths = [...allowedFiles].map(path.dirname).filter(p => path.relative(p, process.cwd()) !== '');
19 }
20
21 apply(resolver) {
22 const { appSrcs } = this;
23 resolver.hooks.file.tapAsync(
24 'ModuleScopePlugin',
25 (request, contextResolver, callback) => {
26 // Unknown issuer, probably webpack internals
27 if (!request.context.issuer) {
28 return callback();
29 }
30 if (
31 // If this resolves to a node_module, we don't care what happens next
32 request.descriptionFileRoot.indexOf('/node_modules/') !== -1 ||
33 request.descriptionFileRoot.indexOf('\\node_modules\\') !== -1 ||
34 // Make sure this request was manual
35 !request.__innerRequest_request
36 ) {
37 return callback();
38 }
39 // Resolve the issuer from our appSrc and make sure it's one of our files
40 // Maybe an indexOf === 0 would be better?
41 if (
42 appSrcs.every(appSrc => {
43 const relative = path.relative(appSrc, request.context.issuer);
44 // If it's not in one of our app src or a subdirectory, not our request!
45 return relative.startsWith('../') || relative.startsWith('..\\');
46 })
47 ) {
48 return callback();
49 }
50 const requestFullPath = path.resolve(
51 path.dirname(request.context.issuer),
52 request.__innerRequest_request
53 );
54 if (this.allowedFiles.has(requestFullPath)) {
55 return callback();
56 }
57 if (this.allowedPaths.some((allowedFile) => {
58 return requestFullPath.startsWith(allowedFile);
59 })) {
60 return callback();
61 }
62 // Find path from src to the requested file
63 // Error if in a parent directory of all given appSrcs
64 if (
65 appSrcs.every(appSrc => {
66 const requestRelative = path.relative(appSrc, requestFullPath);
67 return (
68 requestRelative.startsWith('../') ||
69 requestRelative.startsWith('..\\')
70 );
71 })
72 ) {
73 const scopeError = new Error(
74 `You attempted to import ${chalk.cyan(
75 request.__innerRequest_request
76 )} which falls outside of the project ${chalk.cyan(
77 'src/'
78 )} directory. ` +
79 `Relative imports outside of ${chalk.cyan(
80 'src/'
81 )} are not supported.` +
82 os.EOL +
83 `You can either move it inside ${chalk.cyan(
84 'src/'
85 )}, or add a symlink to it from project's ${chalk.cyan(
86 'node_modules/'
87 )}.`
88 );
89 Object.defineProperty(scopeError, '__module_scope_plugin', {
90 value: true,
91 writable: false,
92 enumerable: false,
93 });
94 callback(scopeError, request);
95 } else {
96 callback();
97 }
98 }
99 );
100 }
101}
102
103module.exports = ModuleScopePlugin;
Note: See TracBrowser for help on using the repository browser.