Skip to content

Commit 0b298ae

Browse files
authored
Feat/config types (#1624)
1 parent 0509aa9 commit 0b298ae

File tree

8 files changed

+210
-232
lines changed

8 files changed

+210
-232
lines changed

.changeset/big-walls-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'preact-cli': patch
3+
---
4+
5+
Added typings for users to use in their preact.config.js files

.eslintrc

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,13 @@
4040
"react/jsx-no-undef": 2,
4141
"react/jsx-uses-react": 2,
4242
"react/jsx-uses-vars": 2
43-
}
43+
},
44+
"overrides": [
45+
{
46+
"files": ["**/*.ts"],
47+
"rules": {
48+
"no-undef": "off"
49+
}
50+
}
51+
]
4452
}

README.md

+13-30
Original file line numberDiff line numberDiff line change
@@ -219,39 +219,21 @@ To customize preact-cli create a `preact.config.js` or a `preact.config.json` fi
219219
```js
220220
// ... imports or other code up here ...
221221

222-
// these props are both optional
223-
export default {
224-
// you can add preact-cli plugins here
225-
plugins: [
226-
// either a function
227-
// (you'd probably import this because you can use the `webpack` function instead of an inline plugin)
228-
function () {},
229-
// strings also work (they get imported by preact-cli), useful for the json config
230-
'plugin-name',
231-
// with options
232-
[
233-
'plugin-name',
234-
{
235-
option: true,
236-
},
237-
],
238-
],
239-
/**
240-
* Function that mutates the original webpack config.
241-
* Supports asynchronous changes when a promise is returned (or it's an async function).
242-
*
243-
* @param {object} config - original webpack config.
244-
* @param {object} env - options passed to the CLI.
245-
* @param {WebpackConfigHelpers} helpers - object with useful helpers for working with the webpack config.
246-
* @param {object} options - this is mainly relevant for plugins (will always be empty in the config), default to an empty object
247-
**/
248-
webpack(config, env, helpers, options) {
249-
/** you can change the config here **/
250-
},
222+
/**
223+
* Function that mutates the original webpack config.
224+
* Supports asynchronous changes when a promise is returned (or it's an async function).
225+
*
226+
* @param {import('preact-cli').Config} config - original webpack config
227+
* @param {import('preact-cli').Env} env - current environment and options pass to the CLI
228+
* @param {import('preact-cli').Helpers} helpers - object with useful helpers for working with the webpack config
229+
* @param {Record<string, unknown>} options - this is mainly relevant for plugins (will always be empty in the config), default to an empty object
230+
*/
231+
export default (config, env, helpers, options) => {
232+
/** you can change the config here **/
251233
};
252234
```
253235

254-
See [WebpackConfigHelpers] docs for more info on `helpers` argument which contains methods to find various parts of configuration. Additionally see our [recipes wiki] containing examples on how to change webpack configuration.
236+
See [Webpack config helpers wiki] for more info on the `helpers` argument which contains methods to find various parts of configuration. Additionally see our [recipes wiki] containing examples on how to change webpack configuration.
255237

256238
#### Prerender multiple routes
257239

@@ -397,6 +379,7 @@ Automatic code splitting is applied to all JavaScript and TypeScript files in th
397379
[plugins wiki]: https://github.com/preactjs/preact-cli/wiki/Plugins
398380
[preactjs-templates organization]: https://github.com/preactjs-templates
399381
[preactjs-templates/default]: https://github.com/preactjs-templates/default
382+
[webpack config helpers wiki]: https://github.com/preactjs/preact-cli/wiki/Webpack-Config-Helpers
400383
[recipes wiki]: https://github.com/preactjs/preact-cli/wiki/Config-Recipes
401384
[prpl]: https://developers.google.com/web/fundamentals/performance/prpl-pattern
402385
[`babel-preset-env`]: https://github.com/babel/babel-preset-env#targetsbrowsers

docs/webpack-helpers.md

-154
This file was deleted.

packages/cli/lib/lib/webpack/transform-config.js

