How to use CommonsChunkPlugin with target: ‘node’?

I have the following webpack config:

var glob = require('glob');
var path = require('path');
var webpack = require('webpack');
var _ = require('lodash');

var config = require('./webpack.config.server');
var dirs = require('../dirs');

var testFiles = glob.sync(path.join(__dirname, '../app/**/__tests__/*.js'));

module.exports = _.assign({}, config, {
  context: path.join(__dirname),
  target: 'node',
  devtool: 'source-map',
  entry: {
    server: '../app/temp.js',
    'server-test': testFiles,
  },
  output: _.assign({}, config.output, {
    filename: '[name].bundle.js',
  }),
  watch: true,
  plugins: (config.plugins || []).concat([
    new webpack.optimize.CommonsChunkPlugin({
      name: 'server',
      minChunks: Infinity,
    }),
  ]),
});

The server test bundle output is like this. But requiring this into node doesn’t run its entry point. Is this even supposed to work?

exports.ids = [1];
exports.modules = [
/* 0 */
/***/ function(module, exports, __webpack_require__) {

    module.exports = __webpack_require__(3);


/***/ },
/* 1 */,
/* 2 */,
/* 3 */
/***/ function(module, exports, __webpack_require__) {

    'use strict';

    var _Util = __webpack_require__(4);

    var _AppConstants = __webpack_require__(5);

    describe('pageTimestamps', function (done) {
      it('works for same aligned start/end time', function () {
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize)).toEqual([]);
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize, true)).toEqual([_AppConstants.PageSize]);
      });
      it('works for same misaligned start/end time', function () {
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize + 1, _AppConstants.PageSize + 1)).toEqual([_AppConstants.PageSize]);
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize + 1, _AppConstants.PageSize + 1, true)).toEqual([_AppConstants.PageSize]);
      });
      it('works for aligned pages', function () {
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize * 2)).toEqual([_AppConstants.PageSize]);
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize * 2, true)).toEqual([_AppConstants.PageSize, _AppConstants.PageSize * 2]);
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize * 3)).toEqual([_AppConstants.PageSize, _AppConstants.PageSize * 2]);
        expect((0, _Util.pageTimestamps)(_AppConstants.PageSize, _AppConstants.PageSize * 4)).toEqual([_AppConstants.PageSize, _AppConstants.PageSize * 2, _AppConstants.PageSize * 3]);
      });
    });

/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {

    'use strict';

    Object.defineProperty(exports, '__esModule', {
      value: true
    });
    exports.defaultPodId = defaultPodId;
    exports.pageTimestamp = pageTimestamp;
    exports.pageTimestamps = pageTimestamps;
    exports.getOrCreate = getOrCreate;
    exports.isAdminUser = isAdminUser;
    exports.authError = authError;
    exports.isNumAvail = isNumAvail;
    exports.capitalizeFirstLetter = capitalizeFirstLetter;

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

    var _lodash = __webpack_require__(1);

    var _lodash2 = _interopRequireDefault(_lodash);

    var _utilAppConstants = __webpack_require__(5);

    function defaultPodId() {
      return Meteor.user() && Meteor.user().profile.defaultPodId;
    }

    function pageTimestamp(time) {
      var iTime = parseInt(time);
      return iTime - iTime %% _utilAppConstants.PageSize;
    }

    function pageTimestamps(beginTime, endTime, endInclusive) {
      var lastTimestamp = endInclusive ? pageTimestamp(endTime + _utilAppConstants.PageSize) : pageTimestamp(endTime + _utilAppConstants.PageSize - 1);
      return _lodash2['default'].range(pageTimestamp(beginTime), lastTimestamp, _utilAppConstants.PageSize);
    }

    function getOrCreate(object, key, defaultValue) {
      var value = object[key];
      if (!value) {
        if (defaultValue === undefined) defaultValue = {};
        value = object[key] = defaultValue;
      }
      return value;
    }

    function isAdminUser(userId) {
      if (typeof userId === 'undefined') userId = Meteor.userId();
      return Roles.userIsInRole(userId, 'admin');
    }

    function authError() {
      throw new Meteor.Error('not-authorized');
    }

    function isNumAvail(num) {
      return typeof num === 'number' && !isNaN(num);
    }

    function capitalizeFirstLetter(str) {
      check(str, String);
      return str.length == 0 ? '' : str.charAt(0).toUpperCase() + str.slice(1);
    }

/***/ },
/* 5 */
/***/ function(module, exports) {

    'use strict';

    Object.defineProperty(exports, '__esModule', {
      value: true
    });
    var HistoricalCaptureInterval = 10000;

    exports.HistoricalCaptureInterval = HistoricalCaptureInterval;
    var NATime = 10 * 1000;exports.NATime = NATime;
    // 10 seconds
    var TimeoutCheckInterval = 1000;

    exports.TimeoutCheckInterval = TimeoutCheckInterval;
    var PageSize = 1000 * 60 * 60;exports.PageSize = PageSize;
    // 1 hour

    var CHANNEL_ID_SEPARATOR = '^';
    exports.CHANNEL_ID_SEPARATOR = CHANNEL_ID_SEPARATOR;

/***/ }
];;
//# sourceMappingURL=server-test.bundle.js.map

Author: Fantashit

2 thoughts on “How to use CommonsChunkPlugin with target: ‘node’?

  1. You must not use the CommonsChunkPlugin for node.js. The CommonsChunkPlugin is a web optimization (better caching). It makes no sense for node.js.

  2. Finding this issue helped me a lot. Because of it I found out that it was CommonsChunkPlugin out of all my plugins that prevented me from correctly using target node.
    I think it would be very nice if the plugin would tell a warning if it’s being run for the target node. This way it would be easier to pinpoint the problem.

Comments are closed.