Get file watching working in dev mode for content.txt files.

This commit is contained in:
Dillon Kearns 2019-08-20 20:12:14 -07:00
parent 1310f5cbea
commit 942e55f319
2 changed files with 23 additions and 13 deletions

View File

@ -1,18 +1,35 @@
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const matter = require("gray-matter");
function unpackFile(filePath) {
const { content, data } = matter(fs.readFileSync(filePath).toString());
const baseRoute = filePath
.replace("content/", "")
.replace(/(index)?\.[a-zA-Z]*$/, "");
return {
baseRoute,
content
};
}
module.exports = class AddFilesPlugin {
constructor(filesList) {
this.filesList = filesList;
}
constructor() {}
apply(compiler) {
compiler.hooks.emit.tap("AddFilesPlugin", compilation => {
this.filesList.forEach(file => {
const files = glob.sync("content/**/*.*", {}).map(unpackFile);
files.forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478
const filename = path.join(file.name, "content.txt");
const filename = path.join(file.baseRoute, "content.txt");
compilation.fileDependencies.add(filename);
compilation.assets[filename] = {
source: () => file.content,
size: () => file.content.length

View File

@ -86,14 +86,7 @@ function webpackOptions(
entry: { hello: "./index.js" },
mode: production ? "production" : "development",
plugins: [
new AddFilesPlugin(
fileContents.map(([path, content]) => {
return {
name: path,
content: content
};
})
),
new AddFilesPlugin(),
new CopyPlugin([
{
from: "static/**/*",