Skip to content

Commit b92cc1a

Browse files
committed
docs: use es module syntax for all configs in the docs
1 parent 28ccd11 commit b92cc1a

File tree

20 files changed

+115
-96
lines changed

20 files changed

+115
-96
lines changed

docs/docs/dev-server/middleware/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ You can use middleware to modify responses to any request from the browser, for
1616
<summary>Read more</summary>
1717

1818
```javascript
19-
const proxy = require('koa-proxies');
19+
import proxy from 'koa-proxies';
2020

21-
module.exports = {
21+
export default {
2222
port: 9000,
2323
middlewares: [
2424
proxy('/api', {
@@ -40,7 +40,7 @@ You can rewrite certain file requests using a simple middleware. This can be use
4040
Serve `/index.html` from `/src/index.html`:
4141

4242
```javascript
43-
module.exports = {
43+
export default {
4444
middlewares: [
4545
function rewriteIndex(context, next) {
4646
if (context.url === '/' || context.url === '/index.html') {

docs/docs/dev-server/plugins/esbuild.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ npm i -D @web/dev-server-esbuild
2121
Create a configuration file:
2222

2323
```js
24-
const { esbuildPlugin } = require('@web/dev-server-esbuild');
24+
import { esbuildPlugin } from '@web/dev-server-esbuild';
2525

26-
module.exports = {
26+
export default {
2727
plugins: [esbuildPlugin({ ts: true })],
2828
};
2929
```

docs/docs/dev-server/plugins/examples.md

+24-21
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ console.log(`The current version is: ${version}`);
5151
Add a plugin to serve the contents of this environment:
5252

5353
```js
54-
const packageJson = require('./package.json');
54+
import fs from 'fs';
55+
import path from 'path';
5556

56-
module.exports = {
57+
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
58+
59+
export default {
5760
plugins: [
5861
{
5962
name: 'env-vars',
@@ -80,12 +83,12 @@ Another approach is to replace constants or patterns in your code. We don't reco
8083
You can use the [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace) for replacing environment variables in your code. Make sure to add an `include` pattern to avoid processing files unnecessarily.
8184

8285
```js
83-
const rollupReplace = require('@rollup/plugin-replace');
84-
const { fromRollup } = require('@web/dev-server-rollup');
86+
import rollupReplace from '@rollup/plugin-replace';
87+
import { fromRollup } from '@web/dev-server-rollup';
8588

8689
const replace = fromRollup(rollupReplace);
8790

88-
module.exports = {
91+
export default {
8992
plugins: [replace({ include: ['src/**/*.js'], __environment__: '"development"' })],
9093
};
9194
```
@@ -100,7 +103,7 @@ Es modules are immutable, you cannot mock or stub them at runtime in the browser
100103
<summary>View example</summary>
101104

102105
```js
103-
module.exports = {
106+
export default {
104107
plugins: [
105108
{
106109
name: 'stub-package',
@@ -133,12 +136,12 @@ Note that the dev server and test runner already includes `esbuild` for compilin
133136
<summary>View example</summary>
134137

135138
```js
136-
const rollupBabel = require('@rollup/plugin-babel');
137-
const { fromRollup } = require('@web/dev-server-rollup');
139+
import rollupBabel from '@rollup/plugin-babel';
140+
import { fromRollup } from '@web/dev-server-rollup';
138141

139142
const babel = fromRollup(rollupBabel);
140143

141-
module.exports = {
144+
export default {
142145
plugins: [babel({ include: ['src/**/*.js'], plugins: ['babel-plugin-foo'] })],
143146
};
144147
```
@@ -159,12 +162,12 @@ To import JSON files you can use [@rollup/plugin-json](https://www.npmjs.com/pac
159162
In addition to installing the rollup plugin, we need to tell the dev server to serve json files as js modules:
160163

161164
```js
162-
const rollupJson = require('@rollup/plugin-json');
163-
const { fromRollup } = require('@web/dev-server-rollup');
165+
import rollupJson from '@rollup/plugin-json';
166+
import { fromRollup } from '@web/dev-server-rollup';
164167

165168
const json = fromRollup(rollupJson);
166169

167-
module.exports = {
170+
export default {
168171
// tell the server to serve json files as js
169172
mimeTypes: {
170173
'**/*.json': 'js',
@@ -187,12 +190,12 @@ There are a lot of ways to import CSS. For this example, we have tested two roll
187190

188191
```js
189192
/* eslint-disable */
190-
const rollupPostcss = require('rollup-plugin-postcss');
191-
const { fromRollup } = require('@web/dev-server-rollup');
193+
import rollupPostcss from 'rollup-plugin-postcss';
194+
import { fromRollup } from '@web/dev-server-rollup';
192195

193196
const postcss = fromRollup(rollupPostcss);
194197

195-
module.exports = {
198+
export default {
196199
// in a monorepo you need to adjust the rootdir of the web server
197200
// postcss injects a module which needs to be reachable from the browser
198201
// rootDir: '../..',
@@ -214,12 +217,12 @@ If you're using `lit-element`, you can use [rollup-plugin-lit-css](https://www.n
214217

215218
```js
216219
/* eslint-disable */
217-
const rollupLitcss = require('rollup-plugin-lit-css');
218-
const { fromRollup } = require('@web/dev-server-rollup');
220+
import rollupLitcss from 'rollup-plugin-lit-css';
221+
import { fromRollup } from '@web/dev-server-rollup';
219222

220223
const litcss = fromRollup(rollupLitcss);
221224

222-
module.exports = {
225+
export default {
223226
// tell the server to serve css files as js
224227
mimeTypes: {
225228
'**/*.css': 'js',
@@ -242,12 +245,12 @@ Make sure not to use the `limit` option, as this causes the plugin to emit files
242245

243246
```js
244247
/* eslint-disable */
245-
const rollupUrl = require('rollup-plugin-url');
246-
const { fromRollup } = require('@web/dev-server-rollup');
248+
import rollupUrl from 'rollup-plugin-url';
249+
import { fromRollup } from '@web/dev-server-rollup';
247250

248251
const url = fromRollup(rollupUrl);
249252

250-
module.exports = {
253+
export default {
251254
// tell the server to serve your assets files as js
252255
mimeTypes: {
253256
'./assets/**/*': 'js',

docs/docs/dev-server/plugins/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ A plugin is just an object that you add to the `plugins` array in your configura
1717
In your `web-dev-server.config.js` or `web-test-runner.config.js`:
1818

1919
```js
20-
const awesomePlugin = require('awesome-plugin');
20+
import awesomePlugin from 'awesome-plugin';
2121

22-
module.exports = {
22+
export default {
2323
plugins: [
2424
// use a plugin
2525
awesomePlugin({ someOption: 'someProperty' }),

docs/docs/dev-server/plugins/legacy.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ npm i --save-dev @web/dev-server-legacy
1717
Add the plugin to your config:
1818

1919
```js
20-
const { legacyPlugin } = require('@web/dev-server-legacy');
20+
import { legacyPlugin } from '@web/dev-server-legacy';
2121

22-
module.exports = {
22+
export default {
2323
plugins: [legacyPlugin()],
2424
};
2525
```

docs/docs/dev-server/plugins/rollup.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ npm i --save-dev @web/dev-server-rollup
2323
Import the rollup plugin and the `fromRollup` function in your configuration file. Then, wrap the rollup plugin with the adapter function:
2424

2525
```js
26-
const rollupReplace = require('@rollup/plugin-replace');
27-
const { fromRollup } = require('@web/dev-server-rollup');
26+
import rollupReplace from '@rollup/plugin-replace';
27+
import { fromRollup } from '@web/dev-server-rollup';
2828

2929
const replace = fromRollup(rollupReplace);
3030

31-
module.exports = {
31+
export default {
3232
plugins: [replace({ include: ['src/**/*.js'], __environment__: '"development"' })],
3333
};
3434
```
@@ -42,10 +42,10 @@ Some rollup plugins do expensive operations. During development, this matters a
4242
The rollup build process assumes that any imported files are meant to be compiled to JS, web dev server serves many different kinds of files to the browser. If you are transforming a non-standard filetype to JS, for example .json files, you need to instruct the server to handle it as a JS file:
4343

4444
```js
45-
const json = require('@rollup/plugin-json');
46-
const { rollupAdapter } = require('@web/dev-server-rollup');
45+
import json from '@rollup/plugin-json';
46+
import { rollupAdapter } from '@web/dev-server-rollup';
4747

48-
module.exports = {
48+
export default {
4949
mimeTypes: {
5050
// serve all json files as js
5151
'**/*.json': 'js',

docs/docs/dev-server/plugins/writing-plugins.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ A plugin is just an object that you add to the `plugins` array in your configura
1414
In your `web-dev-server.config.js` or `web-test-runner.config.js`:
1515

1616
```js
17-
const awesomePlugin = require('awesome-plugin');
17+
import awesomePlugin from 'awesome-plugin';
1818

19-
module.exports = {
19+
export default {
2020
plugins: [
2121
// use a plugin
2222
awesomePlugin({ someOption: 'someProperty' }),
@@ -99,7 +99,7 @@ Serve an auto generated `index.html`:
9999
```js
100100
const indexHTML = generateIndexHTML();
101101

102-
module.exports = {
102+
export default {
103103
plugins: [
104104
{
105105
name: 'my-plugin',
@@ -118,7 +118,7 @@ Serve a virtual module:
118118
```js
119119
const indexHTML = generateIndexHTML();
120120

121-
module.exports = {
121+
export default {
122122
plugins: [
123123
{
124124
name: 'my-plugin',
@@ -135,7 +135,7 @@ module.exports = {
135135
The file extension is used to infer the mime type to respond with. If you are using a non-standard file extension you need to use the `type` property to set it explicitly:
136136

137137
```js
138-
module.exports = {
138+
export default {
139139
plugins: [
140140
{
141141
name: 'my-plugin',
@@ -163,7 +163,7 @@ The dev server guesses the MIME type based on the file extension. When serving v
163163
The returned MIME type can be a file extension, this will be used to set the corresponding default MIME type. For example `js` resolves to `application/javascript` and `css` to `text/css`.
164164

165165
```js
166-
module.exports = {
166+
export default {
167167
plugins: [
168168
{
169169
name: 'my-plugin',
@@ -190,7 +190,7 @@ module.exports = {
190190
You can use a mime type shorthand, such as `js` or `css`. Koa will resolve this to the full mimetype. It is also possible to set the full mime type directly:
191191

192192
```js
193-
module.exports = {
193+
export default {
194194
plugins: [
195195
{
196196
name: 'my-plugin',
@@ -220,7 +220,7 @@ In a web server, the response body is not always a string, but it can be a binar
220220
Rewrite the base path of your application for local development;
221221

222222
```js
223-
module.exports = {
223+
export default {
224224
plugins: [
225225
{
226226
name: 'my-plugin',
@@ -238,7 +238,7 @@ module.exports = {
238238
Inject a script to set global variables during local development:
239239

240240
```js
241-
module.exports = {
241+
export default {
242242
plugins: [
243243
{
244244
name: 'my-plugin',
@@ -259,9 +259,11 @@ module.exports = {
259259
Inject environment variables into a JS module:
260260

261261
```js
262-
const packageJson = require('./package.json');
262+
import fs from 'fs';
263263

264-
module.exports = {
264+
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
265+
266+
export default {
265267
plugins: [
266268
{
267269
name: 'my-plugin',
@@ -278,9 +280,9 @@ module.exports = {
278280
Transform markdown to HTML:
279281

280282
```js
281-
const markdownToHTML = require('markdown-to-html-library');
283+
import { markdownToHTML } from 'markdown-to-html-library';
282284

283-
module.exports = {
285+
export default {
284286
plugins: [
285287
{
286288
name: 'my-plugin',
@@ -308,7 +310,7 @@ module.exports = {
308310
Polyfill CSS modules in JS:
309311

310312
```js
311-
module.exports = {
313+
export default {
312314
plugins: [
313315
{
314316
name: 'my-plugin',
@@ -348,7 +350,7 @@ The dev server already resolves module imports when the `--node-resolve` flag is
348350
The hook receives the import string and should return the string to replace it with. This should be a browser-compatible path, not a file path.
349351

350352
```js
351-
module.exports = {
353+
export default {
352354
plugins: [
353355
{
354356
name: 'my-plugin',
@@ -394,17 +396,17 @@ function myFancyPlugin() {
394396
};
395397
}
396398

397-
module.exports = {
399+
export default {
398400
plugins: [myFancyPlugin()],
399401
};
400402
```
401403

402404
Boot up another server for proxying in serverStart:
403405

404406
```js
405-
const proxy = require('koa-proxies');
407+
import proxy from 'koa-proxies';
406408

407-
module.exports = {
409+
export default {
408410
plugins: [
409411
{
410412
name: 'my-plugin',
@@ -443,7 +445,7 @@ function myFancyPlugin() {
443445
};
444446
}
445447

446-
module.exports = {
448+
export default {
447449
plugins: [myFancyPlugin()],
448450
};
449451
```

docs/docs/test-runner/browsers/browserstack.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ For modern browsers, we recommend using other browser launchers, as they are a l
1313
## Usage
1414

1515
```js
16-
const { browserstackLauncher } = require('@web/test-runner-browserstack');
16+
import { browserstackLauncher } from '@web/test-runner-browserstack';
1717

1818
const sharedCapabilities = {
1919
// it's recommended to store user and key as environment variables
@@ -28,7 +28,7 @@ const sharedCapabilities = {
2828
build: `build ${process.env.GITHUB_RUN_NUMBER || 'unknown'}`,
2929
};
3030

31-
module.exports = {
31+
export default {
3232
browsers: [
3333
browserstackLauncher({
3434
capabilities: {

docs/docs/test-runner/browsers/chrome.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ For other projects you can install the browser launcher by running:
2323
And included in your config:
2424

2525
```js
26-
const { chromeLauncher } = require('@web/test-runner-chrome');
26+
import { chromeLauncher } from '@web/test-runner-chrome';
2727

28-
module.exports = {
28+
export default {
2929
browsers: [chromeLauncher()],
3030
};
3131
```
@@ -37,9 +37,9 @@ If you want to customize the puppeteer launcher options, you can add the browser
3737
You can find all possible launch options in the [official documentation](https://github.com/microsoft/puppeteer/blob/master/docs/api.md#browsertypelaunchoptions)
3838

3939
```js
40-
const { chromeLauncher } = require('@web/test-runner-chrome');
40+
import { chromeLauncher } from '@web/test-runner-chrome';
4141

42-
module.exports = {
42+
export default {
4343
browsers: [
4444
chromeLauncher({
4545
launchOptions: {

0 commit comments

Comments
 (0)