-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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 { | ||
|
@@ -27,7 +37,7 @@ export function makeWatchRun(instance: TSInstance) { | |
} | ||
|
||
lastTimes.set(filePath, date); | ||
updateFile(instance, filePath); | ||
promises.push(updateFile(instance, loader, loaderIndex, filePath)); | ||
} | ||
|
||
// On watch update add all known dts files expect the ones in node_modules | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this will create a new instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be a bit efficient to cache the result of ... In the first place,
|
||
}, | ||
(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(); | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { message } from './message'; | ||
console.log(message); |
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} |
There was a problem hiding this comment.
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
Promise
s vs just callingupdateFile
there and then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runLoaders
called inupdateFile
is an async function. We must wait for all of them to finish before callingcallback
.To implement waiting for multiple async functions, I think
Promise.all
is a good way.