Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import path from 'path';

import fs from 'fs';

import glob from 'glob';


import { buildChunkMap, replaceSource } from './lib';

const pluginName = 'manifest-replace-plugin';
Expand All @@ -25,26 +28,24 @@ class ManifestReplacePlugin {

compiler.hooks.emit.tap(pluginName, (compilation) => {
const chunkMap = buildChunkMap(compilation);
const relativeTargetDir = options.outputDir
? path.relative(compiler.options.output.path, options.outputDir)
: '';

const outputDir = options.outputDir
? options.outputDir
: compiler.options.output.path
glob
.sync(path.join(options.include, '**/**'), { nodir: true })
.filter((file) => options.test.test(path.basename(file)))
.forEach((file) => {
const source = replaceSource(file, chunkMap);

const assetKey = path.join(
relativeTargetDir,
path.relative(options.include, file)
);
const outputPath = path.join(outputDir, path.relative(options.include, file));
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}

// eslint-disable-next-line no-param-reassign
compilation.assets[assetKey] = {
source: () => source,
size: () => source.length,
};
fs.writeFile(outputPath, source, err => {
if (err) console.error(err);
});
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import fs from 'fs';

export function buildChunkMap(compilation) {
const publicPath = compilation.mainTemplate.getPublicPath({
const publicPath = compilation.getAssetPath(compilation.outputOptions.publicPath, {
hash: compilation.hash,
});

Expand Down
10 changes: 10 additions & 0 deletions test/cases/output-dir-case/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<body>
<script type="text/javascript" src="index-391ce5a8374ac3b1bf96.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions test/cases/output-dir-case/expected/output/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App Output Folder</title>
</head>
<body>
<script type="text/javascript" src="index-391ce5a8374ac3b1bf96.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions test/cases/output-dir-case/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<body>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions test/cases/output-dir-case/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// document.write('Hello world!');
10 changes: 10 additions & 0 deletions test/cases/output-dir-case/output/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App Output Folder</title>
</head>
<body>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions test/cases/output-dir-case/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path');

const ManifestReplacePlugin = require('../../../');

module.exports = {
entry: {
'index': './index.js',
},
output: {
publicPath: '',
filename: '[name]-[chunkhash].js',
},
plugins: [
new ManifestReplacePlugin({
include: path.resolve(__dirname),
outputDir: path.join(__dirname, '../../js/output-dir-case')
}),
]
};
22 changes: 16 additions & 6 deletions test/webpack-manifest-replace-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ const fs = require('fs');

const webpack = require('webpack');

const recursiveFileRead = (expectedDir, outputDir, relativeDir) => {
fs.readdirSync(expectedDir).forEach((expectedFile) => {
const filePath = path.resolve(expectedDir, expectedFile)
if (fs.lstatSync(filePath).isDirectory()) {
recursiveFileRead(filePath, outputDir, path.relative(expectedDir, filePath))
return;
}
const expected = fs.readFileSync(filePath, 'utf8');
const actual = fs.readFileSync(path.resolve(path.join(outputDir, relativeDir), expectedFile), 'utf8');

assert.strictEqual(actual, expected);
});
}

describe('Tests', () => {
fs.readdirSync(path.join(__dirname, 'cases')).forEach((testCase) => {
it(testCase, () => {
Expand All @@ -26,13 +40,9 @@ describe('Tests', () => {
}

const expectedDir = path.join(testDir, 'expected');
fs.readdirSync(expectedDir).forEach((expectedFile) => {
const expected = fs.readFileSync(path.resolve(expectedDir, expectedFile), 'utf8');
const actual = fs.readFileSync(path.resolve(outputDir, expectedFile), 'utf8');

assert.strictEqual(actual, expected);
});
recursiveFileRead(expectedDir, outputDir, '.');
});
});
})
});