Alexey Velikiy 5 năm trước cách đây
mục cha
commit
2e2e7ada93
5 tập tin đã thay đổi với 35 bổ sung19 xóa
  1. 7 1
      app.js
  2. 19 18
      build/webpack.dev.conf.js
  3. 6 0
      package-lock.json
  4. 1 0
      package.json
  5. 2 0
      template.ejs

+ 7 - 1
app.js

@@ -1,9 +1,11 @@
 import { MODULE_CONST } from './test_module'
 import * as express from 'express'
+import { resolve } from 'path'
 
 
 var app = express();
-
+app.set('view engine', 'ejs');
+app.set('views', resolve(__dirname, '..'));
 
 app.get('/', function(req, res) {
    res.send("Hello world!");
@@ -13,4 +15,8 @@ app.get('/test-module', function(req, res) {
   res.send(MODULE_CONST);
 });
 
+app.get('/template', function(req, res) {
+  res.render('template');
+});
+
 app.listen(3000);

+ 19 - 18
build/webpack.dev.conf.js

@@ -1,31 +1,32 @@
 const base = require('./webpack.base.conf');
 const webpack = require('webpack')
+const path = require('path')
+
+
+function resolve(p) {
+  return path.resolve(__dirname, './../', p);
+}
 
 
 base.mode = 'development';
 base.externals = [
   function(context, request, callback) {
-    
-    if(request.toString() === './view') {
-      console.log('context: ', context)
-      console.log('request: ', request.toString())
-    }
-
-    // if(request[0] == '.') {
+    if(request[0] == '.') {
       callback();
-    // } else {
-    //   callback(null, "require('" + request + "')");
-    // }
+    } 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/
-  )
-]
+// base.plugins = [
+//   new webpack.ContextReplacementPlugin(
+//     /express\/lib/,
+//     resolve('node_modules'),
+//     {
+//       'ejs': 'lodash'
+//     }
+//   )
+// ]
 
 module.exports = base;

+ 6 - 0
package-lock.json

@@ -1102,6 +1102,12 @@
       "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=",
       "dev": true
     },
+    "ejs": {
+      "version": "2.6.2",
+      "resolved": "https://registry.npmjs.org/ejs/-/ejs-2.6.2.tgz",
+      "integrity": "sha512-PcW2a0tyTuPHz3tWyYqtK6r1fZ3gp+3Sop8Ph+ZYN81Ob5rwmbHEzaqs10N3BEsaGTkh/ooniXK+WwszGlc2+Q==",
+      "dev": true
+    },
     "elliptic": {
       "version": "6.5.0",
       "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.0.tgz",

+ 1 - 0
package.json

@@ -26,6 +26,7 @@
   },
   "homepage": "https://github.com/CorpGlory/express-webpack-example#readme",
   "devDependencies": {
+    "ejs": "^2.6.2",
     "express": "^4.17.1",
     "webpack": "^4.39.1",
     "webpack-cli": "^3.3.6"

+ 2 - 0
template.ejs

@@ -0,0 +1,2 @@
+
+Hey from EJS <%= new Date() %> template