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

Loader cache is not used for multiple compiler runs #7533

Closed
jantimon opened this issue Jun 14, 2018 · 5 comments
Closed

Loader cache is not used for multiple compiler runs #7533

jantimon opened this issue Jun 14, 2018 · 5 comments

Comments

@jantimon
Copy link
Contributor

jantimon commented Jun 14, 2018

Bug report

What is the current behavior?

Multiple calls to compiler.run() won't retrieve data from the loader cache.

If the current behavior is a bug, please provide the steps to reproduce.

index.js

const webpackConfig = {
  entry: { main: "./index.js" },
  context: __dirname,
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: require.resolve("./loader")
      }
    ]
  },
  output: {
    path: __dirname + "/dist"
  }
};

const webpack = require("webpack");
compiler = webpack(webpackConfig);

// Start first run:
compiler.run(() => {
  // Start second run:
  compiler.run(() => {
    // Log output:
    console.log(
      "Content of compilation result",
      require("fs").readFileSync("./dist/main.js", {
        encoding: "utf8"
      })
    );
  });
});

loader.js

let runs = 0;
module.exports = function() {
  runs++;
  return "console.log('      ######## Loader Runs " + runs + " ########');";
};

The dist/main.js contains Loader Runs 2
This means that the loader was called twice although no file was changed.

What is the expected behavior?

I would expect that the loader would have been called only once so the dist/main.js should contain Loader Runs 1

Other relevant information:
webpack version: 4.12.0
Node.js version: v6.9.1
Operating System: OSX 10.11.6

@alexander-akait
Copy link
Member

/cc @ooflorent

@sokra
Copy link
Member

sokra commented Jun 19, 2018

Are you using production mode? cache is only enabled in development.

sokra added a commit that referenced this issue Jun 19, 2018
@jantimon
Copy link
Contributor Author

jantimon commented Jun 20, 2018

@sokra wow cool! didn't know about this caching limitation.

After switching to development in the code

Expand the source code

index.js

const webpackConfig = {
  entry: { main: "./index.js" },
  mode: "development", // <-------- switch to development 
  context: __dirname,
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: require.resolve("./loader")
      }
    ]
  },
  output: {
    path: __dirname + "/dist"
  }
};

const webpack = require("webpack");
compiler = webpack(webpackConfig);

// Start first run:
compiler.run(() => {
  // Start second run:
  compiler.run(() => {
    // Log output:
    console.log(
      "Content of compilation result",
      require("fs").readFileSync("./dist/main.js", {
        encoding: "utf8"
      })
    );
  });
});

loader.js

let runs = 0;
module.exports = function() {
  runs++;
  return "console.log('      ######## Loader Runs " + runs + " ########');";
};

Webpack generates the expected output:

Expand the generated result
Content of compilation result /******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = "./index.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./index.js":
/*!******************!*\
  !*** ./index.js ***!
  \******************/
/*! no static exports found */
/***/ (function(module, exports) {

eval("console.log('      ######## Loader Runs 1 ########');\n\n//# sourceURL=webpack:///./index.js?");

/***/ })

/******/ });


Is it possible to active the cache in the config?

@sokra
Copy link
Member

sokra commented Jun 20, 2018

didn't know about this caching limitation.

It's not a limitation. It's only the default setting. Feel free to set cache: true in production mode too. The most common use case of production mode is to compile once (without watching or recompiling) so we chose this as default. No caching allows to throw away more memory and lowers the memory pressure which increases the performance.

sokra added a commit that referenced this issue Jun 20, 2018
@jantimon
Copy link
Contributor Author

Thanks - it's a perfect default setting.
Good to know 👍

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

3 participants