Projekt

Obecné

Profil

Stáhnout (3.84 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
const fs = require('fs');
4
const path = require('path');
5

    
6
const MemoryFileSystem = require('memory-fs');
7
const mkdirp = require('mkdirp');
8
const { colors } = require('webpack-log');
9

    
10
const DevMiddlewareError = require('./DevMiddlewareError');
11

    
12
module.exports = {
13
  toDisk(context) {
14
    const compilers = context.compiler.compilers || [context.compiler];
15

    
16
    for (const compiler of compilers) {
17
      compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
18
        if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
19
          return;
20
        }
21

    
22
        compiler.hooks.assetEmitted.tapAsync(
23
          'WebpackDevMiddleware',
24
          (file, info, callback) => {
25
            let targetPath = null;
26
            let content = null;
27

    
28
            // webpack@5
29
            if (info.compilation) {
30
              ({ targetPath, content } = info);
31
            } else {
32
              let targetFile = file;
33

    
34
              const queryStringIdx = targetFile.indexOf('?');
35

    
36
              if (queryStringIdx >= 0) {
37
                targetFile = targetFile.substr(0, queryStringIdx);
38
              }
39

    
40
              let { outputPath } = compiler;
41

    
42
              // TODO Why? Need remove in future major release
43
              if (outputPath === '/') {
44
                outputPath = compiler.context;
45
              }
46

    
47
              outputPath = compilation.getPath(outputPath, {});
48
              content = info;
49
              targetPath = path.join(outputPath, targetFile);
50
            }
51

    
52
            const { writeToDisk: filter } = context.options;
53
            const allowWrite =
54
              filter && typeof filter === 'function'
55
                ? filter(targetPath)
56
                : true;
57

    
58
            if (!allowWrite) {
59
              return callback();
60
            }
61

    
62
            const { log } = context;
63
            const dir = path.dirname(targetPath);
64

    
65
            return mkdirp(dir, (mkdirpError) => {
66
              if (mkdirpError) {
67
                return callback(mkdirpError);
68
              }
69

    
70
              return fs.writeFile(targetPath, content, (writeFileError) => {
71
                if (writeFileError) {
72
                  return callback(writeFileError);
73
                }
74

    
75
                log.debug(
76
                  colors.cyan(
77
                    `Asset written to disk: ${path.relative(
78
                      process.cwd(),
79
                      targetPath
80
                    )}`
81
                  )
82
                );
83

    
84
                return callback();
85
              });
86
            });
87
          }
88
        );
89
        compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
90
      });
91
    }
92
  },
93

    
94
  setFs(context, compiler) {
95
    if (
96
      typeof compiler.outputPath === 'string' &&
97
      !path.posix.isAbsolute(compiler.outputPath) &&
98
      !path.win32.isAbsolute(compiler.outputPath)
99
    ) {
100
      throw new DevMiddlewareError(
101
        '`output.path` needs to be an absolute path or `/`.'
102
      );
103
    }
104

    
105
    let fileSystem;
106

    
107
    // store our files in memory
108
    const isConfiguredFs = context.options.fs;
109
    const isMemoryFs =
110
      !isConfiguredFs &&
111
      !compiler.compilers &&
112
      compiler.outputFileSystem instanceof MemoryFileSystem;
113

    
114
    if (isConfiguredFs) {
115
      // eslint-disable-next-line no-shadow
116
      const { fs } = context.options;
117

    
118
      if (typeof fs.join !== 'function') {
119
        // very shallow check
120
        throw new Error(
121
          'Invalid options: options.fs.join() method is expected'
122
        );
123
      }
124

    
125
      // eslint-disable-next-line no-param-reassign
126
      compiler.outputFileSystem = fs;
127
      fileSystem = fs;
128
    } else if (isMemoryFs) {
129
      fileSystem = compiler.outputFileSystem;
130
    } else {
131
      fileSystem = new MemoryFileSystem();
132

    
133
      // eslint-disable-next-line no-param-reassign
134
      compiler.outputFileSystem = fileSystem;
135
    }
136

    
137
    // eslint-disable-next-line no-param-reassign
138
    context.fs = fileSystem;
139
  },
140
};
(3-3/6)