mirror of
https://github.com/dillonkearns/elm-pages.git
synced 2024-09-11 09:05:28 +03:00
Run webpack server for elm-pages build.
This commit is contained in:
parent
f45cc838e9
commit
7c89584333
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@ node_modules/
|
||||
elm-stuff/
|
||||
dist/
|
||||
.cache/
|
||||
generator/src/Main.js
|
||||
elm-pages-*.tgz
|
||||
|
@ -1,7 +1,8 @@
|
||||
// import * as workboxBuild from "workbox-build";
|
||||
const workboxBuild = require("workbox-build");
|
||||
|
||||
export function buildSW(): Promise<void> {
|
||||
module.exports = { buildSW };
|
||||
|
||||
function buildSW() {
|
||||
// This will return a Promise
|
||||
return workboxBuild.generateSW({
|
||||
globDirectory: "dist/",
|
@ -1,4 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { Elm } = require("./Main.js");
|
||||
const { version } = require("../../package.json");
|
||||
const fs = require("fs");
|
||||
const glob = require("glob");
|
||||
const develop = require("./develop.js");
|
||||
develop.start();
|
||||
const chokidar = require("chokidar");
|
||||
const { buildSW } = require("./build-service-worker.js");
|
||||
const matter = require("gray-matter");
|
||||
|
||||
const contentGlobPath = "content/**/*.emu";
|
||||
|
||||
let watcher = null;
|
||||
|
||||
function unpackFile(path) {
|
||||
return { path, contents: fs.readFileSync(path).toString() };
|
||||
}
|
||||
|
||||
function parseMarkdown(path, fileContents) {
|
||||
const { content, data } = matter(fileContents);
|
||||
return { path, metadata: JSON.stringify(data), body: content };
|
||||
}
|
||||
|
||||
function run() {
|
||||
console.log("Running elm-pages...");
|
||||
const content = glob.sync(contentGlobPath, {}).map(unpackFile);
|
||||
const markdownContent = glob
|
||||
.sync("content/**/*.md", {})
|
||||
.map(unpackFile)
|
||||
.map(({ path, contents }) => {
|
||||
return parseMarkdown(path, contents);
|
||||
});
|
||||
const images = glob
|
||||
.sync("images/**/*", {})
|
||||
.filter(imagePath => !fs.lstatSync(imagePath).isDirectory());
|
||||
|
||||
let app = Elm.Main.init({
|
||||
flags: {
|
||||
argv: process.argv,
|
||||
versionMessage: version,
|
||||
content,
|
||||
markdownContent,
|
||||
images
|
||||
}
|
||||
});
|
||||
|
||||
app.ports.printAndExitSuccess.subscribe(message => {
|
||||
console.log(message);
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
app.ports.printAndExitFailure.subscribe(message => {
|
||||
console.log(message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
app.ports.writeFile.subscribe(contents => {
|
||||
fs.writeFileSync("./gen/RawContent.elm", contents.rawContent);
|
||||
fs.writeFileSync("./prerender.config.js", contents.prerenderrc);
|
||||
fs.writeFileSync("./src/js/image-assets.js", contents.imageAssets);
|
||||
console.log("elm-pages DONE");
|
||||
if (contents.watch) {
|
||||
startWatchIfNeeded();
|
||||
develop.start();
|
||||
} else {
|
||||
// TODO start prod mode build here
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
function startWatchIfNeeded() {
|
||||
if (!watcher) {
|
||||
console.log("Watching...");
|
||||
watcher = chokidar
|
||||
.watch([contentGlobPath, "content/**/*.md"], {
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 500
|
||||
},
|
||||
ignoreInitial: true
|
||||
})
|
||||
.on("all", function(event, filePath) {
|
||||
console.log(`Rerunning for ${filePath}...`);
|
||||
run();
|
||||
console.log("Done!");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,94 +0,0 @@
|
||||
// @ts-ignore
|
||||
import { Elm } from "./Main.elm";
|
||||
import { version } from "../../package.json";
|
||||
import * as fs from "fs";
|
||||
import * as glob from "glob";
|
||||
import * as chokidar from "chokidar";
|
||||
import { buildSW } from "./build-service-worker";
|
||||
// @ts-ignore
|
||||
const matter = require("gray-matter");
|
||||
|
||||
const contentGlobPath = "content/**/*.emu";
|
||||
|
||||
let watcher: chokidar.FSWatcher | null = null;
|
||||
|
||||
function unpackFile(path: string) {
|
||||
return { path, contents: fs.readFileSync(path).toString() };
|
||||
}
|
||||
|
||||
type Result = { path: string; metadata: any; body: string };
|
||||
function parseMarkdown(path: string, fileContents: string): Result {
|
||||
const { content, data } = matter(fileContents);
|
||||
return { path, metadata: JSON.stringify(data), body: content };
|
||||
}
|
||||
|
||||
function run() {
|
||||
console.log("Running elm-pages...");
|
||||
const content = glob.sync(contentGlobPath, {}).map(unpackFile);
|
||||
const markdownContent = glob
|
||||
.sync("content/**/*.md", {})
|
||||
.map(unpackFile)
|
||||
.map(({ path, contents }: { path: string; contents: string }) => {
|
||||
return parseMarkdown(path, contents);
|
||||
});
|
||||
const images = glob
|
||||
.sync("images/**/*", {})
|
||||
.filter(imagePath => !fs.lstatSync(imagePath).isDirectory());
|
||||
|
||||
let app = Elm.Main.init({
|
||||
flags: {
|
||||
argv: process.argv,
|
||||
versionMessage: version,
|
||||
content,
|
||||
markdownContent,
|
||||
images
|
||||
}
|
||||
});
|
||||
|
||||
app.ports.printAndExitSuccess.subscribe((message: string) => {
|
||||
console.log(message);
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
app.ports.printAndExitFailure.subscribe((message: string) => {
|
||||
console.log(message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
app.ports.writeFile.subscribe(
|
||||
(contents: {
|
||||
rawContent: string;
|
||||
prerenderrc: string;
|
||||
imageAssets: string;
|
||||
watch: boolean;
|
||||
}) => {
|
||||
fs.writeFileSync("./gen/RawContent.elm", contents.rawContent);
|
||||
fs.writeFileSync("./prerender.config.js", contents.prerenderrc);
|
||||
fs.writeFileSync("./src/js/image-assets.js", contents.imageAssets);
|
||||
console.log("elm-pages DONE");
|
||||
if (contents.watch) {
|
||||
startWatchIfNeeded();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
function startWatchIfNeeded() {
|
||||
if (!watcher) {
|
||||
console.log("Watching...");
|
||||
watcher = chokidar
|
||||
.watch([contentGlobPath, "content/**/*.md"], {
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 500
|
||||
},
|
||||
ignoreInitial: true
|
||||
})
|
||||
.on("all", function(event, filePath) {
|
||||
console.log(`Rerunning for ${filePath}...`);
|
||||
run();
|
||||
console.log("Done!");
|
||||
});
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
"description": "Type-safe static sites, written in pure elm with your own custom elm-markup syntax.",
|
||||
"main": "./lib/index.js",
|
||||
"scripts": {
|
||||
"build": "parcel build generator/src/elm-pages.ts --out-file bundle.js --target node --bundle-node-modules --no-source-maps --no-minify"
|
||||
"build": "cd generator && elm make src/Main.elm --output src/Main.js"
|
||||
},
|
||||
"author": "Dillon Kearns",
|
||||
"license": "BSD-3",
|
||||
@ -47,6 +47,6 @@
|
||||
"lib/manifest/index.js"
|
||||
],
|
||||
"bin": {
|
||||
"elm-pages": "generator/bin/elm-pages"
|
||||
"elm-pages": "generator/src/elm-pages.js"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user