Browse Source

Integrate the file watch and copy into the webpack file

Steven Silvester 7 years ago
parent
commit
7c9ab976b1

+ 1 - 4
MANIFEST.in

@@ -10,9 +10,6 @@ include package.json
 include setupbase.py
 
 include jupyterlab/package.app.json
-include jupyterlab/webpack.config.js
-include jupyterlab/index.app.js
-include jupyterlab/node-version-check.js
-include jupyterlab/released_packages.txt
+include jupyterlab/*.js
 
 prune jupyterlab/tests

+ 1 - 1
jupyterlab/make-release.js

@@ -10,7 +10,7 @@ childProcess.execSync('npm run build:prod', {stdio:[0,1,2]});
 // Update the package.app.json file.
 var data = require('./package.json');
 data['scripts']['build'] = 'webpack'
-data['scripts']['watch'] = 'webpack --watch'
+data['scripts']['watch'] = "concurrently \"webpack --watch\" \"node watch\""
 data['jupyterlab']['outputDir'] = '..';
 data['jupyterlab']['linkedPackages'] = {};
 text = JSON.stringify(sortPackageJson(data), null, 2) + '\n';

+ 1 - 0
jupyterlab/package.app.json

@@ -89,6 +89,7 @@
     "source-map-loader": "0.2.1",
     "style-loader": "^0.13.1",
     "url-loader": "^0.5.7",
+    "watch": "~1.0.2",
     "webpack": "^2.2.1"
   },
   "jupyterlab": {

+ 2 - 2
jupyterlab/package.json

@@ -89,6 +89,7 @@
     "source-map-loader": "0.2.1",
     "style-loader": "^0.13.1",
     "url-loader": "^0.5.7",
+    "watch": "~1.0.2",
     "webpack": "^2.2.1"
   },
   "jupyterlab": {
@@ -142,7 +143,6 @@
       "@phosphor/widgets"
     ],
     "version": "0.29.0.dev0",
-    "linkedPackages": {
-    }
+    "linkedPackages": {}
   }
 }

+ 0 - 46
jupyterlab/watch.js

@@ -1,46 +0,0 @@
-var watch = require('watch');
-var path = require('path');
-var fs = require('fs-extra');
-
-
-/**
- * Watch a package for changes and copy them to the local node_modules.
- */
-function watchPackage(packagePath) {
-  var data = require(path.join(packagePath, 'package.json'));
-  var targetBase = require.resolve(path.join(data.name, 'package.json'));
-  targetBase = path.dirname(targetBase);
-
-  var filter = function(f, stat) {
-    return f.split(path.sep).indexOf('node_modules') === -1;
-  }
-  var options = {
-    "ignoreDotFiles": true,
-    "filter": filter,
-    "interval": 1
-  }
-
-  watch.watchTree(packagePath, options, function (f, curr, prev) {
-    if (typeof f !== 'object' && curr !== null) {
-      var target = path.join(targetBase, f.slice(packagePath.length));
-      if (f === target) {
-        return;
-      }
-      if (curr.nlink === 0) {
-        fs.removeSync(target);
-      } else {
-        fs.copySync(f, target);
-      }
-    }
-  });
-}
-
-
-// Get the linked package paths an watch them.
-var jlab = require('./package.json').jupyterlab;
-var linkedPackages = {};
-Object.keys(jlab.linkedPackages).forEach(function (name) {
-  watchPackage(fs.realpathSync(jlab.linkedPackages[name]));
-});
-
-watchPackage(fs.realpathSync('../packages/console'))

+ 69 - 1
jupyterlab/webpack.config.js

@@ -4,6 +4,7 @@ var fs = require('fs-extra');
 var Handlebars = require('handlebars');
 var crypto = require('crypto');
 var package_data = require('./package.json');
+var watch = require('watch');
 var Build = require('@jupyterlab/buildutils').Build;
 
 // Ensure a clear build directory.
@@ -32,6 +33,61 @@ var result = template(data);
 
 fs.writeFileSync(path.resolve(buildDir, 'index.out.js'), result);
 
+
+/**
+ * Watch a package for changes and copy them to the local node_modules.
+ */
+function watchPackage(packagePath) {
+  packagePath = fs.realpathSync(packagePath);
+  var data = require(path.join(packagePath, 'package.json'));
+
+  try {
+    var targetBase = require.resolve(path.join(data.name, 'package.json'));
+  } catch (err) {
+    console.error(err);
+    return;
+  }
+  targetBase = fs.realpathSync(path.dirname(targetBase));
+
+  if (targetBase === packagePath) {
+    return;
+  }
+
+  var options = {
+    "ignoreDotFiles": true,
+    "interval": 0.5,
+    "filter": function(f, stat) {
+      return f.split(path.sep).indexOf('node_modules') === -1;
+    }
+  }
+
+  watch.watchTree(packagePath, options, function (f, curr, prev) {
+    if (typeof f !== 'object' && curr !== null) {
+      var target = path.join(targetBase, f.slice(packagePath.length));
+      if (curr.nlink !== 0) {
+        try {
+          console.log('copying', target);
+          fs.copySync(f, target);
+        } catch (err) {
+          console.error(err);
+        }
+      }
+    }
+  });
+}
+
+
+// Handle watch mode for the linked packages.
+var localPaths = [];
+if (process.argv.indexOf('--watch') !== -1) {
+  Object.keys(jlab.linkedPackages).forEach(function (name) {
+    watchPackage(jlab.linkedPackages[name]);
+    var localPath = require.resolve(path.join(name, 'package.json'));
+    localPaths.push(path.dirname(localPath));
+  });
+}
+
+
 // Create the hash
 var hash = crypto.createHash('md5');
 hash.update(fs.readFileSync('./package.json'));
@@ -64,7 +120,19 @@ module.exports = {
     ],
   },
   watchOptions: {
-    ignored: /node_modules/
+    ignored: function(search) {
+      // Limit the watched files to those in our local linked package dirs.
+      var ignore = true;
+      localPaths.forEach(function(localPath) {
+        if (search.slice(0, localPath.length) === localPath) {
+          var rest = search.slice(localPath.length);
+          if (rest.indexOf('node_modules') === -1) {
+            ignore = false;
+          }
+        }
+      });
+      return ignore;
+    }
   },
   node: {
     fs: 'empty'

+ 1 - 3
setupbase.py

@@ -89,9 +89,7 @@ def find_package_data():
         schema_dirs.append(pjoin(dir[slice_len:], '*'))
 
     return {
-        'jupyterlab': ['build/*', 'index.app.js',
-                       'webpack.config.js', 'package.app.json',
-                       'released_packages.txt', 'node-version-check.js'
+        'jupyterlab': ['build/*', '*.js', 'package.app.json'
                        ] + theme_dirs + schema_dirs
     }