-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strapi): add plugin:watch command (#18462)
- Loading branch information
1 parent
d4dddeb
commit 5d12a35
Showing
14 changed files
with
232 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: plugin:build | ||
description: An in depth look at the plugin:build command of the Strapi CLI | ||
tags: | ||
- CLI | ||
- commands | ||
- plugins | ||
- building | ||
--- | ||
|
||
The `plugin:watch` command is used to watch plugin source files and compile them to production viable assets in real-time. | ||
This is done by using `pack-up` underneath and a specific configuration, for this command we _do not_ look for a `packup.config` file. | ||
|
||
## Usage | ||
|
||
```bash | ||
strapi plugin:watch | ||
``` | ||
|
||
### Options | ||
|
||
```bash | ||
Watch & compile your strapi plugin for local development. | ||
|
||
Options: | ||
-d, --debug Enable debugging mode with verbose logs (default: false) | ||
--silent Don't log anything (default: false) | ||
-h, --help Display help for command | ||
``` | ||
## How it works | ||
The command sequence can be visualised as follows: | ||
- Load package.json | ||
- Validate that package.json against a `yup` schema | ||
- Validate the ordering of an export map if `pkg.exports` is defined | ||
- Create a set of "bundles" to build ignoring the package.json exports map that is _specifically_ set up for strapi-plugins. | ||
- Pass the created config to `pack-up`'s watch API. | ||
- Run's indefinitely |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/core/strapi/src/commands/actions/plugin/watch/action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import boxen from 'boxen'; | ||
import chalk from 'chalk'; | ||
import { ConfigBundle, WatchCLIOptions, watch } from '@strapi/pack-up'; | ||
import { notifyExperimentalCommand } from '../../../utils/helpers'; | ||
import { createLogger } from '../../../utils/logger'; | ||
import { Export, loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
||
interface ActionOptions extends WatchCLIOptions { | ||
force?: boolean; | ||
} | ||
|
||
export default async ({ force, ...opts }: ActionOptions) => { | ||
const logger = createLogger({ debug: opts.debug, silent: opts.silent, timestamp: false }); | ||
try { | ||
/** | ||
* Notify users this is an experimental command and get them to approve first | ||
* this can be opted out by setting the argument --yes | ||
*/ | ||
await notifyExperimentalCommand('plugin:watch', { force }); | ||
|
||
const cwd = process.cwd(); | ||
|
||
const pkg = await loadPkg({ cwd, logger }); | ||
const pkgJson = await validatePkg({ pkg }); | ||
|
||
if (!pkgJson.exports['./strapi-admin'] && !pkgJson.exports['./strapi-server']) { | ||
throw new Error( | ||
'You need to have either a strapi-admin or strapi-server export in your package.json' | ||
); | ||
} | ||
|
||
const bundles: ConfigBundle[] = []; | ||
|
||
if (pkgJson.exports['./strapi-admin']) { | ||
const exp = pkgJson.exports['./strapi-admin'] as Export; | ||
|
||
const bundle: ConfigBundle = { | ||
source: exp.source, | ||
import: exp.import, | ||
require: exp.require, | ||
runtime: 'web', | ||
}; | ||
|
||
if (exp.types) { | ||
bundle.types = exp.types; | ||
// TODO: should this be sliced from the source path...? | ||
bundle.tsconfig = './admin/tsconfig.build.json'; | ||
} | ||
|
||
bundles.push(bundle); | ||
} | ||
|
||
if (pkgJson.exports['./strapi-server']) { | ||
const exp = pkgJson.exports['./strapi-server'] as Export; | ||
|
||
const bundle: ConfigBundle = { | ||
source: exp.source, | ||
import: exp.import, | ||
require: exp.require, | ||
runtime: 'node', | ||
}; | ||
|
||
if (exp.types) { | ||
bundle.types = exp.types; | ||
// TODO: should this be sliced from the source path...? | ||
bundle.tsconfig = './server/tsconfig.build.json'; | ||
} | ||
|
||
bundles.push(bundle); | ||
} | ||
|
||
await watch({ | ||
cwd, | ||
configFile: false, | ||
config: { | ||
bundles, | ||
dist: './dist', | ||
/** | ||
* ignore the exports map of a plugin, because we're streamlining the | ||
* process and ensuring the server package and admin package are built | ||
* with the correct runtime and their individual tsconfigs | ||
*/ | ||
exports: {}, | ||
}, | ||
...opts, | ||
}); | ||
} catch (err) { | ||
logger.error( | ||
'There seems to be an unexpected error, try again with --debug for more information \n' | ||
); | ||
if (err instanceof Error && err.stack) { | ||
console.log( | ||
chalk.red( | ||
boxen(err.stack, { | ||
padding: 1, | ||
align: 'left', | ||
}) | ||
) | ||
); | ||
} | ||
process.exit(1); | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/core/strapi/src/commands/actions/plugin/watch/command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { StrapiCommand } from '../../../types'; | ||
import { runAction } from '../../../utils/helpers'; | ||
import action from './action'; | ||
|
||
/** | ||
* `$ strapi plugin:build` | ||
*/ | ||
const command: StrapiCommand = ({ command }) => { | ||
command | ||
.command('plugin:watch') | ||
.description('Watch & compile your strapi plugin for local development.') | ||
.option('-d, --debug', 'Enable debugging mode with verbose logs', false) | ||
.option('--silent', "Don't log anything", false) | ||
.action(runAction('plugin:watch', action)); | ||
}; | ||
|
||
export default command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.