Repository: maizzle/framework
Pull request #778 (2022-10-02 14:21:57)

fix: make file config optional
This PR fixes an issue with computing the Maizzle config where the build would fail if you didn't have a `config.{env}.js` while using Maizzle programmatically (i.e. with the [`render`](https://maizzle.com/docs/api) API method).


Patch:
diff --git a/package.json b/package.json
index 1a98b63..e2ee0f9 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,7 @@
     "access": "public"
   },
   "scripts": {
-    "test": "c8 ava -s",
+    "test": "c8 ava",
     "pretest": "xo",
     "style": "xo",
     "release": "np"
diff --git a/src/generators/output/to-disk.js b/src/generators/output/to-disk.js
index 6d48c40..9e1fa44 100644
--- a/src/generators/output/to-disk.js
+++ b/src/generators/output/to-disk.js
@@ -103,6 +103,7 @@ module.exports = async (env, spinner, config) => {
 
             try {
               const compiled = await render(html, {
+                useFileConfig: true,
                 maizzle: {
                   ...config,
                   env
diff --git a/src/generators/output/to-string.js b/src/generators/output/to-string.js
index 32bed0a..dbb6981 100644
--- a/src/generators/output/to-string.js
+++ b/src/generators/output/to-string.js
@@ -17,7 +17,9 @@ module.exports = async (html, options) => {
     throw new RangeError('received empty string')
   }
 
-  const fileConfig = await Config.getMerged(process.env.NODE_ENV)
+  const fileConfig = get(options, 'useFileConfig')
+    ? await Config.getMerged(process.env.NODE_ENV)
+    : {}
 
   let config = merge(fileConfig, get(options, 'maizzle', {}))
 


Test patch:
diff --git a/test/expected/posthtml/extend-template.html b/test/expected/posthtml/extend-template.html
deleted file mode 100644
index b4ca0c4..0000000
--- a/test/expected/posthtml/extend-template.html
+++ /dev/null
@@ -1,2 +0,0 @@
-Parent
-    Child in second.html
diff --git a/test/expected/posthtml/layout.html b/test/expected/posthtml/layout.html
deleted file mode 100644
index 247cfdd..0000000
--- a/test/expected/posthtml/layout.html
+++ /dev/null
@@ -1,3 +0,0 @@
-    Environment: maizzle-ci
-
-    Front matter variable: Hello
diff --git a/test/fixtures/posthtml/extend-template.html b/test/fixtures/posthtml/extend-template.html
deleted file mode 100644
index 60cfbb0..0000000
--- a/test/fixtures/posthtml/extend-template.html
+++ /dev/null
@@ -1,7 +0,0 @@
----
-template: second
----
-
-<extends src="test/stubs/template.html">
-  <block name="button">Child in second.html</block>
-</extends>
diff --git a/test/fixtures/posthtml/layout.html b/test/fixtures/posthtml/layout.html
deleted file mode 100644
index a5f1192..0000000
--- a/test/fixtures/posthtml/layout.html
+++ /dev/null
@@ -1,11 +0,0 @@
----
-greeting: Hello
----
-
-<extends src="test/stubs/layouts/basic.html">
-  <block name="template">
-    Environment: {{ page.env }}
-
-    Front matter variable: {{ page.greeting }}
-  </block>
-</extends>
diff --git a/test/test-posthtml.js b/test/test-posthtml.js
index 9c8e2fc..aecf02e 100644
--- a/test/test-posthtml.js
+++ b/test/test-posthtml.js
@@ -14,27 +14,66 @@ const expected = file => readFile('expected', file)
 const renderString = (string, options = {}) => Maizzle.render(string, options).then(({html}) => html)
 
 test('layouts', async t => {
-  const source = await fixture('posthtml/layout')
+  const source = `---
+greeting: Hello
+---
 
-  const html = await renderString(source, {maizzle: {env: 'maizzle-ci'}})
+<extends src="test/stubs/layouts/basic.html">
+  <block name="template">
+    Front matter variable: {{ page.greeting }}
+  </block>
+</extends>`
 
-  t.is(html.trim(), await expected('posthtml/layout'))
+  const html = await renderString(source, {
+    maizzle: {
+      greeting: 'Hello'
+    }
+  })
+
+  t.is(html.trim(), `Front matter variable: Hello`)
 })
 
 test('inheritance when extending a template', async t => {
-  const source = await fixture('posthtml/extend-template')
+  const source = `---
+template: second
+---
+
+<extends src="test/stubs/template.html">
+  <block name="button">Child in second.html</block>
+</extends>`
+
   let html = await renderString(source)
 
   html = html.replace(/[^\S\r\n]+$/gm, '').trim()
 
-  t.is(html, await expected('posthtml/extend-template'))
+  t.is(html, `Parent
+    Child in second.html`)
 })
 
 test('components', async t => {
-  const source = await fixture('posthtml/component')
+  const source = `<component
+  src="test/stubs/components/component.html"
+  text="Example"
+  locals='{
+    "foo": "bar"
+  }'
+>
+Variable from page: [[ page.env ]]
+
+  <component
+    src="test/stubs/components/component.html"
+    text="Nested component"
+    locals='{
+      "foo": "bar (nested)"
+    }'
+  >
+Variable from page (nested): [[ page.env ]]
+  </component>
+</component>`
+
   const options = {
     maizzle: {
-      env: 'maizzle-ci',
+      env: 'prod',
       build: {
         components: {
           expressions: {
@@ -47,7 +86,19 @@ test('components', async t => {
 
   const html = await renderString(source, options)
 
-  t.is(html.trim(), await expected('posthtml/component'))
+  t.is(html.trim(), `Variable from attribute: Example
+
+Variable from locals attribute: bar
+
+
+Variable from page: prod
+
+  Variable from attribute: Nested component
+
+Variable from locals attribute: bar (nested)
+
+
+Variable from page (nested): prod`)
 })
 
 test('fetch component', async t => {
diff --git a/test/test-todisk.js b/test/test-todisk.js
index 0cea9cd..56e61c1 100644
--- a/test/test-todisk.js
+++ b/test/test-todisk.js
@@ -401,24 +401,6 @@ test('warns if a template cannot be rendered and `fail` option is `silent`', asy
   t.false(files.includes('empty.html'))
 })
 
-test('spins up local development server', async t => {
-  await Maizzle.serve('local', {
-    build: {
-      browsersync: {
-        ui: false
-      },
-      templates: {
-        source: 'test/stubs/templates',
-        destination: {
-          path: t.context.folder
-        }
-      }
-    }
-  })
-
-  t.true(await fs.pathExists(t.context.folder))
-})
-
 test('local server does not compile unwanted file types', async t => {
   await Maizzle.serve('local', {
     build: {
diff --git a/test/test-tostring.js b/test/test-tostring.js
index 53a543d..e86798a 100644
--- a/test/test-tostring.js
+++ b/test/test-tostring.js
@@ -154,9 +154,6 @@ test('@import css files in marked style tags', async t => {
   const html = await renderString(source)
 
   t.is(html, `<style>div {
-  margin-top: 1px;
-  margin-right: 2px;
-  margin-bottom: 3px;
-  margin-left: 4px;
+  margin: 1px 2px 3px 4px;
 }</style>`)
 })