@wordpress/create-block 5.1.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -80,6 +80,8 @@ The rest of the configuration is set to all default values unless overridden wit
80
80
  --wp-scripts enable integration with `@wordpress/scripts` package
81
81
  --no-wp-scripts disable integration with `@wordpress/scripts` package
82
82
  --wp-env enable integration with `@wordpress/env` package
83
+ --typescript scaffold TypeScript source files, when the template supports it
84
+ --no-typescript scaffold JavaScript source files, when the template supports it
83
85
  --textdomain <value> text domain for internationalization
84
86
  -h, --help output usage information
85
87
  ```
@@ -122,6 +124,14 @@ With this argument, the `create-block` package will add to the generated plugin
122
124
  $ npx @wordpress/create-block@latest --wp-env
123
125
  ```
124
126
 
127
+ #### `--typescript`
128
+
129
+ With this argument, the `create-block` package scaffolds the block source files in TypeScript. It also adds a `tsconfig.json` file, the `typescript` and `@types/react` packages, and a `typecheck` script to the generated plugin. The built-in `standard` template supports it, and external templates can opt in.
130
+
131
+ ```bash
132
+ $ npx @wordpress/create-block@latest --typescript
133
+ ```
134
+
125
135
  #### `--textdomain`
126
136
 
127
137
  With this argument, the `create-block` package will a generate a block with the provided text domain. If not specified, the block’s slug is used as the default text domain.
package/lib/index.js CHANGED
@@ -12,6 +12,21 @@ const {
12
12
  runPrompts,
13
13
  } = require( './templates' );
14
14
 
15
+ /**
16
+ * Throws when TypeScript is requested for a template that does not opt in.
17
+ * A template opts in by setting `typescript` in its default values.
18
+ *
19
+ * @param {Object} defaultValues The template's default values.
20
+ * @param {boolean} typescript The value of the `--typescript` option.
21
+ */
22
+ function checkTypeScriptSupport( defaultValues, typescript ) {
23
+ if ( typescript && defaultValues.typescript === undefined ) {
24
+ throw new CLIError(
25
+ 'The "--typescript" option was selected. This template does not support TypeScript!'
26
+ );
27
+ }
28
+ }
29
+
15
30
  const commandName = `wp-create-block`;
16
31
  program
17
32
  .name( commandName )
@@ -56,6 +71,14 @@ program
56
71
  'disable integration with `@wordpress/scripts` package'
57
72
  )
58
73
  .option( '--wp-env', 'enable integration with `@wordpress/env` package' )
74
+ .option(
75
+ '--typescript',
76
+ 'scaffold TypeScript source files, when the template supports it'
77
+ )
78
+ .option(
79
+ '--no-typescript',
80
+ 'scaffold JavaScript source files, when the template supports it'
81
+ )
59
82
  .option( '--textdomain <value>', 'text domain for internationalization' )
60
83
  .action(
61
84
  async (
@@ -69,6 +92,7 @@ program
69
92
  title,
70
93
  wpScripts,
71
94
  wpEnv,
95
+ typescript,
72
96
  variant,
73
97
  targetDir,
74
98
  textdomain,
@@ -104,16 +128,20 @@ program
104
128
  title,
105
129
  wpScripts,
106
130
  wpEnv,
131
+ typescript,
107
132
  targetDir,
108
133
  textdomain,
109
134
  } ).filter( ( [ , value ] ) => value !== undefined )
110
135
  );
111
136
 
112
137
  if ( slug ) {
138
+ // Documented fallback: no variant means the first one.
139
+ variant = variant ?? availableVariants[ 0 ];
113
140
  const defaultValues = getDefaultValues(
114
141
  projectTemplate,
115
142
  variant
116
143
  );
144
+ checkTypeScriptSupport( defaultValues, typescript );
117
145
  const answers = {
118
146
  ...defaultValues,
119
147
  slug,
@@ -140,11 +168,13 @@ program
140
168
  } ) ),
141
169
  } );
142
170
  }
171
+ variant = variant ?? availableVariants[ 0 ];
143
172
 
144
173
  const defaultValues = getDefaultValues(
145
174
  projectTemplate,
146
175
  variant
147
176
  );
177
+ checkTypeScriptSupport( defaultValues, typescript );
148
178
 
