mirror of
https://github.com/modernweb-dev/rocket.git
synced 2026-03-21 15:54:57 +00:00
Compare commits
2 Commits
@rocket/la
...
@rocket/cl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8df9a3e9c3 | ||
|
|
1b9559f2a5 |
@@ -1,4 +1,4 @@
|
|||||||
# Configuration >> Overview ||10
|
# Configuration >> Overview || 10
|
||||||
|
|
||||||
The configuration file is `rocket.config.js` or `rocket.config.mjs`.
|
The configuration file is `rocket.config.js` or `rocket.config.mjs`.
|
||||||
|
|
||||||
@@ -86,3 +86,15 @@ const config = {
|
|||||||
|
|
||||||
export default config;
|
export default config;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Lifecycle
|
||||||
|
|
||||||
|
You can hook into the rocket lifecycle by specifying a function for `before11ty`. This function runs before 11ty calls it's write method. If it is an async function, Rocket will await it's promise.
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
async before11ty() {
|
||||||
|
await copyDataFiles();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|||||||
11
docs/guides/presets/options.md
Normal file
11
docs/guides/presets/options.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Presets >> Create your own > Options || 10
|
||||||
|
|
||||||
|
Your preset can hook into the rocket lifecycle by specifying a function for `before11ty`. This function runs before 11ty calls it's write method. If it is an async function, Rocket will await it's promise.
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
async before11ty() {
|
||||||
|
await copyDataFiles();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
```
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
# @rocket/cli
|
# @rocket/cli
|
||||||
|
|
||||||
|
## 0.9.5
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 1b9559f: Adds `before11ty` hook to config and presets
|
||||||
|
|
||||||
## 0.9.4
|
## 0.9.4
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@rocket/cli",
|
"name": "@rocket/cli",
|
||||||
"version": "0.9.4",
|
"version": "0.9.5",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export class RocketEleventy extends Eleventy {
|
|||||||
|
|
||||||
async write() {
|
async write() {
|
||||||
await this.__rocketCli.mergePresets();
|
await this.__rocketCli.mergePresets();
|
||||||
|
for (const fn of this.__rocketCli.config.__before11tyFunctions) await fn();
|
||||||
await super.write();
|
await super.write();
|
||||||
await this.__rocketCli.update();
|
await this.__rocketCli.update();
|
||||||
}
|
}
|
||||||
@@ -120,7 +121,7 @@ export class RocketCli {
|
|||||||
for (const folder of ['_assets', '_data', '_includes']) {
|
for (const folder of ['_assets', '_data', '_includes']) {
|
||||||
const to = path.join(this.config._inputDirCwdRelative, `_merged${folder}`);
|
const to = path.join(this.config._inputDirCwdRelative, `_merged${folder}`);
|
||||||
await fs.emptyDir(to);
|
await fs.emptyDir(to);
|
||||||
for (const sourceDir of this.config._presetPathes) {
|
for (const sourceDir of this.config._presetPaths) {
|
||||||
const from = path.join(sourceDir, folder);
|
const from = path.join(sourceDir, folder);
|
||||||
if (fs.existsSync(from)) {
|
if (fs.existsSync(from)) {
|
||||||
if (folder === '_includes') {
|
if (folder === '_includes') {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
/** @typedef {import('@web/dev-server').DevServerConfig} DevServerConfig */
|
/** @typedef {import('@web/dev-server').DevServerConfig} DevServerConfig */
|
||||||
|
|
||||||
/** @typedef {import('../types/main').RocketCliOptions} RocketCliOptions */
|
/** @typedef {import('../types/main').RocketCliOptions} RocketCliOptions */
|
||||||
|
/** @typedef {import('../types/main').ImagePreset} ImagePreset */
|
||||||
/** @typedef {import('../types/main').RocketPlugin} RocketPlugin */
|
/** @typedef {import('../types/main').RocketPlugin} RocketPlugin */
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
@@ -37,7 +38,7 @@ function ignore({ src }) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Partial<RocketCliOptions>} inConfig
|
* @param {Partial<RocketCliOptions>} inConfig
|
||||||
* @returns {Promise<RocketCliOptions>}
|
* @returns {Promise<RocketCliOptions & { __before11tyFunctions: RocketCliOptions['before11ty'][] }>}
|
||||||
*/
|
*/
|
||||||
export async function normalizeConfig(inConfig) {
|
export async function normalizeConfig(inConfig) {
|
||||||
let config = {
|
let config = {
|
||||||
@@ -61,6 +62,11 @@ export async function normalizeConfig(inConfig) {
|
|||||||
devServer: {},
|
devServer: {},
|
||||||
|
|
||||||
...inConfig,
|
...inConfig,
|
||||||
|
|
||||||
|
/** @type{RocketCliOptions['before11ty'][]} */
|
||||||
|
__before11tyFunctions: [],
|
||||||
|
|
||||||
|
/** @type{{[key: string]: ImagePreset}} */
|
||||||
imagePresets: {
|
imagePresets: {
|
||||||
responsive: {
|
responsive: {
|
||||||
widths: [600, 900, 1640],
|
widths: [600, 900, 1640],
|
||||||
@@ -121,9 +127,9 @@ export async function normalizeConfig(inConfig) {
|
|||||||
const _inputDirCwdRelative = path.join(_configDirCwdRelative, config.inputDir);
|
const _inputDirCwdRelative = path.join(_configDirCwdRelative, config.inputDir);
|
||||||
|
|
||||||
// cli core preset is always first
|
// cli core preset is always first
|
||||||
config._presetPathes = [path.join(__dirname, '..', 'preset')];
|
config._presetPaths = [path.join(__dirname, '..', 'preset')];
|
||||||
for (const preset of config.presets) {
|
for (const preset of config.presets) {
|
||||||
config._presetPathes.push(preset.path);
|
config._presetPaths.push(preset.path);
|
||||||
|
|
||||||
if (preset.adjustImagePresets) {
|
if (preset.adjustImagePresets) {
|
||||||
config.imagePresets = preset.adjustImagePresets(config.imagePresets);
|
config.imagePresets = preset.adjustImagePresets(config.imagePresets);
|
||||||
@@ -159,9 +165,13 @@ export async function normalizeConfig(inConfig) {
|
|||||||
if (preset.setupCliPlugins) {
|
if (preset.setupCliPlugins) {
|
||||||
config.setupCliPlugins = [...config.setupCliPlugins, ...preset.setupCliPlugins];
|
config.setupCliPlugins = [...config.setupCliPlugins, ...preset.setupCliPlugins];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof preset.before11ty === 'function') {
|
||||||
|
config.__before11tyFunctions.push(preset.before11ty);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// add "local" preset
|
// add "local" preset
|
||||||
config._presetPathes.push(path.resolve(_inputDirCwdRelative));
|
config._presetPaths.push(path.resolve(_inputDirCwdRelative));
|
||||||
|
|
||||||
/** @type {MetaPlugin[]} */
|
/** @type {MetaPlugin[]} */
|
||||||
let pluginsMeta = [
|
let pluginsMeta = [
|
||||||
@@ -186,6 +196,10 @@ export async function normalizeConfig(inConfig) {
|
|||||||
plugins.push(pluginInst);
|
plugins.push(pluginInst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof config.before11ty === 'function') {
|
||||||
|
config.__before11tyFunctions.push(config.before11ty);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: check pathPrefix to NOT have a '/' at the end as it will mean it may get ignored by 11ty 🤷♂️
|
// TODO: check pathPrefix to NOT have a '/' at the end as it will mean it may get ignored by 11ty 🤷♂️
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ function cleanup(config) {
|
|||||||
const configNoPaths = { ...config };
|
const configNoPaths = { ...config };
|
||||||
delete configNoPaths._inputDirCwdRelative;
|
delete configNoPaths._inputDirCwdRelative;
|
||||||
delete configNoPaths.configFile;
|
delete configNoPaths.configFile;
|
||||||
delete configNoPaths._presetPathes;
|
delete configNoPaths._presetPaths;
|
||||||
delete configNoPaths.eleventy;
|
delete configNoPaths.eleventy;
|
||||||
delete configNoPaths.outputDevDir;
|
delete configNoPaths.outputDevDir;
|
||||||
delete configNoPaths.imagePresets.responsive.ignore;
|
delete configNoPaths.imagePresets.responsive.ignore;
|
||||||
@@ -24,11 +24,12 @@ describe('normalizeConfig', () => {
|
|||||||
|
|
||||||
// testing pathes is always a little more complicted 😅
|
// testing pathes is always a little more complicted 😅
|
||||||
expect(config._inputDirCwdRelative).to.match(/empty\/docs$/);
|
expect(config._inputDirCwdRelative).to.match(/empty\/docs$/);
|
||||||
expect(config._presetPathes[0]).to.match(/cli\/preset$/);
|
expect(config._presetPaths[0]).to.match(/cli\/preset$/);
|
||||||
expect(config._presetPathes[1]).to.match(/empty\/docs$/);
|
expect(config._presetPaths[1]).to.match(/empty\/docs$/);
|
||||||
expect(config.outputDevDir).to.match(/_site-dev$/);
|
expect(config.outputDevDir).to.match(/_site-dev$/);
|
||||||
|
|
||||||
expect(cleanup(config)).to.deep.equal({
|
expect(cleanup(config)).to.deep.equal({
|
||||||
|
__before11tyFunctions: [],
|
||||||
command: 'help',
|
command: 'help',
|
||||||
createSocialMediaImages: true,
|
createSocialMediaImages: true,
|
||||||
devServer: {},
|
devServer: {},
|
||||||
@@ -70,6 +71,7 @@ describe('normalizeConfig', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(cleanup(config)).to.deep.equal({
|
expect(cleanup(config)).to.deep.equal({
|
||||||
|
__before11tyFunctions: [],
|
||||||
command: 'help',
|
command: 'help',
|
||||||
createSocialMediaImages: true,
|
createSocialMediaImages: true,
|
||||||
devServer: {
|
devServer: {
|
||||||
@@ -110,6 +112,7 @@ describe('normalizeConfig', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(cleanup(config)).to.deep.equal({
|
expect(cleanup(config)).to.deep.equal({
|
||||||
|
__before11tyFunctions: [],
|
||||||
command: 'help',
|
command: 'help',
|
||||||
createSocialMediaImages: true,
|
createSocialMediaImages: true,
|
||||||
devServer: {
|
devServer: {
|
||||||
@@ -155,6 +158,7 @@ describe('normalizeConfig', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(cleanup(config)).to.deep.equal({
|
expect(cleanup(config)).to.deep.equal({
|
||||||
|
__before11tyFunctions: [],
|
||||||
command: 'help',
|
command: 'help',
|
||||||
createSocialMediaImages: true,
|
createSocialMediaImages: true,
|
||||||
devServer: {},
|
devServer: {},
|
||||||
|
|||||||
61
packages/cli/types/main.d.ts
vendored
61
packages/cli/types/main.d.ts
vendored
@@ -4,21 +4,25 @@ import { CheckHtmlLinksCliOptions } from 'check-html-links/dist-types/types/main
|
|||||||
export interface RocketPreset {
|
export interface RocketPreset {
|
||||||
path: string;
|
path: string;
|
||||||
|
|
||||||
|
adjustImagePresets?: (preset: { [key: string]: ImagePreset }) => { [key: string]: ImagePreset };
|
||||||
|
|
||||||
|
before11ty?: () => void | Promise<void>;
|
||||||
|
|
||||||
// TODO: improve all setup functions
|
// TODO: improve all setup functions
|
||||||
setupUnifiedPlugins?: function[];
|
setupUnifiedPlugins?: function[];
|
||||||
setupDevAndBuildPlugins: function[];
|
setupDevAndBuildPlugins?: function[];
|
||||||
setupBuildPlugins: function[];
|
setupBuildPlugins?: function[];
|
||||||
setupDevPlugins: function[];
|
setupDevPlugins?: function[];
|
||||||
setupCliPlugins: function[];
|
setupCliPlugins?: function[];
|
||||||
setupEleventyPlugins: function[];
|
setupEleventyPlugins?: function[];
|
||||||
setupEleventyComputedConfig: function[];
|
setupEleventyComputedConfig?: function[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RocketStartConfig {
|
interface RocketStartConfig {
|
||||||
createSocialMediaImages?: boolean;
|
createSocialMediaImages?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ImageFormat = 'avif' | 'webp' | 'jpg' | 'png' | 'svg';
|
type ImageFormat = 'avif' | 'webp' | 'jpg' | 'jpeg' | 'png' | 'svg';
|
||||||
|
|
||||||
interface ImagePreset {
|
interface ImagePreset {
|
||||||
widths: number[];
|
widths: number[];
|
||||||
@@ -27,46 +31,49 @@ interface ImagePreset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface RocketCliOptions {
|
export interface RocketCliOptions {
|
||||||
presets: Array<RocketPreset>;
|
presets?: Array<RocketPreset>;
|
||||||
pathPrefix?: string;
|
pathPrefix?: string;
|
||||||
serviceWorkerName?: string;
|
serviceWorkerName?: string;
|
||||||
inputDir: string;
|
inputDir?: string;
|
||||||
outputDir: string;
|
outputDir?: string;
|
||||||
emptyOutputDir?: boolean;
|
emptyOutputDir?: boolean;
|
||||||
absoluteBaseUrl?: string;
|
absoluteBaseUrl?: string;
|
||||||
watch: boolean;
|
watch?: boolean;
|
||||||
createSocialMediaImages?: boolean;
|
createSocialMediaImages?: boolean;
|
||||||
imagePresets: {
|
imagePresets?: {
|
||||||
[key: string]: ImagePreset;
|
[key: string]: ImagePreset;
|
||||||
};
|
};
|
||||||
|
|
||||||
checkLinks: Partial<CheckHtmlLinksCliOptions>;
|
before11ty?: () => void | Promise<void>;
|
||||||
|
|
||||||
|
checkLinks?: Partial<CheckHtmlLinksCliOptions>;
|
||||||
|
|
||||||
start?: RocketStartConfig;
|
start?: RocketStartConfig;
|
||||||
|
|
||||||
// TODO: improve all setup functions
|
// TODO: improve all setup functions
|
||||||
setupUnifiedPlugins?: function[];
|
setupUnifiedPlugins?: function[];
|
||||||
setupDevAndBuildPlugins: function[];
|
setupDevAndBuildPlugins?: function[];
|
||||||
setupBuildPlugins: function[];
|
setupBuildPlugins?: function[];
|
||||||
setupDevPlugins: function[];
|
setupDevPlugins?: function[];
|
||||||
setupCliPlugins: function[];
|
setupCliPlugins?: function[];
|
||||||
setupEleventyPlugins: function[];
|
setupEleventyPlugins?: function[];
|
||||||
setupEleventyComputedConfig: function[];
|
setupEleventyComputedConfig?: function[];
|
||||||
|
|
||||||
// advanced
|
// advanced
|
||||||
devServer: DevServerConfig;
|
devServer?: DevServerConfig;
|
||||||
eleventy: function; // TODO: improve
|
eleventy?: (eleventyConfig: any) => void; // TODO: improve
|
||||||
plugins: RocketPlugin[];
|
plugins?: RocketPlugin[];
|
||||||
|
|
||||||
// rarely used
|
// rarely used
|
||||||
command: string;
|
command?: string;
|
||||||
configFile?: string;
|
configFile?: string;
|
||||||
outputDevDir: string;
|
outputDevDir?: string;
|
||||||
|
|
||||||
private _inputDirCwdRelative: string;
|
private _inputDirCwdRelative?: string;
|
||||||
private _presetPathes?: Array<string>;
|
private _presetPaths?: string[];
|
||||||
|
private __before11tyFunctions?: (() => void | Promise<void>)[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RocketPlugin {
|
export interface RocketPlugin {
|
||||||
commands: Array<string>;
|
commands: string[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user