Skip to content

Commit 8d02e9e

Browse files
committed
initial setup
1 parent eb1a3fc commit 8d02e9e

File tree

4 files changed

+122
-111
lines changed

4 files changed

+122
-111
lines changed

manifest.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
name: 'netlify-plugin-{{name}}'
2-
inputs: []
3-
# Example inputs:
4-
# - name: example
5-
# description: Example description
6-
# default: 5
7-
# required: false
1+
name: 'netlify-plugin-11ty'
2+
inputs:
3+
- name: img_cache_local
4+
description: 'Cache images generated by 11ty Image plugin'
5+
default: true
6+
7+
- name: img_cache_local_dir
8+
description: '11ty image plugin output directory'
9+
default: '/img'
10+
11+
- name: img_cache_remote
12+
default: '../cache'
13+
14+
- name: img_cache_remote_dir
15+
default: '../cache'
16+
17+
- name: img_cache_httpHeader
18+
default: true
19+
20+
- name: other_cache_dir
21+
default: []

netlify.toml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44

55
# List of Build plugins
66
[[plugins]]
7-
# Use the current plugin in builds for testing and debugging.
8-
# Local path to the plugin.
97
package = "."
10-
# Plugin configuration
11-
# [plugins.inputs]
12-
# foo = "bar"
8+
[plugins.inputs]
9+
img_cache_local = true
10+
img_cache_local_dir = ['/img']
11+
12+
img_cache_remote = true
13+
img_cache_remote_dir = '../.cache'
14+
15+
img_cache_httpHeader = true
16+
17+
other_cache_dir = []
1318

1419
[build]
15-
# Dummy build command
16-
command = "echo onBuild"
20+
base = "./"
21+
command = "echo \"haha building!\""
22+
publish = "_site"
1723

1824
[build.environment]
1925
NODE_VERSION = "12.18.0"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"prettier": "^2.1.2",
4949
"release-it": "^14.0.3"
5050
}
51-
}
51+
}

src/index.js

Lines changed: 87 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,96 @@
1-
// This is the main file for the Netlify Build plugin 11ty.
2-
// Please read the comments to learn more about the Netlify Build plugin syntax.
3-
// Find more information in the Netlify documentation.
1+
const path = require('path')
2+
const fs = require('fs')
43

