mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-28 06:05:31 +03:00
Get file watching working in dev mode for content.txt files.
This commit is contained in:
parent
1310f5cbea
commit
942e55f319
@ -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
|
||||
|
@ -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/**/*",
|
||||
|
Loading…
Reference in New Issue
Block a user