-
Notifications
You must be signed in to change notification settings - Fork 214
Support per-page HTML template customisation via 'mains' #1029
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,35 +272,42 @@ Set the main entry points for the application. If the option is not set, Neutrin | |
index: 'index' | ||
} | ||
``` | ||
|
||
Notice the entry point has no extension; the extension is resolved by webpack. If relative paths are specified, | ||
they will be computed and resolved relative to `options.source`; absolute paths will be used as-is. | ||
|
||
By default these main files are not required to be in JavaScript format. They may also potentially be JSX, TypeScript, | ||
or any other preprocessor language. These extensions should be specified in middleware at | ||
`neutrino.config.resolve.extensions`. | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there 2 blank lines here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes I'll remove this additional newline |
||
```js | ||
module.exports = neutrino => { | ||
// if not specified, defaults to an object with a single entry "index", | ||
// resolved to options.source + index | ||
neutrino.options.mains.index; | ||
}; | ||
``` | ||
|
||
Multiple entry points and any page-specific configuration (if supported by the preset) can be specified like so: | ||
|
||
```js | ||
module.exports = { | ||
options: { | ||
mains: { | ||
// If not specified, defaults to options.source + index | ||
index: 'index', | ||
|
||
// Override to relative, resolves to options.source + entry.* | ||
index: 'entry', | ||
|
||
// Override to absolute path | ||
index: '/code/website/src/entry.js', | ||
|
||
// Add additional main, resolves to options.source + admin.* | ||
admin: 'admin' | ||
// Relative path, so resolves to options.source + home.* | ||
index: 'home', | ||
|
||
// Absolute path, used as-is. | ||
login: '/code/website/src/login.js', | ||
|
||
// Long form that allows passing page-specific configuration | ||
// (such as html-webpack-plugin options in the case of @neutrinojs/web). | ||
admin: { | ||
entry: 'admin', | ||
// any page-specific options here (see preset docs) | ||
// ... | ||
} | ||
} | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,10 @@ const requireFromRoot = (moduleId, root) => { | |
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
return require(path); | ||
}; | ||
// Support both a shorter string form and an object form that allows | ||
// specifying any page-specific options supported by the preset. | ||
const normalizeMainConfig = (config) => | ||
(typeof config === 'string') ? { entry: config } : config; | ||
|
||
module.exports = class Neutrino { | ||
constructor(options) { | ||
|
@@ -138,38 +142,46 @@ module.exports = class Neutrino { | |
|
||
bindMainsOnOptions(options, optionsSource) { | ||
Object | ||
.keys(options.mains) | ||
.forEach(key => { | ||
let value = options.mains[key]; | ||
.entries(options.mains) | ||
.forEach(([key, value]) => { | ||
let normalizedConfig = normalizeMainConfig(value); | ||
|
||
Reflect.defineProperty(options.mains, key, { | ||
enumerable: true, | ||
get() { | ||
const source = optionsSource && | ||
optionsSource.source || options.source; | ||
|
||
return normalizePath(source, value); | ||
return { | ||
...normalizedConfig, | ||
// Lazily normalise the path, in case `source` changes after mains is updated. | ||
entry: normalizePath(source, normalizedConfig.entry) | ||
}; | ||
}, | ||
set(newValue) { | ||
value = newValue; | ||
normalizedConfig = normalizeMainConfig(newValue); | ||
} | ||
}); | ||
}); | ||
|
||
this.mainsProxy = new Proxy(options.mains, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the Proxy itself, but only exposed it on the Neutrino API just in case. We can certainly remove it if we don't foresee exposing it as being useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing seemed to break when I removed it fwiw |
||
defineProperty: (target, prop, { value }) => { | ||
let currentValue = value; | ||
let normalizedConfig = normalizeMainConfig(value); | ||
|
||
return Reflect.defineProperty(target, prop, { | ||
enumerable: true, | ||
get() { | ||
const source = optionsSource && | ||
optionsSource.source || options.source; | ||
|
||
return normalizePath(source, currentValue); | ||
return { | ||
...normalizedConfig, | ||
// Lazily normalise the path, in case `source` changes after mains is updated. | ||
entry: normalizePath(source, normalizedConfig.entry) | ||
}; | ||
}, | ||
set(newValue) { | ||
currentValue = newValue; | ||
normalizedConfig = normalizeMainConfig(newValue); | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of pre-existing duplication between
api.md
,creating-presets.md
andcustomization.md
that is out of scope for this PR, but something we should figure out how to avoid at some point.