Skip to content

Commit 175282e

Browse files
committed
make ssr files compatible with older node versions
1 parent 4cf710e commit 175282e

File tree

2 files changed

+92
-96
lines changed

2 files changed

+92
-96
lines changed

packages/vue-server-renderer/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
const { readFileSync } = require('fs')
2-
const createRenderer = require('./create-renderer')
3-
const Module = require('./module')
1+
var createRenderer = require('./create-renderer')
2+
var Module = require('./module')
43

54
function runAsNewModule (code) {
6-
const path = '__app__'
7-
const m = new Module(path, null, true /* isBundle */)
5+
var path = '__app__'
6+
var m = new Module(path, null, true /* isBundle */)
87
m.load(path)
98
m._compile(code, path)
109
return Object.prototype.hasOwnProperty.call(m.exports, 'default')
@@ -15,14 +14,14 @@ function runAsNewModule (code) {
1514
exports.createRenderer = createRenderer
1615

1716
exports.createBundleRenderer = function (code, options) {
18-
const renderer = createRenderer(options)
17+
var renderer = createRenderer(options)
1918
return {
20-
renderToString (cb) {
21-
const app = runAsNewModule(code)
19+
renderToString: function (cb) {
20+
var app = runAsNewModule(code)
2221
renderer.renderToString(app, cb)
2322
},
24-
renderToStream () {
25-
const app = runAsNewModule(code)
23+
renderToStream: function () {
24+
var app = runAsNewModule(code)
2625
return renderer.renderToStream(app)
2726
}
2827
}
Lines changed: 83 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// thanks to airbnb/hypernova
22

3-
const NativeModule = require('module')
4-
const path = require('path')
5-
const { ok } = require('assert')
6-
const { runInNewContext } = require('vm')
3+
var NativeModule = require('module')
4+
var path = require('path')
5+
var assert = require('assert')
6+
var vm = require('vm')
77

8-
const NativeModules = process.binding('natives')
8+
var NativeModules = process.binding('natives')
99

10-
const moduleExtensions = Object.assign({}, NativeModule._extensions)
10+
var moduleExtensions = Object.assign({}, NativeModule._extensions)
1111

1212
function isNativeModule (id) {
1313
return Object.prototype.hasOwnProperty.call(NativeModules, id)
1414
}
1515

1616
// Creates a sandbox so we don't share globals across different runs.
1717
function createContext () {
18-
const sandbox = {
18+
var sandbox = {
1919
Buffer,
2020
clearImmediate,
2121
clearInterval,
@@ -30,105 +30,102 @@ function createContext () {
3030
return sandbox
3131
}
3232

33-
// This class should satisfy the Module interface that NodeJS defines in their native m.js
34-
// implementation.
35-
class Module {
36-
constructor (id, parent, isBundle) {
37-
const cache = parent ? parent.cache : null
38-
this.id = id
39-
this.exports = {}
40-
this.cache = cache || {}
41-
this.parent = parent
42-
this.filename = null
43-
this.loaded = false
44-
this.context = parent ? parent.context : createContext()
45-
this.isBundle = isBundle
46-
}
47-
48-
load (filename) {
49-
ok(!this.loaded)
50-
this.filename = filename
51-
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
52-
}
33+
function Module (id, parent, isBundle) {
34+
var cache = parent ? parent.cache : null
35+
this.id = id
36+
this.exports = {}
37+
this.cache = cache || {}
38+
this.parent = parent
39+
this.filename = null
40+
this.loaded = false
41+
this.context = parent ? parent.context : createContext()
42+
this.isBundle = isBundle
43+
}
5344

54-
run (filename) {
55-
const ext = path.extname(filename)
56-
const extension = moduleExtensions[ext] ? ext : '.js'
57-
moduleExtensions[extension](this, filename)
58-
this.loaded = true
59-
}
45+
Module.prototype.load = function (filename) {
46+
assert.ok(!this.loaded)
47+
this.filename = filename
48+
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
49+
}
6050

61-
require (filePath) {
62-
ok(typeof filePath === 'string', 'path must be a string')
63-
return Module.loadFile(filePath, this)
64-
}
51+
Module.prototype.run = function (filename) {
52+
var ext = path.extname(filename)
53+
var extension = moduleExtensions[ext] ? ext : '.js'
54+
moduleExtensions[extension](this, filename)
55+
this.loaded = true
56+
}
6557

66-
_compile (content, filename) {
67-
const self = this
58+
Module.prototype.require = function (filePath) {
59+
assert.ok(typeof filePath === 'string', 'path must be a string')
60+
return Module.loadFile(filePath, this)
61+
}
6862

69-
function r (filePath) {
70-
return self.require(filePath)
71-
}
72-
r.resolve = request => NativeModule._resolveFilename(request, this)
73-
r.main = process.mainModule
74-
r.extensions = moduleExtensions
75-
r.cache = this.cache
63+
Module.prototype._compile = function (content, filename) {
64+
var self = this
7665

77-
const dirname = path.dirname(filename)
66+
function r (filePath) {
67+
return self.require(filePath)
68+
}
69+
r.resolve = request => NativeModule._resolveFilename(request, this)
70+
r.main = process.mainModule
71+
r.extensions = moduleExtensions
72+
r.cache = this.cache
7873

79-
// create wrapper function
80-
const wrapper = NativeModule.wrap(content)
74+
var dirname = path.dirname(filename)
8175

82-
const options = {
83-
filename,
84-
displayErrors: true
85-
}
76+
// create wrapper function
77+
var wrapper = NativeModule.wrap(content)
8678

87-
const compiledWrapper = runInNewContext(wrapper, this.context, options)
88-
return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
79+
var options = {
80+
filename,
81+
displayErrors: true
8982
}
9083

91-
static load (id, filename = id) {
92-
const m = new Module(id)
93-
m.load(filename)
94-
m.run(filename)
95-
return m
96-
}
84+
var compiledWrapper = vm.runInNewContext(wrapper, this.context, options)
85+
return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
86+
}
9787

98-
static loadFile (file, parent) {
99-
const filename = NativeModule._resolveFilename(file, parent)
88+
Module.load = function (id, filename) {
89+
var m = new Module(id)
90+
filename = filename || id
91+
m.load(filename)
92+
m.run(filename)
93+
return m
94+
}
10095

101-
if (parent) {
102-
const cachedModule = parent.cache[filename]
103-
if (cachedModule) return cachedModule.exports
104-
}
96+
Module.loadFile = function (file, parent) {
97+
var filename = NativeModule._resolveFilename(file, parent)
10598

106-
if (parent.isBundle || isNativeModule(filename)) {
107-
return require(filename)
108-
}
99+
if (parent) {
100+
var cachedModule = parent.cache[filename]
101+
if (cachedModule) return cachedModule.exports
102+
}
103+
104+
if (parent.isBundle || isNativeModule(filename)) {
105+
return require(filename)
106+
}
109107

110-
const m = new Module(filename, parent)
108+
var m = new Module(filename, parent)
111109

112-
m.cache[filename] = m
110+
m.cache[filename] = m
113111

114-
let hadException = true
112+
var hadException = true
115113

116-
try {
117-
m.load(filename)
118-
m.run(filename)
119-
hadException = false
120-
} finally {
121-
if (hadException) {
122-
delete m.cache[filename]
123-
}
114+
try {
115+
m.load(filename)
116+
m.run(filename)
117+
hadException = false
118+
} finally {
119+
if (hadException) {
120+
delete m.cache[filename]
124121
}
125-
126-
return m.exports
127122
}
128123

129-
static addExtension (ext, f) {
130-
moduleExtensions[ext] = f
131-
}
124+
return m.exports
125+
}
126+
127+
Module.addExtension = function (ext, f) {
128+
moduleExtensions[ext] = f
132129
}
133130

134131
module.exports = Module

0 commit comments

Comments
 (0)