UNPKG

1.73 kBJavaScriptView Raw
1/*
2 Copyright © 2019 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11const { join } = require('path');
12
13const chalk = require('chalk');
14
15const { cleanup, init } = require('./ramdisk');
16const { validate } = require('./validate');
17
18const defaults = {
19 blockSize: 512,
20 // 256 mb
21 bytes: 2.56e8,
22 name: 'wpr'
23};
24const { error, info } = console;
25const key = 'webpack-plugin-ramdisk';
26const name = 'WebpackPluginRamdisk';
27
28class WebpackPluginRamdisk {
29 constructor(opts = {}) {
30 const valid = validate(opts);
31
32 /* istanbul ignore if */
33 if (valid.error) {
34 error(chalk.red(`⬢ ${key}:`), `An option was passed to ${name} that is not valid`);
35 throw valid.error;
36 }
37
38 const options = Object.assign({}, defaults, opts);
39 const diskPath = init(options);
40
41 this.diskPath = diskPath;
42 this.options = options;
43 }
44
45 apply(compiler) {
46 const { output } = compiler.options;
47 const outputPath = join(this.diskPath, output.path || 'dist');
48
49 /* eslint-disable no-param-reassign */
50 compiler.options.output = Object.assign({}, compiler.options.output, { path: outputPath });
51 compiler.outputPath = compiler.options.output.path;
52 this.outputPath = compiler.outputPath;
53
54 info(chalk.blue(`⬡ ${key}:`), `Build being written to ${outputPath}`);
55 }
56
57 static cleanup(diskPath) {
58 return cleanup(diskPath);
59 }
60}
61
62module.exports = { defaults, WebpackPluginRamdisk };