Skip to content

Commit

Permalink
Add hardcoded file generation in prep for programmatic file generatio…
Browse files Browse the repository at this point in the history
…n from elm.
  • Loading branch information
dillonkearns committed Jan 5, 2020
1 parent 2d9ae3b commit 815dec7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
17 changes: 16 additions & 1 deletion generator/src/add-files-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ function unpackFile(filePath) {
}

module.exports = class AddFilesPlugin {
constructor(data) {
constructor(data, filesToGenerate) {
this.pagesWithRequests = data;
this.filesToGenerate = filesToGenerate;
console.log('this.filesToGenerate', this.filesToGenerate);

}
apply(compiler) {
compiler.hooks.emit.tap("AddFilesPlugin", compilation => {
Expand Down Expand Up @@ -52,6 +55,18 @@ module.exports = class AddFilesPlugin {
size: () => rawContents.length
};
});

this.filesToGenerate.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
compilation.assets[file.path] = {
source: () => file.content,
size: () => file.content.length
};
});


});
}
};
14 changes: 8 additions & 6 deletions generator/src/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const ClosurePlugin = require("closure-webpack-plugin");
const readline = require("readline");

module.exports = { start, run };
function start({ routes, debug, customPort, manifestConfig, routesWithRequests }) {
function start({ routes, debug, customPort, manifestConfig, routesWithRequests, filesToGenerate }) {
const config = webpackOptions(false, routes, {
debug,
manifestConfig,
routesWithRequests
routesWithRequests,
filesToGenerate
});

const compiler = webpack(config);
Expand Down Expand Up @@ -65,12 +66,13 @@ function start({ routes, debug, customPort, manifestConfig, routesWithRequests }
// app.use(express.static(__dirname + "/path-to-static-folder"));
}

function run({ routes, manifestConfig, routesWithRequests }, callback) {
function run({ routes, manifestConfig, routesWithRequests, filesToGenerate }, callback) {
webpack(
webpackOptions(true, routes, {
debug: false,
manifestConfig,
routesWithRequests
routesWithRequests,
filesToGenerate
})
).run((err, stats) => {
if (err) {
Expand Down Expand Up @@ -118,12 +120,12 @@ function printProgress(progress, message) {
function webpackOptions(
production,
routes,
{ debug, manifestConfig, routesWithRequests }
{ debug, manifestConfig, routesWithRequests, filesToGenerate }
) {
const common = {
mode: production ? "production" : "development",
plugins: [
new AddFilesPlugin(routesWithRequests),
new AddFilesPlugin(routesWithRequests, filesToGenerate),
new CopyPlugin([
{
from: "static/**/*",
Expand Down
7 changes: 5 additions & 2 deletions generator/src/elm-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function run() {
markdownContent,
content,
function(payload) {
console.log('@@@@@@@@@ filesToGenerate', payload.filesToGenerate);
if (contents.watch) {
startWatchIfNeeded();
if (!devServerRunning) {
Expand All @@ -94,7 +95,8 @@ function run() {
routes,
debug: contents.debug,
manifestConfig: payload.manifest,
routesWithRequests: payload.pages
routesWithRequests: payload.pages,
filesToGenerate: payload.filesToGenerate
});
}
} else {
Expand All @@ -106,7 +108,8 @@ function run() {
{
routes,
manifestConfig: payload.manifest,
routesWithRequests: payload.pages
routesWithRequests: payload.pages,
filesToGenerate: payload.filesToGenerate
},
() => {}
);
Expand Down
30 changes: 28 additions & 2 deletions src/Pages/Internal/Platform/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ type ToJsPayload pathKey
type alias ToJsSuccessPayload pathKey =
{ pages : Dict String (Dict String String)
, manifest : Manifest.Config pathKey
, filesToGenerate : List FileToGenerate
}


type alias FileToGenerate =
{ path : List String
, content : String
}


Expand All @@ -55,8 +62,8 @@ toJsCodec =
Errors errorList ->
errors errorList

Success { pages, manifest } ->
success (ToJsSuccessPayload pages manifest)
Success { pages, manifest, filesToGenerate } ->
success (ToJsSuccessPayload pages manifest filesToGenerate)
)
|> Codec.variant1 "Errors" Errors Codec.string
|> Codec.variant1 "Success"
Expand Down Expand Up @@ -90,6 +97,21 @@ successCodec =
|> Codec.field "manifest"
.manifest
(Codec.build Manifest.toJson (Decode.succeed stubManifest))
|> Codec.field "filesToGenerate"
.filesToGenerate
(Codec.build
(\list ->
list
|> Json.Encode.list
(\item ->
Json.Encode.object
[ ( "path", item.path |> String.join "/" |> Json.Encode.string )
, ( "content", item.content |> Json.Encode.string )
]
)
)
(Decode.succeed [])
)
|> Codec.buildObject


Expand Down Expand Up @@ -745,6 +767,10 @@ sendStaticResponsesIfDone mode secrets allRawResponses errors staticResponses ma
(ToJsSuccessPayload
(encodeStaticResponses mode staticResponses)
manifest
[ { path = [ "hello.txt" ]
, content = "Hello generated files!"
}
]
)

else
Expand Down

0 comments on commit 815dec7

Please sign in to comment.