+15-46
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ module.exports = async function (env, webpackConfig, isServer = false) {
139139
/**
140140
* WebpackConfigHelpers
141141
*
142+
* @typedef {import('../../../types.js').Helpers} Helpers
143+
*
142144
* @class WebpackConfigHelpers
143145
*/
144146
class WebpackConfigHelpers {
@@ -150,7 +152,8 @@ class WebpackConfigHelpers {
150152
* Webpack module used to create config.
151153
*
152154
* @readonly
153-
* @returns {object}
155+
* @type {Helpers['webpack']}
156+
*
154157
* @memberof WebpackConfigHelpers
155158
*/
156159
get webpack() {
@@ -160,8 +163,7 @@ class WebpackConfigHelpers {
160163
/**
161164
* Returns wrapper around all loaders from config.
162165
*
163-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
164-
* @returns {LoaderWrapper[]}
166+
* @type {Helpers['getLoaders']}
165167
*
166168
* @memberof WebpackConfigHelpers
167169
*/
@@ -176,8 +178,7 @@ class WebpackConfigHelpers {
176178
/**
177179
* Returns wrapper around all rules from config.
178180
*
179-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
180-
* @returns {RuleWrapper[]}
181+
* @type {Helpers['getRules']}
181182
*
182183
* @memberof WebpackConfigHelpers
183184
*/
@@ -191,8 +192,7 @@ class WebpackConfigHelpers {
191192
/**
192193
* Returns wrapper around all plugins from config.
193194
*
194-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
195-
* @returns {PluginWrapper[]}
195+
* @type {Helpers['getPlugins']}
196196
*
197197
* @memberof WebpackConfigHelpers
198198
*/
@@ -201,11 +201,9 @@ class WebpackConfigHelpers {
201201
}
202202

203203
/**
204+
* Returns wrapper around all rules that match provided file.
204205
*
205-
*
206-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
207-
* @param {string} file - path to test against loader. Resolved relatively to $PWD.
208-
* @returns {RuleWrapper[]}
206+
* @type {Helpers['getRulesByMatchingFile']}
209207
*
210208
* @memberof WebpackConfigHelpers
211209
*/
@@ -221,9 +219,8 @@ class WebpackConfigHelpers {
221219
*
222220
* @example
223221
* helpers.getLoadersByName(config, 'less-loader')
224-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
225-
* @param {string} name - name of loader.
226-
* @returns {LoaderWrapper[]}
222+
*
223+
* @type {Helpers['getLoadersByName']}
227224
*
228225
* @memberof WebpackConfigHelpers
229226
*/
@@ -252,9 +249,8 @@ class WebpackConfigHelpers {
252249
*
253250
* @example
254251
* helpers.getPluginsByName(config, 'HtmlWebpackPlugin')
255-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
256-
* @param {string} name - name of loader.
257-
* @returns {PluginWrapper[]}
252+
*
253+
* @type {Helpers['getPluginsByName']}
258254
*
259255
* @memberof WebpackConfigHelpers
260256
*/
@@ -270,39 +266,12 @@ class WebpackConfigHelpers {
270266
*
271267
* @example
272268
* helpers.getPluginsByType(config, webpack.optimize.CommonsChunkPlugin)
273-
* @param {object} config - [webpack config](https://webpack.js.org/configuration/#options).
274-
* @param {any} type - type of plugin.
275-
* @returns {PluginWrapper[]}
269+
*
270+
* @type {Helpers['getPluginsByType']}
276271
*
277272
* @memberof WebpackConfigHelpers
278273
*/
279274
getPluginsByType(config, type) {
280275
return this.getPlugins(config).filter(w => w.plugin instanceof type);
281276
}
282277
}
283-
284-
/**
285-
* Wrapper around webpack's [loader entry](https://webpack.js.org/configuration/module/#useentry).
286-
*
287-
* @typedef {object} LoaderWrapper
288-
* @property {object} rule - [rule entry](https://webpack.js.org/configuration/module/#module-rules).
289-
* @property {number} ruleIndex - index of rule in config.
290-
* @property {object} loader - [loader entry](https://webpack.js.org/configuration/module/#useentry).
291-
* @property {number} loaderIndex - index of loader in rule.
292-
*/
293-
294-
/**
295-
* Wrapper around webpack's [rule](https://webpack.js.org/configuration/module/#module-rules).
296-
*
297-
* @typedef {object} RuleWrapper
298-
* @property {object} rule - [rule entry](https://webpack.js.org/configuration/module/#module-rules).
299-
* @property {number} index - index of rule in config.
300-
*/
301-
302-
/**
303-
* Wrapper around webpack's [plugin](https://webpack.js.org/configuration/plugins/#plugins).
304-
*
305-
* @typedef {object} PluginWrapper
306-
* @property {object} plugin - [plugin entry](https://webpack.js.org/configuration/plugins/#plugins).
307-
* @property {number} index - index of plugin in config.
308-
*/

packages/cli/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"bin": {
88
"preact": "lib/index.js"
99
},
10+
"types": "types.d.ts",
1011
"scripts": {
1112
"pretest": "rimraf ./tests/output",
1213
"test": "jest"
@@ -80,6 +81,7 @@
8081
"@preact/async-loader": "^3.0.1",
8182
"@prefresh/babel-plugin": "^0.4.1",
8283
"@prefresh/webpack": "^3.2.2",
84+
"@types/webpack": "^4.38.0",
8385
"autoprefixer": "^10.2.5",
8486
"babel-esm-plugin": "^0.9.0",
8587
"babel-loader": "^8.2.2",

0 commit comments

Comments
 (0)