How to use the webpack-sources/lib/ReplaceSource function in webpack-sources

To help you get started, we’ve selected a few webpack-sources examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zinserjan / mocha-webpack / src / webpack / InjectChangedFilesPlugin.js View on Github external
setChangedFiles(compilation, file) {
    const original = compilation.assets[file];
    const originalSource = original.source();
    const originalMap = original.map();

    const result = new ReplaceSource(original);
    const regex = /__webpackManifest__\s*=\s*\[\s*\]/g; // eslint-disable-line
    const files = this.hotFiles.concat(this.failedFiles);
    const changedFiles = `['${files.join("', '")}']`;
    const replacement = `__webpackManifest__ = ${changedFiles}`;

    let match;
    while ((match = regex.exec(originalSource)) !== null) { // eslint-disable-line no-cond-assign
      const start = match.index;
      const end = match.index + (match[0].length - 1);
      result.replace(start, end, replacement);
    }

    const resultSource = result.source();
    const resultMap = result.map();

    compilation.assets[file] = new SourceMapSource( // eslint-disable-line no-param-reassign