-
-
Notifications
You must be signed in to change notification settings - Fork 380
ci: webpack@5 test #459
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
ci: webpack@5 test #459
Changes from all commits
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 |
---|---|---|
|
@@ -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) => { | ||
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) { | ||
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. Should we output 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 it is unnecessary, we should fail compilation if something not written as expected, because it is potentially break application because file(s) can be used in backend 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. ok, agree with you. |
||
return callback(writeFileError); | ||
} | ||
|
||
log.debug( | ||
colors.cyan( | ||
`Asset written to disk: ${path.relative( | ||
process.cwd(), | ||
targetPath | ||
)}` | ||
) | ||
); | ||
|
||
return callback(); | ||
}); | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
}, | ||
|
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.
Can we remove this when we release the next version?
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.
Yes, i think we should drop this in next major, i keep this because i can't understand why it is here 😕 Any ideas?
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.
Yes, it's a good idea which is keeping. I don't know why...
We'll drop this at other pr.