|
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') |
4 | 3 |
|
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 | + } |
26 | 17 |
|
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 | +} |
50 | 27 |
|
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 | + ) |
79 | 50 | }
|
80 | 51 |
|
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 | + }) |
85 | 60 |
|
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 | + } |
88 | 69 | },
|
89 | 70 |
|
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 | + }, |
105 | 96 | }
|
0 commit comments