fix: memory leak when frequent rebuild in watch mode

This commit is contained in:
Clark Du 2017-12-10 21:54:38 +08:00 committed by Pooya Parsa
parent 0b9cd1b408
commit 29df01f16e
4 changed files with 21 additions and 4 deletions

View File

@ -9,7 +9,7 @@ import { join, resolve, basename, extname, dirname } from 'path'
import MFS from 'memory-fs'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import { r, wp, wChunk, createRoutes, sequence, relativeTo, isPureObject, waitFor } from 'utils'
import { r, wp, wChunk, createRoutes, sequence, relativeTo, isPureObject, waitFor, rmCache } from 'utils'
import Debug from 'debug'
import Glob from 'glob'
import clientWebpackConfig from './webpack/client.config.js'
@ -475,7 +475,11 @@ export default class Builder {
/* istanbul ignore if */
if (err) return reject(err)
// not keep modified or deleted items in Vue.prototype
Object.keys(require.cache).forEach(key => !/\.node$/.test(key) && delete require.cache[key])
Object.keys(require.cache).forEach(key => {
if (!/(node_modules\/postcss)|(\.node$)/.test(key)) {
rmCache(key)
}
})
})
)
return

View File

@ -19,7 +19,7 @@ export default function webpackBaseConfig({ name, isServer }) {
const config = {
name,
devtool: this.options.dev ? 'cheap-module-source-map' : false,
devtool: this.options.dev ? 'cheap-module-eval-source-map' : false,
entry: {
app: null
},

View File

@ -23,7 +23,7 @@ export default function webpackServerConfig() {
config = Object.assign(config, {
target: 'node',
node: false,
devtool: 'source-map',
devtool: this.options.dev ? config.devtool : 'source-map',
entry: resolve(this.options.buildDir, 'server.js'),
output: Object.assign({}, config.output, {
filename: 'server-bundle.js',

View File

@ -268,3 +268,16 @@ export function createRoutes(files, srcDir) {
})
return cleanChildrenRoutes(routes)
}
export function rmCache(path) {
const mod = require.cache[path]
delete require.cache[path]
if (mod.parent && mod.parent.children) {
for (let i = 0; i < mod.parent.children.length; i++) {
if (mod.parent.children[i] === mod) {
mod.parent.children.splice(i, 1)
break
}
}
}
}