Skip to content

Elm 0.19 port #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# elm-static-html-lib
Note: This is a fork of https://github.com/eeue56/elm-static-html-lib for elm 0.19

Generate static html by passing an optional json object to your Elm views.

Expand Down Expand Up @@ -156,4 +157,4 @@ You may want to hide warnings, which you can do by setting `HIDE_WARNINGS=true`
### 0.1.0

- elmStaticHtml now exposes two functions with no default, e.g `import { elmStaticHtml, multiple }`
- `multiple` added to provide a way to compile multiple views in a single pass
- `multiple` added to provide a way to compile multiple views in a single pass
2 changes: 1 addition & 1 deletion example/MyModule.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ view model =
Html.div
[]
[ Html.text <| "I am " ++ model.name
, Html.text <| "And I am " ++ toString model.age ++ " years old."
, Html.text <| "And I am " ++ String.fromInt model.age ++ " years old."
]


Expand Down
15 changes: 0 additions & 15 deletions example/elm-package.json

This file was deleted.

21 changes: 21 additions & 0 deletions example/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "application",
"source-directories": [
"."
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/json": "1.1.3"
},
"indirect": {
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
15 changes: 9 additions & 6 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ function runTwice() {
elmStaticHtml(process.cwd(), "MyModule.view", firstRunOptions)
.then((generatedHtml) => {
fs.writeFileSync("output.html", generatedHtml);
elmStaticHtml(process.cwd(), "MyModule.view", secondRunOptions)
return elmStaticHtml(process.cwd(), "MyModule.view", secondRunOptions)
.then((generatedHtml) => {
fs.writeFileSync("output2.html", generatedHtml);
elmStaticHtml(process.cwd(), "MyModule.view", secondRunOptions)
return elmStaticHtml(process.cwd(), "MyModule.view", secondRunOptions)
.then((generatedHtml) => {
fs.writeFileSync("output3.html", generatedHtml);
});
});
});
})
.catch(console.error);
}

runTwice();
Expand All @@ -27,7 +28,8 @@ function runWithoutModel() {
elmStaticHtml(process.cwd(), "MyModule.otherView", {})
.then((generatedHtml) => {
fs.writeFileSync("output4.html", generatedHtml);
});
})
.catch(console.error);
}

runWithoutModel();
Expand All @@ -37,7 +39,7 @@ function runLazyView() {
.then((generatedHtml) => {
fs.writeFileSync("output5.html", generatedHtml);
}).catch((err) => {
console.log(err);
console.error(err);
});
}

Expand All @@ -54,7 +56,8 @@ function runMultiple() {
.then((generatedHtmls) => {
generatedHtmls
.map((output) => fs.writeFileSync(output.fileOutputName, output.generatedHtml));
});
})
.catch(console.error);
}

runMultiple();
4 changes: 0 additions & 4 deletions example/output.html

This file was deleted.

51 changes: 22 additions & 29 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ function makeCacheDir(dirPath: string) {
}
}

function parseProjectName(repoName: string): string {
return repoName
.replace("https://github.com/", "")
.replace(".git", "")
.replace("/", "$");
}

