Skip to content

Commit 6ff008a

Browse files
committed
reexport gulp again. support async twig.logicLoader
1 parent c621af9 commit 6ff008a

File tree

6 files changed

+79
-59
lines changed

6 files changed

+79
-59
lines changed

Gulpfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const tasks = ampCreator({
3939
canonical: 'http://localhost:4488/' + file.relative,
4040
},
4141
}),
42+
logicLoader: async () => {
43+
return {}
44+
},
4245
},
4346
prettyUrlExtensions: ['html'],
4447
})

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "create-amp-page",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0-alpha.2",
44
"license": "MIT",
55
"homepage": "https://github.com/bemit/create-amp-page",
66
"author": "Michael Becker <https://mlbr.xyz>",
7+
"description": "Full fledged static side generator composed out of extendable gulp tasks, optimized for - but not limited to - AMP.",
78
"keywords": [
89
"amp",
910
"accelerated-mobile-pages",

src/AmpCreatorOptions.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ export interface AmpCreatorOptions {
112112
logicLoader?: () => {
113113
functions?: TwigFunction[]
114114
filters?: TwigFunction[]
115-
}
115+
} | Promise<{
116+
functions?: TwigFunction[]
117+
filters?: TwigFunction[]
118+
}>
116119
// output file extension including the '.' like path.extname(filename). Use true to keep source extname and a "falsy" value to drop the file extension
117120
outputExtname?: string | boolean
118121
// enables debug info logging

src/htmlTask/htmlTask.js

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,56 @@ export const makeTwigHandler = ({
3030
addImageSuffix,
3131
]
3232

33-
return () => subpipe((stream) => {
33+
return async () => {
3434
// share twig logic for `twig-as-entrypoint` and `frontmatter-as-entrypoint` (collections)
3535
const extraLogic = {
3636
functions: undefined,
3737
filters: undefined,
3838
}
3939
if(twig.logicLoader) {
40-
const logic = twig.logicLoader()
40+
const logic = await twig.logicLoader()
4141
if(logic.functions) {
4242
extraLogic.functions = logic.functions
4343
}
4444
if(logic.filters) {
4545
extraLogic.filters = logic.filters
4646
}
4747
}
48-
return stream
49-
.pipe(twigGulp({
50-
base: paths.html,
51-
extend: twig && twig.extend,
52-
functions: [
53-
...extendedTwigFunctions,
54-
...(twig && twig.functions ? twig.functions : []),
55-
...(extraLogic.functions || []),
56-
],
57-
filters: [
58-
...(twig && twig.filters ? twig.filters : []),
59-
...(extraLogic.filters || []),
60-
],
61-
extname: twig && typeof twig.outputExtname !== 'undefined' ? twig.outputExtname : '.html',
62-
cache: !!(twig && twig.cache),
63-
debug: !!(twig && twig.debug),
64-
trace: !!(twig && twig.trace),
65-
}))
66-
// middlewares after twig compilation
67-
// middlewares for style injection
68-
.pipe(injectCSS({
69-
paths, injectTag: cssInjectTag,
70-
failOnSize: process.env.NODE_ENV === 'production',
71-
cssBuffer: cssBuffer,
72-
}))
73-
// middlewares after CSS injection
74-
.pipe(cleanHtmlCss({
75-
minifyHtml,
76-
cleanInlineCSS,
77-
cleanInlineCSSWhitelist,
78-
}))
79-
.pipe(ampOptimizer(ampOptimize))
80-
})
48+
return subpipe((stream) =>
49+
stream
50+
.pipe(twigGulp({
51+
base: paths.html,
52+
extend: twig && twig.extend,
53+
functions: [
54+
...extendedTwigFunctions,
55+
...(twig && twig.functions ? twig.functions : []),
56+
...(extraLogic.functions || []),
57+
],
58+
filters: [
59+
...(twig && twig.filters ? twig.filters : []),
60+
...(extraLogic.filters || []),
61+
],
62+
extname: twig && typeof twig.outputExtname !== 'undefined' ? twig.outputExtname : '.html',
63+
cache: Boolean(twig && twig.cache),
64+
debug: Boolean(twig && twig.debug),
65+
trace: Boolean(twig && twig.trace),
66+
}))
67+
// middlewares after twig compilation
68+
// middlewares for style injection
69+
.pipe(injectCSS({
70+
paths, injectTag: cssInjectTag,
71+
failOnSize: process.env.NODE_ENV === 'production',
72+
cssBuffer: cssBuffer,
73+
}))
74+
// middlewares after CSS injection
75+
.pipe(cleanHtmlCss({
76+
minifyHtml,
77+
cleanInlineCSS,
78+
cleanInlineCSSWhitelist,
79+
}))
80+
.pipe(ampOptimizer(ampOptimize)),
81+
)
82+
}
8183
}
8284

8385
export const makeHtmlTask = (
@@ -94,31 +96,41 @@ export const makeHtmlTask = (
9496
const htmlTasks = []
9597

9698
const twigHandler = makeTwigHandler({paths, twig, ...options})
97-
htmlTasks.push(function pagesByTemplates() {
98-
return gulp.src(paths.htmlPages + '/*.twig')
99-
.pipe(twigDataHandler(twig))
100-
.pipe(twigHandler())
101-
.pipe(gulp.dest(paths.dist))
102-
.pipe(browsersync.stream())
103-
})
99+
htmlTasks.push(
100+
function pagesByTemplates() {
101+
return new Promise(async (resolve, reject) => {
102+
gulp.src(paths.htmlPages + '/*.twig')
103+
.pipe(twigDataHandler(twig))
104+
.pipe(await twigHandler())
105+
.pipe(gulp.dest(paths.dist))
106+
.pipe(browsersync.stream())
107+
.on('finish', resolve)
108+
.on('error', reject)
109+
})
110+
},
111+
)
104112

105113
if(collections && Array.isArray(collections)) {
106114
collections.forEach(collection => {
107115
htmlTasks.push(
108116
function pagesByFrontmatter() {
109-
return gulp.src(collection.data)
110-
.pipe(twigMultiLoad(
111-
{
112-
...twig,
113-
...(collection.fmMap ? {fmMap: collection.fmMap} : {}),
114-
...(collection.customMerge ? {customMerge: collection.customMerge} : {}),
115-
},
116-
collection.tpl,
117-
))
118-
.pipe(twigHandler())
119-
.pipe(twigMultiSave(collection.ext))
120-
.pipe(gulp.dest(path.join(paths.dist, collection.base)))
121-
.pipe(browsersync.stream())
117+
return new Promise(async (resolve, reject) => {
118+
gulp.src(collection.data)
119+
.pipe(twigMultiLoad(
120+
{
121+
...twig,
122+
...(collection.fmMap ? {fmMap: collection.fmMap} : {}),
123+
...(collection.customMerge ? {customMerge: collection.customMerge} : {}),
124+
},
125+
collection.tpl,
126+
))
127+
.pipe(await twigHandler())
128+
.pipe(twigMultiSave(collection.ext))
129+
.pipe(gulp.dest(path.join(paths.dist, collection.base)))
130+
.pipe(browsersync.stream())
131+
.on('finish', resolve)
132+
.on('error', reject)
133+
})
122134
},
123135
)
124136
})

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './ampCreator.js'
22
export * from './AmpCreatorOptions.js'
3+
export * from 'gulp'

0 commit comments

Comments
 (0)