|
1 | 1 | // @ts-check
|
| 2 | +/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */ |
| 3 | +/** @typedef {import("webpack/lib/FileSystemInfo").Snapshot} Snapshot */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies |
| 9 | + * @param {WebpackCompilation} mainCompilation |
| 10 | + * @param {number} startTime |
| 11 | + */ |
| 12 | +function createSnapshot (fileDependencies, mainCompilation, startTime) { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + mainCompilation.fileSystemInfo.createSnapshot( |
| 15 | + startTime, |
| 16 | + fileDependencies.fileDependencies, |
| 17 | + fileDependencies.contextDependencies, |
| 18 | + fileDependencies.missingDependencies, |
| 19 | + null, |
| 20 | + (err, snapshot) => { |
| 21 | + if (err) { |
| 22 | + return reject(err); |
| 23 | + } |
| 24 | + resolve(snapshot); |
| 25 | + } |
| 26 | + ); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
2 | 30 | /**
|
3 |
| - * To use the available webpack core api |
4 |
| - * we have to use different child compilers |
5 |
| - * depending on the used webpack version |
| 31 | + * Returns true if the files inside this snapshot |
| 32 | + * have not been changed |
| 33 | + * |
| 34 | + * @param {Snapshot} snapshot |
| 35 | + * @param {WebpackCompilation} mainCompilation |
| 36 | + * @returns {Promise<boolean>} |
6 | 37 | */
|
7 |
| -const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]); |
8 |
| - |
9 |
| -// Typescript hack to test only the webpack 4 code |
10 |
| -/** @type {import('./webpack4/file-watcher-api')} */ |
11 |
| -module.exports = webpackMajorVersion === 4 |
12 |
| - ? require('./webpack4/file-watcher-api.js') |
13 |
| - // Hack to ignore './webpack5/file-watcher-api.js' from typescript: |
14 |
| - : require('./webpack' + 5 + '/file-watcher-api.js'); |
| 38 | +function isSnapShotValid (snapshot, mainCompilation) { |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + mainCompilation.fileSystemInfo.checkSnapshotValid( |
| 41 | + snapshot, |
| 42 | + (err, isValid) => { |
| 43 | + if (err) { |
| 44 | + reject(err); |
| 45 | + } |
| 46 | + resolve(isValid); |
| 47 | + } |
| 48 | + ); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Ensure that the files keep watched for changes |
| 54 | + * and will trigger a recompile |
| 55 | + * |
| 56 | + * @param {WebpackCompilation} mainCompilation |
| 57 | + * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies |
| 58 | + */ |
| 59 | +function watchFiles (mainCompilation, fileDependencies) { |
| 60 | + Object.keys(fileDependencies).forEach((depencyTypes) => { |
| 61 | + fileDependencies[depencyTypes].forEach(fileDependency => { |
| 62 | + mainCompilation[depencyTypes].add(fileDependency); |
| 63 | + }); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +module.exports = { |
| 68 | + createSnapshot, |
| 69 | + isSnapShotValid, |
| 70 | + watchFiles |
| 71 | +}; |
0 commit comments