function runElmApp(moduleHash: string, dirPath: string, filenamesAndModels: any[][]): Promise<Output[]> {

return new Promise((resolve, reject) => {
const Elm = require(path.join(dirPath, "elm.js"));
const elmFile = path.join(dirPath, "elm.js");

// Find and replace the placeholder
const elmFileContent = fs.readFileSync(elmFile, 'utf-8');
fs.writeFileSync(elmFile, elmFileContent.replace("return elm$json$Json$Encode$string('REPLACE_ME_WITH_JSON_STRINGIFY')", 'return x'));

const Elm = require(elmFile);
const privateName = `PrivateMain${moduleHash}`;

if (Object.keys(Elm).indexOf(privateName) === - 1) {
if (Object.keys(Elm.Elm).indexOf(privateName) === - 1) {
return reject("Code generation problem: Unable to find the module: " + privateName);
}

const elmApp = Elm[privateName].worker(filenamesAndModels);
const elmApp = Elm.Elm[privateName].init({
flags: filenamesAndModels
});

elmApp.ports[`htmlOut${moduleHash}`].subscribe(resolve);
});
Expand Down Expand Up @@ -112,8 +114,8 @@ export function multiple(
return runElmApp(moduleHash, dirPath, filenamesAndModels);
}

// try to load elm-package.json
const originalElmPackagePath = path.join(rootDir, "elm-package.json");
// try to load elm.json
const originalElmPackagePath = path.join(rootDir, "elm.json");
let elmPackage: any = null;
try {
elmPackage = JSON.parse(fs.readFileSync(originalElmPackagePath, "utf8"));
Expand All @@ -124,12 +126,10 @@ export function multiple(
makeCacheDir(dirPath);
wipeElmFromCache(dirPath);

const projectName = parseProjectName(elmPackage.repository);
elmPackage = fixElmPackage(rootDir, elmPackage);

const elmPackagePath = path.join(dirPath, "elm-package.json");
const elmPackagePath = path.join(dirPath, "elm.json");
const privateMainPath = path.join(dirPath, `PrivateMain${moduleHash}.elm`);
const nativePath = path.join(dirPath, "Native/Jsonify.js");

fs.writeFileSync(elmPackagePath, JSON.stringify(elmPackage));

Expand All @@ -146,9 +146,6 @@ export function multiple(
const rendererFileContents = templates.generateRendererFile(moduleHash, templateConfigs);
fs.writeFileSync(privateMainPath, rendererFileContents);

const nativeString = templates.generateNativeModuleString(projectName);
fs.writeFileSync(nativePath, nativeString);

return installPackages(dirPath, installMethod).then(() => {
return runCompiler(moduleHash, privateMainPath, dirPath, configs, elmMakePath);
});
Expand All @@ -173,8 +170,8 @@ export function elmStaticHtml(rootDir: string, viewFunction: string, options: Op
.then((outputs) => outputs[0].generatedHtml);
}

// try to load elm-package.json
const originalElmPackagePath = path.join(rootDir, "elm-package.json");
// try to load elm.json
const originalElmPackagePath = path.join(rootDir, "elm.json");
let elmPackage: any = null;
try {
elmPackage = JSON.parse(fs.readFileSync(originalElmPackagePath, "utf8"));
Expand All @@ -185,36 +182,32 @@ export function elmStaticHtml(rootDir: string, viewFunction: string, options: Op
makeCacheDir(dirPath);
wipeElmFromCache(dirPath);

const projectName = parseProjectName(elmPackage.repository);
elmPackage = fixElmPackage(rootDir, elmPackage);

const elmPackagePath = path.join(dirPath, "elm-package.json");
const elmPackagePath = path.join(dirPath, "elm.json");
const privateMainPath = path.join(dirPath, `PrivateMain${viewHash}.elm`);
const nativePath = path.join(dirPath, "Native/Jsonify.js");

fs.writeFileSync(elmPackagePath, JSON.stringify(elmPackage));

const rendererFileContents = templates.generateRendererFile(viewHash, [config]);
fs.writeFileSync(privateMainPath, rendererFileContents);

const nativeString = templates.generateNativeModuleString(projectName);
fs.writeFileSync(nativePath, nativeString);

return installPackages(dirPath, options.installMethod).then(() => {
return runCompiler(viewHash, privateMainPath, dirPath, [config], options.elmMakePath)
.then((results) => results[0].generatedHtml);
});
}

function fixElmPackage(workingDir: string, elmPackage: any) {
elmPackage["native-modules"] = true;
const sources = elmPackage["source-directories"].map((dir: string) => {
return path.join(workingDir, dir);
});
sources.push(".");

elmPackage["source-directories"] = sources;
elmPackage.dependencies["eeue56/elm-html-in-elm"] = "2.0.0 <= v < 3.0.0";
elmPackage.dependencies.direct["ThinkAlexandria/elm-html-in-elm"] = "1.0.1";
elmPackage.dependencies.direct["elm/json"] = "1.1.3";


return elmPackage;
}
Expand All @@ -226,7 +219,7 @@ function runCompiler(moduleHash: string,
const options: any = {
cwd: rootDir,
output: "elm.js",
yes: true,
optimize: true
};

if (elmMakePath) {
Expand All @@ -247,7 +240,7 @@ function runCompiler(moduleHash: string,
const runs = runElmApp(moduleHash, rootDir,
configs.map((config) => [config.fileOutputName, config.model]));

return runs.then(resolve);
return runs.then(resolve).catch(reject);
},
);
});
Expand Down
Loading