-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: the
writeToFile
option has compatibility with webpack@5 (#459)
- Loading branch information
1 parent
4ee0f29
commit 5c90e1e
Showing
7 changed files
with
55 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,63 +12,68 @@ const DevMiddlewareError = require('./DevMiddlewareError'); | |
module.exports = { | ||
toDisk(context) { | ||
const compilers = context.compiler.compilers || [context.compiler]; | ||
|
||
for (const compiler of compilers) { | ||
compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bigmeow
|
||
const { assets } = compilation; | ||
const { log } = context; | ||
const { writeToDisk: filter } = context.options; | ||
let { outputPath } = compiler; | ||
compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => { | ||
compiler.hooks.assetEmitted.tapAsync( | ||
'WebpackDevMiddleware', | ||
(file, content, callback) => { | ||
let targetFile = file; | ||
|
||
if (outputPath === '/') { | ||
outputPath = compiler.context; | ||
} | ||
const queryStringIdx = targetFile.indexOf('?'); | ||
|
||
for (const assetPath of Object.keys(assets)) { | ||
let targetFile = assetPath; | ||
if (queryStringIdx >= 0) { | ||
targetFile = targetFile.substr(0, queryStringIdx); | ||
} | ||
|
||
const queryStringIdx = targetFile.indexOf('?'); | ||
let { outputPath } = compiler; | ||
|
||
if (queryStringIdx >= 0) { | ||
targetFile = targetFile.substr(0, queryStringIdx); | ||
} | ||
// TODO Why? Need remove in future major release | ||
if (outputPath === '/') { | ||
outputPath = compiler.context; | ||
} | ||
|
||
const targetPath = path.isAbsolute(targetFile) | ||
? targetFile | ||
: path.join(outputPath, targetFile); | ||
const allowWrite = | ||
filter && typeof filter === 'function' ? filter(targetPath) : true; | ||
outputPath = compilation.getPath(outputPath, {}); | ||
|
||
if (allowWrite) { | ||
const asset = assets[assetPath]; | ||
let content = asset.source(); | ||
const targetPath = path.join(outputPath, targetFile); | ||
|
||
if (!Buffer.isBuffer(content)) { | ||
// TODO need remove in next major release | ||
if (Array.isArray(content)) { | ||
content = content.join('\n'); | ||
} | ||
const { writeToDisk: filter } = context.options; | ||
const allowWrite = | ||
filter && typeof filter === 'function' | ||
? filter(targetPath) | ||
: true; | ||
|
||
content = Buffer.from(content, 'utf8'); | ||
if (!allowWrite) { | ||
return callback(); | ||
} | ||
|
||
mkdirp.sync(path.dirname(targetPath)); | ||
|
||
try { | ||
fs.writeFileSync(targetPath, content, 'utf-8'); | ||
|
||
log.debug( | ||
colors.cyan( | ||
`Asset written to disk: ${path.relative( | ||
process.cwd(), | ||
targetPath | ||
)}` | ||
) | ||
); | ||
} catch (e) { | ||
log.error(`Unable to write asset to disk:\n${e}`); | ||
} | ||
const { log } = context; | ||
const dir = path.dirname(targetPath); | ||
|
||
return mkdirp(dir, (mkdirpError) => { | ||
if (mkdirpError) { | ||
return callback(mkdirpError); | ||
} | ||
|
||
return fs.writeFile(targetPath, content, (writeFileError) => { | ||
if (writeFileError) { | ||
return callback(writeFileError); | ||
} | ||
|
||
log.debug( | ||
colors.cyan( | ||
`Asset written to disk: ${path.relative( | ||
process.cwd(), | ||
targetPath | ||
)}` | ||
) | ||
); | ||
|
||
return callback(); | ||
}); | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if
writeToDisk: true
, my code will not work. But it works fine in version 3.7.0This is my webpack version: "webpack": ">=4 < 4.29",