1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const { join } = require('path');
|
12 |
|
13 | const chalk = require('chalk');
|
14 |
|
15 | const { cleanup, init } = require('./ramdisk');
|
16 | const { validate } = require('./validate');
|
17 |
|
18 | const defaults = {
|
19 | blockSize: 512,
|
20 |
|
21 | bytes: 2.56e8,
|
22 | name: 'wpr'
|
23 | };
|
24 | const { error, info } = console;
|
25 | const key = 'webpack-plugin-ramdisk';
|
26 | const name = 'WebpackPluginRamdisk';
|
27 |
|
28 | class WebpackPluginRamdisk {
|
29 | constructor(opts = {}) {
|
30 | const valid = validate(opts);
|
31 |
|
32 |
|
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 |
|
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 |
|
62 | module.exports = { defaults, WebpackPluginRamdisk };
|