5-
/* eslint-disable no-unused-vars */
6-
module.exports = {
7-
// The plugin main logic uses `on...` event handlers that are triggered on
8-
// each new Netlify Build.
9-
// Anything can be done inside those event handlers.
10-
// Information about the current build are passed as arguments. The build
11-
// configuration file and some core utilities are also available.
12-
async onPreBuild({
13-
// Whole configuration file. For example, content of `netlify.toml`
14-
netlifyConfig,
15-
// Users can pass configuration inputs to any plugin in their Netlify
16-
// configuration file.
17-
// For example:
18-
//
19-
// [[plugins]]
20-
// package = "netlify-plugin-11ty"
21-
// [plugins.inputs]
22-
// foo = "bar"
23-
inputs,
24-
// `onError` event handlers receive the error instance as argument
25-
error,
4+
const getCacheDirs = (constants, inputs) => {
5+
const getCacheDirs__ = (constants, input) => {
6+
let cacheDirs = []
7+
8+
if (typeof input === 'string') {
9+
cacheDirs.push(path.normalize(constants.PUBLISH_DIR + '/' + input))
10+
} else if (Array.isArray(input)) {
11+
input.map((x) => {
12+
cacheDirs.push(path.normalize(constants.PUBLISH_DIR + '/' + x))
13+
})
14+
} else {
15+
console.log(`Warning: Unsupported value in inputs. Ignoring ${input}`)
16+
}
2617

27-
// Build constants
28-
constants: {
29-
// Path to the Netlify configuration file. `undefined` if none was used
30-
CONFIG_PATH,
31-
// Directory that contains the deploy-ready HTML files and assets
32-
// generated by the build. Its value is always defined, but the target
33-
// might not have been created yet.
34-
PUBLISH_DIR,
35-
// The directory where function source code lives.
36-
// `undefined` if not specified by the user.
37-
FUNCTIONS_SRC,
38-
// The directory where built serverless functions are placed before
39-
// deployment. Its value is always defined, but the target might not have
40-
// been created yet.
41-
FUNCTIONS_DIST,
42-
// Boolean indicating whether the build was run locally (Netlify CLI) or
43-
// in the production CI
44-
IS_LOCAL,
45-
// Version of Netlify Build as a `major.minor.patch` string
46-
NETLIFY_BUILD_VERSION,
47-
// The Netlify Site ID
48-
SITE_ID,
49-
},
18+
return cacheDirs
19+
}
20+
21+
return [
22+
...getCacheDirs__(constants, inputs.img_cache_local_dir),
23+
...getCacheDirs__(constants, inputs.img_cache_remote_dir),
24+
...getCacheDirs__(constants, inputs.other_cache_dir),
25+
]
26+
}
5027

51-
// Core utilities
52-
utils: {
53-
// Utility to report errors.
54-
// See https://github.com/netlify/build#error-reporting
55-
build,
56-
// Utility to display information in the deploy summary.
57-
// See https://github.com/netlify/build#logging
58-
status,
59-
// Utility for caching files.
60-
// See https://github.com/netlify/build/blob/master/packages/cache-utils#readme
61-
cache,
62-
// Utility for running commands.
63-
// See https://github.com/netlify/build/blob/master/packages/run-utils#readme
64-
run,
65-
// Utility for dealing with modified, created, deleted files since a git commit.
66-
// See https://github.com/netlify/build/blob/master/packages/git-utils#readme
67-
git,
68-
// Utility for handling Netlify Functions.
69-
// See https://github.com/netlify/build/tree/master/packages/functions-utils#readme
70-
functions,
71-
},
72-
}) {
73-
try {
74-
// Commands are printed in Netlify logs
75-
await run('echo', ['Hello world!\n'])
76-
} catch (error) {
77-
// Report a user error
78-
build.failBuild('Error message', { error })
28+
const getHttpHeaders = (inputs) => {
29+
let httpHeader = ''
30+
31+
inputs.img_cache_local_dir.map((x) => {
32+
httpHeader += `
33+
${x}/*
34+
cache-control: public
35+
cache-control: max-age=31536000
36+
cache-control: immutable
37+
38+
`
39+
})
40+
41+
return httpHeader
42+
}
43+
44+
module.exports = {
45+
async onPreBuild({ constants, inputs, utils }) {
46+
if (!constants.PUBLISH_DIR || process.cwd() === constants.PUBLISH_DIR) {
47+
utils.build.failBuild(
48+
`11ty sites must publish the dist directory, but your site’s publish directory is set to : “${constants.PUBLISH_DIR}”. Please set your publish directory to your 11ty site’s dist directory.`,
49+
)
7950
}
8051

81-
// Console logs are shown in Netlify logs
82-
console.log('Netlify configuration', netlifyConfig)
83-
console.log('Plugin configuration', inputs)
84-
console.log('Build directory', PUBLISH_DIR)
52+
const cacheDirs = getCacheDirs(constants, inputs)
53+
cacheDirs.map((x) => {
54+
if (fs.existsSync(x)) {
55+
console.log(
56+
`Warning: directory ${x} already exists before restoring caches. It will be replaced.`,
57+
)
58+
}
59+
})
8560

86-
// Display success information
87-
status.show({ summary: 'Success!' })
61+
if (await utils.cache.restore(cacheDirs)) {
62+
console.log('Restoring 11ty directories from cache:')
63+
cacheDirs.map((x) => {
64+
console.log('- ' + x)
65+
})
66+
} else {
67+
console.log('No 11ty caches found. Building fresh.')
68+
}
8869
},
8970

90-
// Other available event handlers
91-
/*
92-
// Before build commands are executed
93-
onPreBuild() {},
94-
// Build commands are executed
95-
onBuild() {},
96-
// After Build commands are executed
97-
onPostBuild() {},
98-
// Runs on build success
99-
onSuccess() {},
100-
// Runs on build error
101-
onError() {},
102-
// Runs on build error or success
103-
onEnd() {},
104-
*/
71+
async onPostBuild({ constants, inputs, utils }) {
72+
if (inputs.img_cache_httpHeader) {
73+
fs.appendFile(
74+
`${constants.PUBLISH_DIR}/_headers`,
75+
getHttpHeaders(inputs),
76+
(err) => {
77+
if (err) throw err
78+
console.log('Saved http header')
79+
},
80+
)
81+
}
82+
83+
const cacheDirs = getCacheDirs(constants, inputs)
84+
85+
if (await utils.cache.save(cacheDirs)) {
86+
console.log('Saving 11ty directories to cache:')
87+
cacheDirs.map((x) => {
88+
console.log('- ' + x)
89+
})
90+
} else {
91+
console.log(
92+
`Warning: 11ty build not found. Is you publish directory set correctly? “${constants.PUBLISH_DIR}”`,
93+
)
94+
}
95+
},
10596
}

0 commit comments

Comments
 (0)