Skip to content

Commit

Permalink
reexport gulp again. support async twig.logicLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
elbakerino committed Sep 12, 2021
1 parent c621af9 commit 6ff008a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 59 deletions.
3 changes: 3 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const tasks = ampCreator({
canonical: 'http://localhost:4488/' + file.relative,
},
}),
logicLoader: async () => {
return {}
},
},
prettyUrlExtensions: ['html'],
})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "create-amp-page",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.2",
"license": "MIT",
"homepage": "https://github.com/bemit/create-amp-page",
"author": "Michael Becker <https://mlbr.xyz>",
"description": "Full fledged static side generator composed out of extendable gulp tasks, optimized for - but not limited to - AMP.",
"keywords": [
"amp",
"accelerated-mobile-pages",
Expand Down
5 changes: 4 additions & 1 deletion src/AmpCreatorOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export interface AmpCreatorOptions {
logicLoader?: () => {
functions?: TwigFunction[]
filters?: TwigFunction[]
}
} | Promise<{
functions?: TwigFunction[]
filters?: TwigFunction[]
}>
// output file extension including the '.' like path.extname(filename). Use true to keep source extname and a "falsy" value to drop the file extension
outputExtname?: string | boolean
// enables debug info logging
Expand Down
122 changes: 67 additions & 55 deletions src/htmlTask/htmlTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,56 @@ export const makeTwigHandler = ({
addImageSuffix,
]

return () => subpipe((stream) => {
return async () => {
// share twig logic for `twig-as-entrypoint` and `frontmatter-as-entrypoint` (collections)
const extraLogic = {
functions: undefined,
filters: undefined,
}
if(twig.logicLoader) {
const logic = twig.logicLoader()
const logic = await twig.logicLoader()
if(logic.functions) {
extraLogic.functions = logic.functions
}
if(logic.filters) {
extraLogic.filters = logic.filters
}
}
return stream
.pipe(twigGulp({
base: paths.html,
extend: twig && twig.extend,
functions: [
...extendedTwigFunctions,
...(twig && twig.functions ? twig.functions : []),
...(extraLogic.functions || []),
],
filters: [
...(twig && twig.filters ? twig.filters : []),
...(extraLogic.filters || []),
],
extname: twig && typeof twig.outputExtname !== 'undefined' ? twig.outputExtname : '.html',
cache: !!(twig && twig.cache),
debug: !!(twig && twig.debug),
trace: !!(twig && twig.trace),
}))
// middlewares after twig compilation
// middlewares for style injection
.pipe(injectCSS({
paths, injectTag: cssInjectTag,
failOnSize: process.env.NODE_ENV === 'production',
cssBuffer: cssBuffer,
}))
// middlewares after CSS injection
.pipe(cleanHtmlCss({
minifyHtml,
cleanInlineCSS,
cleanInlineCSSWhitelist,
}))
.pipe(ampOptimizer(ampOptimize))
})
return subpipe((stream) =>
stream
.pipe(twigGulp({
base: paths.html,
extend: twig && twig.extend,
functions: [
...extendedTwigFunctions,
...(twig && twig.functions ? twig.functions : []),
...(extraLogic.functions || []),
],
filters: [
...(twig && twig.filters ? twig.filters : []),
...(extraLogic.filters || []),
],
extname: twig && typeof twig.outputExtname !== 'undefined' ? twig.outputExtname : '.html',
cache: Boolean(twig && twig.cache),
debug: Boolean(twig && twig.debug),
trace: Boolean(twig && twig.trace),
}))
// middlewares after twig compilation
// middlewares for style injection
.pipe(injectCSS({
paths, injectTag: cssInjectTag,
failOnSize: process.env.NODE_ENV === 'production',
cssBuffer: cssBuffer,
}))
// middlewares after CSS injection
.pipe(cleanHtmlCss({
minifyHtml,
cleanInlineCSS,
cleanInlineCSSWhitelist,
}))
.pipe(ampOptimizer(ampOptimize)),
)
}
}

export const makeHtmlTask = (
Expand All @@ -94,31 +96,41 @@ export const makeHtmlTask = (
const htmlTasks = []

const twigHandler = makeTwigHandler({paths, twig, ...options})
htmlTasks.push(function pagesByTemplates() {
return gulp.src(paths.htmlPages + '/*.twig')
.pipe(twigDataHandler(twig))
.pipe(twigHandler())
.pipe(gulp.dest(paths.dist))
.pipe(browsersync.stream())
})
htmlTasks.push(
function pagesByTemplates() {
return new Promise(async (resolve, reject) => {
gulp.src(paths.htmlPages + '/*.twig')
.pipe(twigDataHandler(twig))
.pipe(await twigHandler())
.pipe(gulp.dest(paths.dist))
.pipe(browsersync.stream())
.on('finish', resolve)
.on('error', reject)
})
},
)

if(collections && Array.isArray(collections)) {
collections.forEach(collection => {
htmlTasks.push(
function pagesByFrontmatter() {
return gulp.src(collection.data)
.pipe(twigMultiLoad(
{
...twig,
...(collection.fmMap ? {fmMap: collection.fmMap} : {}),
...(collection.customMerge ? {customMerge: collection.customMerge} : {}),
},
collection.tpl,
))
.pipe(twigHandler())
.pipe(twigMultiSave(collection.ext))
.pipe(gulp.dest(path.join(paths.dist, collection.base)))
.pipe(browsersync.stream())
return new Promise(async (resolve, reject) => {
gulp.src(collection.data)
.pipe(twigMultiLoad(
{
...twig,
...(collection.fmMap ? {fmMap: collection.fmMap} : {}),
...(collection.customMerge ? {customMerge: collection.customMerge} : {}),
},
collection.tpl,
))
.pipe(await twigHandler())
.pipe(twigMultiSave(collection.ext))
.pipe(gulp.dest(path.join(paths.dist, collection.base)))
.pipe(browsersync.stream())
.on('finish', resolve)
.on('error', reject)
})
},
)
})
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ampCreator.js'
export * from './AmpCreatorOptions.js'
export * from 'gulp'

0 comments on commit 6ff008a

Please sign in to comment.