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

Apply other loaders when updating files in watch mode #1115

Merged
merged 5 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@
"dependencies": {
"chalk": "^2.3.0",
"enhanced-resolve": "^4.0.0",
"loader-runner": "^3.1.0",
"loader-utils": "^1.0.2",
"micromatch": "^4.0.0",
"semver": "^6.0.0"
},
"devDependencies": {
"@types/loader-runner": "^2.2.3",
"@types/micromatch": "^3.1.0",
"@types/node": "*",
"@types/semver": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export function initializeInstance(
);
loader._compiler.hooks.watchRun.tapAsync(
'ts-loader',
makeWatchRun(instance)
makeWatchRun(instance, loader)
);
}
} else {
Expand Down Expand Up @@ -345,7 +345,7 @@ export function initializeInstance(
);
loader._compiler.hooks.watchRun.tapAsync(
'ts-loader',
makeWatchRun(instance)
makeWatchRun(instance, loader)
);
}
}
Expand Down
66 changes: 54 additions & 12 deletions src/watch-run.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as fs from 'fs';
import * as loaderRunner from 'loader-runner';
import * as path from 'path';
import * as webpack from 'webpack';

import * as constants from './constants';
Expand All @@ -8,12 +11,19 @@ import { readFile } from './utils';
/**
* Make function which will manually update changed files
*/
export function makeWatchRun(instance: TSInstance) {
export function makeWatchRun(
instance: TSInstance,
loader: webpack.loader.LoaderContext
) {
// Called Before starting compilation after watch
const lastTimes = new Map<string, number>();
const startTime = 0;

return (compiler: webpack.Compiler, callback: () => void) => {
// Save the loader index. 'loader.loaderIndex' is set to '-1' after all the loaders are run.
const loaderIndex = loader.loaderIndex;

return (compiler: webpack.Compiler, callback: (err?: Error) => void) => {
const promises = [];
if (instance.loaderOptions.transpileOnly) {
instance.reportTranspileErrors = true;
} else {
Expand All @@ -27,7 +37,7 @@ export function makeWatchRun(instance: TSInstance) {
}

lastTimes.set(filePath, date);
updateFile(instance, filePath);
promises.push(updateFile(instance, loader, loaderIndex, filePath));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the significance of making this array of Promises vs just calling updateFile there and then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runLoaders called in updateFile is an async function. We must wait for all of them to finish before calling callback.
To implement waiting for multiple async functions, I think Promise.all is a good way.

}

// On watch update add all known dts files expect the ones in node_modules
Expand All @@ -37,26 +47,58 @@ export function makeWatchRun(instance: TSInstance) {
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
filePath.match(constants.nodeModules) === null
) {
updateFile(instance, filePath);
promises.push(updateFile(instance, loader, loaderIndex, filePath));
}
}
}

// Update all the watched files from solution builder
if (instance.solutionBuilderHost) {
for (const filePath of instance.solutionBuilderHost.watchedFiles.keys()) {
updateFile(instance, filePath);
promises.push(updateFile(instance, loader, loaderIndex, filePath));
}
}

callback();
Promise.all(promises)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember if we've discussed this already, are there any issues with us moving to a Promise (ie async) approach instead of the current approach? Given we're dealing with callbacks I suspect not, but I wanted to check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are no new issues introduced by the 'async' approach.

.then(() => callback())
.catch(err => callback(err));
};
}

function updateFile(instance: TSInstance, filePath: string) {
updateFileWithText(
instance,
filePath,
nFilePath => readFile(nFilePath) || ''
);
function updateFile(
instance: TSInstance,
loader: webpack.loader.LoaderContext,
loaderIndex: number,
filePath: string
) {
return new Promise<void>((resolve, reject) => {
if (instance.rootFileNames.has(path.normalize(filePath))) {
loaderRunner.runLoaders(
{
resource: filePath,
loaders: loader.loaders
.slice(loaderIndex + 1)
.map(l => ({ loader: l.path, options: l.options })),
context: {},
readResource: fs.readFile.bind(fs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this will create a new instance of readFile each time updateFile runs. Would it be more performant to create this once and reuse it? In fact is the bind necessary at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a bit efficient to cache the result of fs.readFile.bind(fs) as you say.
I think bind is necessary, because there is no guarantee that fs.readFile does not use this internally.

... In the first place, readResource is not necessary, because it is optional and defaulted to readFile. As @types/loader-runner does not mark it as optional, I pass the default value explicitly to avoid a type error.
This may be better:

runLoaders(
  {
    resource: ...,
    loaders: ...
  } as loaderRunner.RunLoaderOption,

},
(err, result) => {
if (err) {
reject(err);
} else {
const text = result.result![0]!.toString();
updateFileWithText(instance, filePath, () => text);
resolve();
}
}
);
} else {
updateFileWithText(
instance,
filePath,
nFilePath => readFile(nFilePath) || ''
);
resolve();
}
});
}
2 changes: 2 additions & 0 deletions test/comparison-tests/otherLoadersWatch/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { message } from './message';
console.log(message);
113 changes: 113 additions & 0 deletions test/comparison-tests/otherLoadersWatch/expectedOutput-3.8/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/******/ (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 = "./app.ts");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./app.ts":
/*!****************!*\
!*** ./app.ts ***!
\****************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\nexports.__esModule = true;\nvar message_1 = __webpack_require__(/*! ./message */ \"./message.ts\");\nconsole.log(message_1.message);\n\n\n//# sourceURL=webpack:///./app.ts?");

/***/ }),

/***/ "./message.ts":
/*!********************!*\
!*** ./message.ts ***!
\********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\nexports.__esModule = true;\nexports.message = 'Hello, world!';\n\n\n//# sourceURL=webpack:///./message.ts?");

/***/ })

/******/ });
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 4.22 KiB main [emitted] main
Entrypoint main = bundle.js
[./app.ts] 111 bytes {main} [built]
[./message.ts] 76 bytes {main} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/******/ (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 = "./app.ts");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./app.ts":
/*!****************!*\
!*** ./app.ts ***!
\****************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\nexports.__esModule = true;\nvar message_1 = __webpack_require__(/*! ./message */ \"./message.ts\");\nconsole.log(message_1.message);\n\n\n//# sourceURL=webpack:///./app.ts?");

/***/ }),

/***/ "./message.ts":
/*!********************!*\
!*** ./message.ts ***!
\********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\nexports.__esModule = true;\nexports.message = 'Hello, world!';\n\n\n//# sourceURL=webpack:///./message.ts?");

/***/ })

/******/ });
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Asset Size Chunks Chunk Names
bundle.js 4.22 KiB main [emitted] main
Entrypoint main = bundle.js
[./app.ts] 111 bytes {main} [built]
[./message.ts] 76 bytes {main}
Loading