-
-
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.
* fixes #286. writeToDisk option * add filter function capability
- Loading branch information
1 parent
08207cc
commit 8a0bb32
Showing
11 changed files
with
617 additions
and
1,493 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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); // eslint-disable-line import/no-extraneous-dependencies | ||
const MemoryFileSystem = require('memory-fs'); | ||
const pathabs = require('path-is-absolute'); | ||
const NodeOutputFileSystem = require('webpack/lib/node/NodeOutputFileSystem'); | ||
const DevMiddlewareError = require('./DevMiddlewareError'); | ||
|
||
const { mkdirp } = new NodeOutputFileSystem(); | ||
|
||
module.exports = { | ||
toDisk(context) { | ||
context.compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => { | ||
const { assets, compiler } = compilation; | ||
const { log } = context; | ||
const { writeToDisk: filter } = context.options; | ||
let { outputPath } = compiler; | ||
|
||
if (outputPath === '/') { | ||
outputPath = compiler.context; | ||
} | ||
|
||
for (const assetPath of Object.keys(assets)) { | ||
const asset = assets[assetPath]; | ||
const source = asset.source(); | ||
const isAbsolute = pathabs(assetPath); | ||
const writePath = isAbsolute ? assetPath : path.join(outputPath, assetPath); | ||
const relativePath = path.relative(process.cwd(), writePath); | ||
const allowWrite = filter && typeof filter === 'function' ? filter(writePath) : true; | ||
|
||
if (allowWrite) { | ||
let output = source; | ||
|
||
mkdirp.sync(path.dirname(writePath)); | ||
|
||
if (Array.isArray(source)) { | ||
output = source.join('\n'); | ||
} | ||
|
||
try { | ||
fs.writeFileSync(writePath, output, 'utf-8'); | ||
log.debug(chalk`{cyan Asset written to disk}: ${relativePath}`); | ||
} catch (e) { | ||
log.error(`Unable to write asset to disk:\n${e}`); | ||
} | ||
} | ||
} | ||
}); | ||
}, | ||
|
||
setFs(context, compiler) { | ||
if (typeof compiler.outputPath === 'string' && !pathabs.posix(compiler.outputPath) && !pathabs.win32(compiler.outputPath)) { | ||
throw new DevMiddlewareError('`output.path` needs to be an absolute path or `/`.'); | ||
} | ||
|
||
let fileSystem; | ||
// store our files in memory | ||
const isMemoryFs = !compiler.compilers && compiler.outputFileSystem instanceof MemoryFileSystem; | ||
|
||
if (isMemoryFs) { | ||
fileSystem = compiler.outputFileSystem; | ||
} else { | ||
fileSystem = new MemoryFileSystem(); | ||
compiler.outputFileSystem = fileSystem; | ||
} | ||
|
||
context.fs = fileSystem; | ||
} | ||
}; |
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
Oops, something went wrong.