149
179
  const blockAnswers = await runPrompts(
150
180
  projectTemplate,
@@ -156,6 +186,8 @@ program
156
186
  'dashicon',
157
187
  'category',
158
188
  ! plugin && ! textdomain && 'textdomain',
189
+ defaultValues.typescript !== undefined &&
190
+ 'typescript',
159
191
  ].filter( Boolean ),
160
192
  variant,
161
193
  optionsValues
@@ -216,5 +248,6 @@ program
216
248
  log.info(
217
249
  ` $ ${ commandName } todo-list --no-plugin --textdomain=my-plugin`
218
250
  );
251
+ log.info( ` $ ${ commandName } todo-list --typescript` );
219
252
  } )
220
253
  .parse( process.argv );
@@ -12,6 +12,7 @@ module.exports = async ( {
12
12
  version,
13
13
  wpEnv,
14
14
  wpScripts,
15
+ typescript,
15
16
  npmDependencies,
16
17
  npmDevDependencies,
17
18
  customScripts,
@@ -47,7 +48,8 @@ module.exports = async ( {
47
48
  try {
48
49
  checkDependency( packageArg );
49
50
  const parsed = npmPackageArg( packageArg );
50
- dependencies[ parsed.name ] = parsed.saveSpec || 'latest';
51
+ dependencies[ parsed.name ] =
52
+ parsed.saveSpec || parsed.rawSpec || 'latest';
51
53
  } catch ( { message } ) {
52
54
  info( '' );
53
55
  info( `Skipping "${ packageArg }" npm dependency. Reason:` );
@@ -61,7 +63,8 @@ module.exports = async ( {
61
63
  try {
62
64
  checkDependency( packageArg );
63
65
  const parsed = npmPackageArg( packageArg );
64
- devDependencies[ parsed.name ] = parsed.saveSpec || 'latest';
66
+ devDependencies[ parsed.name ] =
67
+ parsed.saveSpec || parsed.rawSpec || 'latest';
65
68
  } catch ( { message } ) {
66
69
  info( '' );
67
70
  info(
@@ -99,6 +102,7 @@ module.exports = async ( {
99
102
  ? 'wp-scripts start --webpack-copy-php'
100
103
  : 'wp-scripts start' ) + ' --blocks-manifest',
101
104
  } ),
105
+ ...( typescript && { typecheck: 'tsc' } ),
102
106
  ...( wpEnv && { env: 'wp-env' } ),
103
107
  ...customScripts,
104
108
  },
package/lib/output.js CHANGED
@@ -13,7 +13,11 @@ const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
13
13
  // If the rendered template is empty, don't write it. This is how we can conditionally add template files.
14
14
  const renderedFile = render( inputFile, view );
15
15
  if ( renderedFile.trim().length ) {
16
- const outputFilePath = join( view.rootDirectory, outputFile );
16
+ // File names can use view values, e.g. `index.{{jsExt}}.mustache`.
17
+ const outputFilePath = join(
18
+ view.rootDirectory,
19
+ render( outputFile, view )
20
+ );
17
21
  await makeDir( dirname( outputFilePath ) );
18
22
  writeFile(
19
23
  outputFilePath.replace( /\$slug/g, view.slug ),
package/lib/prompts.js CHANGED
@@ -75,6 +75,11 @@ const category = {
75
75
  ),
76
76
  };
77
77
 
78
+ const typescript = {
79
+ type: 'confirm',
80
+ message: 'Do you want to use TypeScript?',
81
+ };
82
+
78
83
  const textdomain = {
79
84
  type: 'input',
80
85
  message:
@@ -143,6 +148,7 @@ module.exports = {
143
148
  description,
144
149
  dashicon,
145
150
  category,
151
+ typescript,
146
152
  textdomain,
147
153
  pluginURI,
148
154
  version,
package/lib/scaffold.js CHANGED
@@ -40,6 +40,7 @@ module.exports = async (
40
40
  testedUpTo,
41
41
  wpScripts,
42
42
  wpEnv,
43
+ typescript,
43
44
  npmDependencies,
44
45
  npmDevDependencies,
45
46
  customScripts,
@@ -89,8 +90,12 @@ module.exports = async (
89
90
  testedUpTo,
90
91
  wpScripts,
91
92
  wpEnv,
93
+ typescript: !! typescript,
92
94
  npmDependencies,
93
- npmDevDependencies,
95
+ // First, so a version that the template pins wins.
96
+ npmDevDependencies: typescript
97
+ ? [ 'typescript', '@types/react', ...( npmDevDependencies ?? [] ) ]
98
+ : npmDevDependencies,
94
99
  customScripts,
95
100
  folderName: folderName.replace( /\$slug/g, slug ),
96
101
  editorScript,
@@ -114,6 +119,8 @@ module.exports = async (
114
119
  namespacePascalCase: pascalCase( transformedValues.namespace ),
115
120
  slugSnakeCase: snakeCase( transformedValues.slug ),
116
121
  slugPascalCase: pascalCase( transformedValues.slug ),
122
+ jsExt: transformedValues.typescript ? 'ts' : 'js',
123
+ jsxExt: transformedValues.typescript ? 'tsx' : 'jsx',
117
124
  ...variantVars,
118
125
  };
119
126
 
@@ -241,6 +248,11 @@ module.exports = async (
241
248
  info( '' );
242
249
  code( ' $ npm run lint:js' );
243
250
  info( ' Lints JavaScript files.' );
251
+ if ( view.typescript ) {
252
+ info( '' );
253
+ code( ' $ npm run typecheck' );
254
+ info( ' Checks TypeScript types.' );
255
+ }
244
256
  info( '' );
245
257
  code( ' $ npm run plugin-zip' );
246
258
  info( ' Creates a zip file for a WordPress plugin.' );
@@ -25,7 +25,7 @@ import './editor.scss';
25
25
  *
26
26
  * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
27
27
  *
28
- * @return {Element} Element to render.
28
+ * @return {{^typescript}}{Element} {{/typescript}}Element to render.
29
29
  */
30
30
  export default function Edit() {
31
31
  return (
@@ -25,13 +25,13 @@ import metadata from './block.json';
25
25
  */
26
26
  registerBlockType( metadata.name, {
27
27
  /**
28
- * @see ./edit.js
28
+ * @see ./edit.{{jsxExt}}
29
29
  */
30
30
  edit: Edit,
31
31
  {{#isStaticVariant}}
32
32
 
33
33
  /**
34
- * @see ./save.js
34
+ * @see ./save.{{jsxExt}}
35
35
  */
36
36
  save,
37
37
  {{/isStaticVariant}}
@@ -14,7 +14,7 @@ import { useBlockProps } from '@wordpress/block-editor';
14
14
  *
15
15
  * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
16
16
  *
17
- * @return {Element} Element to render.
17
+ * @return {{^typescript}}{Element} {{/typescript}}Element to render.
18
18
  */
19
19
  export default function save() {
20
20
  return (
@@ -0,0 +1,5 @@
1
+ {{#typescript}}
2
+ // Lets TypeScript accept style imports, which webpack bundles.
3
+ declare module '*.css';
4
+ declare module '*.scss';
5
+ {{/typescript}}
@@ -0,0 +1,18 @@
1
+ {{#typescript}}
2
+ {
3
+ "compilerOptions": {
4
+ "target": "es2022",
5
+ "lib": [ "es2022", "dom", "dom.iterable" ],
6
+ "module": "preserve",
7
+ "jsx": "react-jsx",
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true,
11
+ "resolveJsonModule": true,
12
+ "isolatedModules": true,
13
+ "verbatimModuleSyntax": true,
14
+ "moduleDetection": "force",
15
+ "types": []
16
+ }
17
+ }
18
+ {{/typescript}}
package/lib/templates.js CHANGED
@@ -51,6 +51,7 @@ const predefinedPluginTemplates = {
51
51
  },
52
52
  viewScript: 'file:./view.js',
53
53
  example: {},
54
+ typescript: false,
54
55
  folderName: './src/$slug',
55
56
  npmDependencies: [
56
57
  '@wordpress/block-editor',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/create-block",
3
- "version": "5.1.0",
3
+ "version": "5.3.0",
4
4
  "description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -52,5 +52,5 @@
52
52
  "publishConfig": {
53
53
  "access": "public"
54
54
  },
55
- "gitHead": "9b70363b9f93cff0583a1f3153738d383cf5a49e"
55
+ "gitHead": "7f19202f61aeba8f4bdce892a1a0df485ad76e2b"
56
56
  }