Skip to content

Commit 3c7f25c

Browse files
committed
fix(#30): 🐛 incremental build does not update streamed file contents
1 parent c5a65ee commit 3c7f25c

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ function createTransformStream(flushFn, entryPoints) {
2626
return cb(createError(new TypeError('File should be a buffer')))
2727
}
2828

29-
entryPoints.push(file)
29+
// What is going on?
30+
//
31+
// The problem attaches incrementalBuild. Because of rebuild context creates once,
32+
// resolvePlugin doesn't receive updated virtual files. We moved variable `entryPoints`
33+
// to a higher level so the plugin could receive actual files by reference.
34+
//
35+
// But now entryPoints doesn't reset its state and stores all files for every rebuilt.
36+
// This code searches existed files and updates their contents.
37+
const fileIndex = entryPoints.findIndex((cfile) => cfile.path === file.path)
38+
39+
if (fileIndex >= 0) {
40+
entryPoints[fileIndex] = file
41+
} else {
42+
entryPoints.push(file)
43+
}
44+
3045
cb(null)
3146
},
3247
flush: flushFn,
@@ -57,6 +72,7 @@ function simpleBuild() {
5772
const {metafileName, esbuildOptions} = splitOptions(pluginOptions)
5873

5974
async function flushFunction(cb) {
75+
/** @type import('esbuild').BuildOptions */
6076
const params = {
6177
logLevel: 'silent',
6278
...esbuildOptions,
@@ -105,14 +121,16 @@ function simpleBuild() {
105121
}
106122

107123
function incrementalBuild() {
124+
/** @type import('esbuild').BuildContext */
108125
let ctx
126+
/** @type Array<import('vinyl').BufferFile> */
127+
const entryPoints = []
109128

110129
return function plugin(pluginOptions = {}) {
111-
/** @type Array<import('vinyl').BufferFile> */
112-
const entryPoints = []
113130
const {metafileName, esbuildOptions} = splitOptions(pluginOptions)
114131

115132
async function flushFunction(cb) {
133+
/** @type import('esbuild').BuildOptions */
116134
const params = {
117135
logLevel: 'silent',
118136
...esbuildOptions,

index.mjs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ function createTransformStream(flushFn, entryPoints) {
3030
return cb(createError(new TypeError('File should be a buffer')))
3131
}
3232

33-
entryPoints.push(file)
33+
// What is going on?
34+
//
35+
// The problem attaches incrementalBuild. Because of rebuild context creates once,
36+
// resolvePlugin doesn't receive updated virtual files. We moved variable `entryPoints`
37+
// to a higher level so the plugin could receive actual files by reference.
38+
//
39+
// But now entryPoints doesn't reset its state and stores all files for every rebuilt.
40+
// This code searches existed files and updates their contents.
41+
const fileIndex = entryPoints.findIndex((cfile) => cfile.path === file.path)
42+
43+
if (fileIndex >= 0) {
44+
entryPoints[fileIndex] = file
45+
} else {
46+
entryPoints.push(file)
47+
}
48+
3449
cb(null)
3550
},
3651
flush: flushFn,
@@ -61,6 +76,7 @@ function simpleBuild() {
6176
const {metafileName, esbuildOptions} = splitOptions(pluginOptions)
6277

6378
async function flushFunction(cb) {
79+
/** @type import('esbuild').BuildOptions */
6480
const params = {
6581
logLevel: 'silent',
6682
...esbuildOptions,
@@ -109,14 +125,16 @@ function simpleBuild() {
109125
}
110126

111127
function incrementalBuild() {
128+
/** @type import('esbuild').BuildContext */
112129
let ctx
130+
/** @type Array<import('vinyl').BufferFile> */
131+
const entryPoints = []
113132

114133
return function plugin(pluginOptions = {}) {
115-
/** @type Array<import('vinyl').BufferFile> */
116-
const entryPoints = []
117134
const {metafileName, esbuildOptions} = splitOptions(pluginOptions)
118135

119136
async function flushFunction(cb) {
137+
/** @type import('esbuild').BuildOptions */
120138
const params = {
121139
logLevel: 'silent',
122140
...esbuildOptions,

0 commit comments

Comments
 (0)