瀏覽代碼

webpack games + genereted javascripts

Alexey Velikiy 5 年之前
父節點
當前提交
fd125afb81
共有 5 個文件被更改,包括 233 次插入6 次删除
  1. 2 1
      app.js
  2. 22 5
      build/webpack.dev.conf.js
  3. 13 0
      build/webpack.prod.conf.js
  4. 11 0
      docs/webpack_empty_context.js
  5. 185 0
      docs/webpack_express_view.js

+ 2 - 1
app.js

@@ -1,9 +1,10 @@
 import { MODULE_CONST } from './test_module'
-
 import * as express from 'express'
 
+
 var app = express();
 
+
 app.get('/', function(req, res) {
    res.send("Hello world!");
 });

+ 22 - 5
build/webpack.dev.conf.js

@@ -1,14 +1,31 @@
 const base = require('./webpack.base.conf');
+const webpack = require('webpack')
+
 
 base.mode = 'development';
 base.externals = [
   function(context, request, callback) {
-    if(request[0] == '.') {
-      callback();
-    } else {
-      callback(null, "require('" + request + "')");
+    
+    if(request.toString() === './view') {
+      console.log('context: ', context)
+      console.log('request: ', request.toString())
     }
+
+    // if(request[0] == '.') {
+      callback();
+    // } else {
+    //   callback(null, "require('" + request + "')");
+    // }
   }
-],
+]
+
+base.plugins = [
+  new webpack.ContextReplacementPlugin(
+    /\.\/node_modules\/express\/lib sync recursive/,
+    // Regular expression to match the files
+    // that should be imported
+    /ejs/
+  )
+]
 
 module.exports = base;

+ 13 - 0
build/webpack.prod.conf.js

@@ -1,5 +1,18 @@
 var base = require('./webpack.base.conf');
 
 base.mode = 'production';
+base.externals = [
+  function(context, request, callback) {
+
+    if(request.toString === './view') {
+      console.log('context: ', context)
+      console.log('request: ', request.toString())
+    }
+    
+    //callback(null, "require('" + request + "')");
+    callback();
+    
+  }
+],
 
 module.exports = base;

+ 11 - 0
docs/webpack_empty_context.js

@@ -0,0 +1,11 @@
+function webpackEmptyContext(req) {
+  var e = new Error("Cannot find module '" + req + "'");
+  e.code = 'MODULE_NOT_FOUND';
+  throw e;
+}
+webpackEmptyContext.keys = function() { return []; };
+webpackEmptyContext.resolve = webpackEmptyContext;
+module.exports = webpackEmptyContext;
+webpackEmptyContext.id = "./node_modules/express/lib sync recursive";
+
+//# sourceURL=webpack:///./node_modules/express/lib_sync?

+ 185 - 0
docs/webpack_express_view.js

@@ -0,0 +1,185 @@
+/*!
+ * express
+ * Copyright(c) 2009-2013 TJ Holowaychuk
+ * Copyright(c) 2013 Roman Shtylman
+ * Copyright(c) 2014-2015 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+
+
+/**
+ * Module dependencies.
+ * @private
+ */
+
+var debug = __webpack_require__(/*! debug */ "./node_modules/debug/src/index.js")('express:view');
+var path = __webpack_require__(/*! path */ "path");
+var fs = __webpack_require__(/*! fs */ "fs");
+
+/**
+ * Module variables.
+ * @private
+ */
+
+var dirname = path.dirname;
+var basename = path.basename;
+var extname = path.extname;
+var join = path.join;
+var resolve = path.resolve;
+
+/**
+ * Module exports.
+ * @public
+ */
+
+module.exports = View;
+
+/**
+ * Initialize a new `View` with the given `name`.
+ *
+ * Options:
+ *
+ *   - `defaultEngine` the default template engine name
+ *   - `engines` template engine require() cache
+ *   - `root` root path for view lookup
+ *
+ * @param {string} name
+ * @param {object} options
+ * @public
+ */
+
+function View(name, options) {
+  var opts = options || {};
+
+  this.defaultEngine = opts.defaultEngine;
+  this.ext = extname(name);
+  this.name = name;
+  this.root = opts.root;
+
+  if (!this.ext && !this.defaultEngine) {
+    throw new Error('No default engine was specified and no extension was provided.');
+  }
+
+  var fileName = name;
+
+  if (!this.ext) {
+    // get extension from default engine name
+    this.ext = this.defaultEngine[0] !== '.'
+      ? '.' + this.defaultEngine
+      : this.defaultEngine;
+
+    fileName += this.ext;
+  }
+
+  if (!opts.engines[this.ext]) {
+    // load engine
+    var mod = this.ext.substr(1)
+    debug('require "%s"', mod)
+
+    // default engine export
+    var fn = __webpack_require__("./node_modules/express/lib sync recursive")(mod).__express
+
+    if (typeof fn !== 'function') {
+      throw new Error('Module "' + mod + '" does not provide a view engine.')
+    }
+
+    opts.engines[this.ext] = fn
+  }
+
+  // store loaded engine
+  this.engine = opts.engines[this.ext];
+
+  // lookup path
+  this.path = this.lookup(fileName);
+}
+
+/**
+ * Lookup view by the given `name`
+ *
+ * @param {string} name
+ * @private
+ */
+
+View.prototype.lookup = function lookup(name) {
+  var path;
+  var roots = [].concat(this.root);
+
+  debug('lookup "%s"', name);
+
+  for (var i = 0; i < roots.length && !path; i++) {
+    var root = roots[i];
+
+    // resolve the path
+    var loc = resolve(root, name);
+    var dir = dirname(loc);
+    var file = basename(loc);
+
+    // resolve the file
+    path = this.resolve(dir, file);
+  }
+
+  return path;
+};
+
+/**
+ * Render with the given options.
+ *
+ * @param {object} options
+ * @param {function} callback
+ * @private
+ */
+
+View.prototype.render = function render(options, callback) {
+  debug('render "%s"', this.path);
+  this.engine(this.path, options, callback);
+};
+
+/**
+ * Resolve the file within the given directory.
+ *
+ * @param {string} dir
+ * @param {string} file
+ * @private
+ */
+
+View.prototype.resolve = function resolve(dir, file) {
+  var ext = this.ext;
+
+  // <path>.<ext>
+  var path = join(dir, file);
+  var stat = tryStat(path);
+
+  if (stat && stat.isFile()) {
+    return path;
+  }
+
+  // <path>/index.<ext>
+  path = join(dir, basename(file, ext), 'index' + ext);
+  stat = tryStat(path);
+
+  if (stat && stat.isFile()) {
+    return path;
+  }
+};
+
+/**
+ * Return a stat, maybe.
+ *
+ * @param {string} path
+ * @return {fs.Stats}
+ * @private
+ */
+
+function tryStat(path) {
+  debug('stat "%s"', path);
+
+  try {
+    return fs.statSync(path);
+  } catch (e) {
+    return undefined;
+  }
+}
+
+
+//# sourceURL=webpack:///./node_modules/express/lib/view.js?