Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set property 'webpackStats' of undefined #282

Closed
cexoso opened this issue Mar 16, 2018 · 1 comment
Closed

Cannot set property 'webpackStats' of undefined #282

cexoso opened this issue Mar 16, 2018 · 1 comment

Comments

@cexoso
Copy link

cexoso commented Mar 16, 2018

  • Operating System: MacOS
  • Node Version: v8.4.0
  • NPM Version: 5.6.0
  • webpack version: 4.1.1
  • webpack-dev-middleware Version: 1.12.0
  • [*] This is a bug

Code

var webpack = require('webpack')
var path = require('path')
module.exports = {
    name: 'backend dev hot middlware',
    mode: "development",
    entry: [
        'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
        './src/app/index.js'
    ],
    output: {
        path: path.join(__dirname, '/public/static'),
        filename: '[name].js',
        chunkFilename: '[id].chunk.js',
        publicPath: '/build/'
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        rules: [
            {
                test: /\.jsx|.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['env']
                }
            }
        ]
    }
}
#!/usr/bin/env node
console.log('start babel env ...')
require('babel-polyfill')
require('babel-core/register')
console.log('Waiting for webpacking ...')
var Koa = require('koa')
var app = new Koa();
var { devMiddleware, hotMiddleware } = require('koa-webpack-middleware')
var chokidar = require('chokidar')
var webpack = require('webpack')
var webpackConfig = require('../webpack.config')
var path = require('path')
var { getPortPromise } = require('portfinder')
var applyMiddlewares = require('../src/server/applyMiddlewares').default;
var compiler = webpack(webpackConfig);
app.use(
    devMiddleware(compiler, {
        noInfo: true,
        watchOptions: {
            aggregateTimeout: 300,
            poll: true
        },
        publicPath: '/build/',
        stats: {
            colors: true
        },
        serverSideRender: true 
    })
);
app.use(
    hotMiddleware(compiler, {
        log: console.log,
        path: '/__webpack_hmr',
        heartbeat: 10 * 1000
    })
);
applyMiddlewares(app);
app.use((ctx, next) => {
    const  assetsByChunkName = ctx.res.locals.webpackStats.toJson().assetsByChunkName
    console.log(assetsByChunkName.compilation.Compilation.inputFileSystem)
    // then use `assetsByChunkName` for server-sider rendering
})
app.on('error', function (err, ctx) {
    console.log('error occured:', err.stack)
})

var server = require('http').createServer(app.callback())

var watcher = chokidar.watch([
    path.join(__dirname, '../src/server')
])
watcher.on('ready', function () {
    watcher.on('all', function (e, p) {
        console.log("Clearing module cache");
        Object.keys(require.cache).forEach(function (id) {
            if (/[\/\\](server)[\/\\]/.test(id)) delete require.cache[id];
        });
    })
})
var isListened = false
compiler.plugin("emit", function (compilation, callback) {
    callback()
    if (!isListened) {
        getPortPromise().then(
            port => server.listen(port, function () {
                console.log('App started, at port %d', port)
                isListened = true
            }),
            (err) => {
                console.log('Could not get a free port', err);
                process.exit(-1);
            }
        )
    }
});

Expected Behavior

I want to get assetsByChunkName.

Actual Behavior

error occured: TypeError: Cannot set property 'webpackStats' of undefined
at /Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/webpack-dev-middleware/middleware.js:30:30
at Object.ready (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/webpack-dev-middleware/lib/Shared.js:166:29)
at /Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/webpack-dev-middleware/middleware.js:29:12
at Promise ()
at goNext (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/webpack-dev-middleware/middleware.js:28:11)
at webpackDevMiddleware (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/webpack-dev-middleware/middleware.js:41:33)
at _callee$ (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/koa-webpack-middleware/lib/devMiddleware.js:23:22)
at tryCatch (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/regenerator-runtime/runtime.js:65:40)
at Generator.invoke [as _invoke] (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/regenerator-runtime/runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (/Users/xiongjie/Documents/GitHub/react_isomorphism/node_modules/regenerator-runtime/runtime.js:117:21)

@shellscape
Copy link
Contributor

You'll need to provide a repo that we'll be able to reproduce this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants