Skip to content

Commit

Permalink
fix: use configMutations for deploy config API (#5517)
Browse files Browse the repository at this point in the history
* fix: use `configMutations` for deploy config API

* chore: remove `console.log`
  • Loading branch information
eduardoboucas authored Feb 14, 2024
1 parent 24f130e commit a3b3298
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 500 deletions.
47 changes: 5 additions & 42 deletions packages/build/src/plugins_core/deploy_config/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
import { promises as fs } from 'fs'
import { resolve } from 'path'

import { mergeConfigs } from '@netlify/config'

import type { NetlifyConfig } from '../../index.js'
import { getConfigMutations } from '../../plugins/child/diff.js'
import { CoreStep, CoreStepFunction } from '../types.js'

import { filterConfig } from './util.js'

// The properties that can be set using this API. Each element represents a
// path using dot-notation — e.g. `["build", "functions"]` represents the
// `build.functions` property.
const ALLOWED_PROPERTIES = [
['build', 'edge_functions'],
['build', 'functions'],
['build', 'publish'],
['functions', '*'],
['functions', '*', '*'],
['headers'],
['images', 'remote_images'],
['redirects'],
]

// For array properties, any values set in this API will be merged with the
// main configuration file in such a way that user-defined values always take
// precedence. The exception are these properties that let frameworks set
// values that should be evaluated before any user-defined values. They use
// a special notation where `headers!` represents "forced headers", etc.
const OVERRIDE_PROPERTIES = new Set(['headers!', 'redirects!'])
const ALLOWED_PROPERTIES = [['images', 'remote_images']]

const coreStep: CoreStepFunction = async function ({
buildDir,
netlifyConfig,
systemLog = () => {
// no-op
},
Expand All @@ -55,33 +37,14 @@ const coreStep: CoreStepFunction = async function ({
throw new Error('An error occured while processing the platform configurarion defined by your framework')
}

const configOverrides: Partial<NetlifyConfig> = {}

for (const key in config) {
// If the key uses the special notation for defining mutations that should
// take precedence over user-defined properties, extract the canonical
// property, set it on a different object, and delete it from the main one.
if (OVERRIDE_PROPERTIES.has(key)) {
const canonicalKey = key.slice(0, -1)

configOverrides[canonicalKey] = config[key]

delete config[key]
}
}

// Filtering out any properties that can't be mutated using this API.
config = filterConfig(config, [], ALLOWED_PROPERTIES, systemLog)

// Merging the different configuration sources. The order here is important.
// Leftmost elements of the array take precedence.
const newConfig = mergeConfigs([config, netlifyConfig, configOverrides], { concatenateArrays: true })
const configMutations = getConfigMutations({}, config, applyDeployConfig.event)

for (const key in newConfig) {
netlifyConfig[key] = newConfig[key]
return {
configMutations,
}

return {}
}

export const applyDeployConfig: CoreStep = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
{
"functions": {
"my_framework*": {
"included_files": ["files/**"]
}
},
"redirects": [
{
"from": "/$netlify-snapshot-preserve/from-config",
"to": "/$netlify-snapshot-preserve/to-config",
"status": 418,
"force": true
}
],
"redirects!": [
{
"from": "/$netlify-snapshot-preserve/from-config-override",
"to": "/$netlify-snapshot-preserve/to-config-override",
"status": 418,
"force": true
}
],
"images": {
"remote_images": ["domain1.netlify", "domain2.netlify"]
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
{
"functions": {
"my_framework*": {
"included_files": ["files/**"]
}
},
"redirects": [
{
"from": "/$netlify-snapshot-preserve/from-config",
"to": "/$netlify-snapshot-preserve/to-config",
"status": 418,
"force": true
}
],
"redirects!": [
{
"from": "/$netlify-snapshot-preserve/from-config-override",
"to": "/$netlify-snapshot-preserve/to-config-override",
"status": 418,
"force": true
}
],
"images": {
"remote_images": ["domain1.netlify", "domain2.netlify"]
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
{
"build": {
"command": "echo Goodbye",
"publish": "publish/api",
"processing": {
"css": "!important"
}
},
"plugins": [
{
"package": "@netlify/imaginary-plugin"
}
],
"redirects": [
{
"from": "/$netlify-snapshot-preserve/from-config",
"to": "/$netlify-snapshot-preserve/to-config",
"status": 418,
"force": true
}
],
"redirects!": [
{
"from": "/$netlify-snapshot-preserve/from-config-override",
"to": "/$netlify-snapshot-preserve/to-config-override",
"status": 418,
"force": true
}
],
"unknown_property": 42
"images": {
"remote_images": ["domain1.netlify", "domain2.netlify"]
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit a3b3298

Please sign in to comment.