Parcourir la source

dynamic require breaks jest, disabling for now

telamonian il y a 5 ans
Parent
commit
3df4e9b0ae
1 fichiers modifiés avec 66 ajouts et 51 suppressions
  1. 66 51
      packages/ui-components/src/icon/icon.ts

+ 66 - 51
packages/ui-components/src/icon/icon.ts

@@ -1,68 +1,83 @@
-import { nameFromPath } from '../utils';
-
 /**
- * `require.context` polyfill for jest tests.
- * Modified from https://stackoverflow.com/a/42191018/425458.
+ * To add an icon to the defaultIconRegistry requires two lines of code:
+ *   1. import the icon's .svg
+ *
+ *   2. add a relevant entry to _defaultIcons
  */
-// This condition actually should detect if it's an Node environment
-if (typeof require.context === 'undefined') {
-  const fs = require('fs');
-  const path = require('path');
-
-  require.context = (base: string, deep?: boolean, filter?: RegExp): any => {
-    const files: { [key: string]: boolean } = Object.create(null);
-
-    function readDirectory(directory: string) {
-      fs.readdirSync(directory).forEach((file: string) => {
-        const fullPath = path.resolve(directory, file);
-
-        if (fs.statSync(fullPath).isDirectory()) {
-          if (deep) readDirectory(fullPath);
 
-          return;
-        }
+/* tslint:disable */
+import jupyterFaviconSvg from '../../style/icons/jupyter-favicon.svg';
 
-        if (!filter.test(fullPath)) return;
+// filetype icons
+import html5Svg from '../../style/icons/filetype/html5.svg';
+import reactSvg from '../../style/icons/filetype/react.svg';
 
-        files[fullPath] = true;
-      });
-    }
+// statusbar icons
+import kernelSvg from '../../style/icons/statusbar/kernel.svg';
+import lineFormSvg from '../../style/icons/statusbar/line-form.svg';
+import notTrustedSvg from '../../style/icons/statusbar/not-trusted.svg';
+import statusBarSvg from '../../style/icons/statusbar/status-bar.svg';
+import terminalSvg from '../../style/icons/statusbar/terminal.svg';
+import trustedSvg from '../../style/icons/statusbar/trusted.svg';
 
-    readDirectory(path.resolve(__dirname, base));
+// sidebar icons
+import buildSvg from '../../style/icons/sidebar/build.svg'; // originally ic-build-24px.svg
+import extensionSvg from '../../style/icons/sidebar/extension.svg'; // originally ic-extension-24px.svg
+import folderSvg from '../../style/icons/sidebar/folder.svg'; // originally ic-folder-24px.svg
+import paletteSvg from '../../style/icons/sidebar/palette.svg'; // originally ic-palette-24px.svg
+import runningSvg from '../../style/icons/sidebar/running.svg'; // originally stop-circle.svg
+import tabSvg from '../../style/icons/sidebar/tab.svg'; // originally ic-tab-24px.svg
+/* tslint:enable */
 
-    function Module(file: string) {
-      return require(file);
-    }
-
-    Module.keys = () => Object.keys(files);
+export namespace Icon {
+  export const defaultIcons: ReadonlyArray<IModel> = [
+    { name: 'jupyter-favicon', svg: jupyterFaviconSvg },
 
-    return Module;
-  };
-}
+    { name: 'html5', svg: html5Svg },
+    { name: 'react', svg: reactSvg },
 
-export namespace Icon {
-  /**
-   * Import all svgs from a directory. The input argument should be
-   * of the form `require.context('raw-loader!<path>', true, /\.svg$/)`.
-   * <path> should be a string literal path, as this is needed by `require`.
-   */
-  export function importSvgs(r: any): ReadonlyArray<IModel> {
-    return r.keys().reduce((svgs: IModel[], item: string, index: number) => {
-      const name = nameFromPath(item);
-      if (name !== 'bad') {
-        svgs.push({ name: name, svg: r(item).default });
-      }
-      return svgs;
-    }, []);
-  }
+    { name: 'kernel', svg: kernelSvg },
+    { name: 'line-form', svg: lineFormSvg },
+    { name: 'not-trusted', svg: notTrustedSvg },
+    { name: 'status-bar', svg: statusBarSvg },
+    { name: 'terminal', svg: terminalSvg },
+    { name: 'trusted', svg: trustedSvg },
 
-  export const defaultIcons: ReadonlyArray<IModel> = importSvgs(
-    require.context('raw-loader!../../style/icons', true, /\.svg$/)
-  );
+    { name: 'build', svg: buildSvg },
+    { name: 'extension', svg: extensionSvg },
+    { name: 'folder', svg: folderSvg },
+    { name: 'palette', svg: paletteSvg },
+    { name: 'running', svg: runningSvg },
+    { name: 'tab', svg: tabSvg }
+  ];
 
   export interface IModel {
     name: string;
     className?: string;
     svg: string;
   }
+
+  /**
+   * The dynamic import stuff is webpack only and breaks Jest,
+   * so it's turned off for now
+   */
+
+  // /**
+  //  * Import all svgs from a directory. The input argument should be
+  //  * of the form `require.context('raw-loader!<path>', true, /\.svg$/)`.
+  //  * <path> should be a string literal path, as this is needed by `require`.
+  //  */
+  // export function importSvgs(r: any): ReadonlyArray<IModel> {
+  //   return r.keys().reduce((svgs: IModel[], item: string, index: number) => {
+  //     const name = nameFromPath(item);
+  //     if (name !== 'bad') {
+  //       svgs.push({ name: name, svg: r(item).default });
+  //     }
+  //     return svgs;
+  //   }, []);
+  // }
+
+  // export const defaultIcons: ReadonlyArray<IModel> = importSvgs(
+  //   require.context('raw-loader!../../style/icons', true, /\.svg$/)
+  // );
 }