Component docs can load test helpers

Most of the time we won't need this fully fledged environment, but for
some things (such as context menus, guess what I'm working on right now
:D) we do need a faked out environment to enable these features to run
when we run the component in isolation in the component docs.

Note: while this CL contains no component docs changes that take
advantage of it, I've tested locally with context menus in the data grid
and this change does work.

Fixed: 1148323
Change-Id: Ic8c508840a20b6d0f0e72fd7019a16271f04bea6
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2597313
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
diff --git a/scripts/component_server/server.js b/scripts/component_server/server.js
index b9a0aa9..32978a9 100644
--- a/scripts/component_server/server.js
+++ b/scripts/component_server/server.js
@@ -200,9 +200,27 @@
   } else {
     // This means it's an asset like a JS file or an image.
     const normalizedPath = normalizeImagePathIfRequired(filePath);
-    const fullPath = path.join(devtoolsFrontendFolder, normalizedPath);
 
-    if (!fullPath.startsWith(devtoolsFrontendFolder)) {
+    let fullPath = path.join(devtoolsFrontendFolder, normalizedPath);
+    if (fullPath.endsWith(path.join('locales', 'en-US.json'))) {
+      // Rewrite this path so we can load up the locale in the component-docs
+      fullPath = path.join(devtoolsFrontendFolder, 'i18n', 'locales', 'en-US.json');
+    }
+    /**
+     * Component docs can also load files from the test directory, so we ensure
+     * that we allow that here, and also then deal with the relative imports
+     * that come from the test directory, which will contain the front_end
+     * folder already. In which case we take the devToolsFrontendFolder, and go
+     * up one directory with '..' to make sure we import the right file from the
+     * right place.
+     */
+    const fileIsInTestFolder = normalizedPath.startsWith('/test/');
+    const fileStartsWithFrontEnd = normalizedPath.startsWith('/front_end/');
+    if (fileIsInTestFolder || fileStartsWithFrontEnd) {
+      fullPath = path.join(devtoolsFrontendFolder, '..', normalizedPath);
+    }
+
+    if (!fullPath.startsWith(devtoolsFrontendFolder) && !fileIsInTestFolder) {
       console.error(`Path ${fullPath} is outside the DevTools Frontend root dir.`);
       process.exit(1);
     }