浏览代码

wip make the loader a typescript module

Steven Silvester 8 年之前
父节点
当前提交
34fcea5f9b
共有 4 个文件被更改,包括 90 次插入32 次删除
  1. 1 1
      jupyterlab/lab.html
  2. 0 31
      jupyterlab/loader.js
  3. 75 0
      jupyterlab/loader.ts
  4. 14 0
      jupyterlab/tsconfig.json

+ 1 - 1
jupyterlab/lab.html

@@ -44,7 +44,7 @@ Distributed under the terms of the Modified BSD License.
 
 {% block script %}
 <script>
-  var lab = jupyter.lab = jupyter.require("{{ jupyterlab_main }}");
+  var lab = window.jupyterlab = jupyter.require("{{ jupyterlab_main }}");
   var plugins = [];
 
   {% for plugin_entry in plugin_entries %}

+ 0 - 31
jupyterlab/loader.js

@@ -1,31 +0,0 @@
-// Copyright (c) Jupyter Development Team.
-// Distributed under the terms of the Modified BSD License.
-
-var ModuleLoader = require('@jupyterlab/extension-builder/lib/loader').ModuleLoader;
-var extractPlugins = require('@jupyterlab/extension-builder/lib/extract').extractPlugins;
-
-var loader = new ModuleLoader();
-var requireFunc = loader.require.bind(loader);
-
-
-/**
- * Get an entry point given by the user after validating.
- */
-function getEntryPoint(entryPoint) {
-  var plugins = requireFunc(entryPoint);
-  try {
-    plugins = extractPlugins(plugins);
-  } catch (err) {
-    console.error(err);
-    plugins = [];
-  }
-  return plugins;
-}
-
-
-module.exports = {
-  define: loader.define.bind(loader),
-  require: requireFunc,
-  requireBundle: loader.ensureBundle.bind(loader),
-  getEntryPoint: getEntryPoint
-}

+ 75 - 0
jupyterlab/loader.ts

@@ -0,0 +1,75 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import {
+  ModuleLoader
+} from '@jupyterlab/extension-builder/lib/loader';
+
+import {
+  extractPlugins
+} from '@jupyterlab/extension-builder/lib/extract';
+
+
+/**
+ * A module loader instance.
+ */
+const loader = new ModuleLoader();
+
+
+/**
+ * Synchronously require a module that has already been loaded.
+ *
+ * @param path - The semver-mangled fully qualified path of the module.
+ *   For example, "foo@^1.1.0/lib/bar/baz.js".
+ *
+ * @returns The exports of the requested module, if registered.  The module
+ *   selected is the registered module that maximally satisfies the semver
+ *   range of the request.
+ */
+export
+function require(path: string): ModuleLoader.IModule {
+  return loader.require.call(loader, mod);
+}
+
+
+/**
+ * Define a module that can be synchronously required.
+ *
+ * @param path - The version-mangled fully qualified path of the module.
+ *   For example, "foo@1.0.1/lib/bar/baz.js".
+ *
+ * @param callback - The callback function for invoking the module.
+ */
+export
+function define(path: string, callback: ModuleLoader.DefineCallback): void {
+  loader.define.call(loader, mod, callback);
+}
+
+
+/**
+ * Requre a bundle is to be loaded on a page.
+ *
+ * @param path - The public path of the bundle (e.g. "lab/jupyter.bundle.js").
+ *
+ * @returns A promise that resolves with the requested bundle.
+ */
+export
+function requireBundle(mod: string): Promise<ModuleLoader.IBundle> {
+  return loader.ensureBundle.call(loader, mod);
+}
+
+
+/**
+ * Get an entry point given by the user after validating.
+ */
+export
+function getEntryPoint(entryPoint: string): JupyterLabPlugin[] {
+  let plugins = require(entryPoint);
+  try {
+    plugins = extractPlugins(plugins);
+  } catch (err) {
+    console.error(err);
+    plugins = [];
+  }
+  return plugins as JupyterLabPlugin[];
+}

+ 14 - 0
jupyterlab/tsconfig.json

@@ -0,0 +1,14 @@
+{
+  "compilerOptions": {
+    "declaration": true,
+    "noImplicitAny": true,
+    "noEmitOnError": true,
+    "lib": ["dom", "es5", "es2015.collection", "es2015.promise"],
+    "types": [],
+    "module": "commonjs",
+    "moduleResolution": "node",
+    "target": "ES5",
+    "outDir": ".",
+    "sourceMap": true
+  }
+}