Skip to content

Commit cd9568f

Browse files
authored
Use ESM (#5396)
* upgrade remark-emoji * remove WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG * migrate build-content-tree * fix import * use mjs * read json * use mjs * fix __dirname * fix __dirname * fix lodash * fix __dirname * fix __dirname * upgrade remark * fix import * upgrade remark-html * upgrade remark-gfm * Revert "upgrade remark-gfm" This reverts commit 88d9713. * upgrade remark-autolink-headings * update remark-slug * upgrade unist-util-visit * clean up * fix extension * upgrade remark-gfm * Revert "upgrade remark-gfm" This reverts commit 7a3554e. * fix __dirname * fix __dirname * fix tests for jest
1 parent 820557a commit cd9568f

33 files changed

+809
-241
lines changed

jest.config.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import jestConfig from 'jest-config';
2+
export default {
3+
verbose: true,
4+
transform: {},
5+
moduleFileExtensions: [...jestConfig.defaults.moduleFileExtensions, 'mjs'],
6+
testMatch: [
7+
'**/__tests__/**/*.[jt]s?(x)',
8+
'**/?(*.)+(spec|test).[tj]s?(x)',
9+
'**/*.test.mjs',
10+
],
11+
};

package.json

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
"clean-printable": "rimraf src/content/**/printable.mdx",
2929
"preclean": "run-s clean-dist clean-printable",
3030
"clean": "rimraf src/content/**/_*.mdx src/**/_*.json repositories/*.json",
31-
"start": "npm run clean-dist && webpack serve --config webpack.dev.js --env dev --progress --node-env development",
32-
"content": "node src/scripts/build-content-tree.js ./src/content ./src/_content.json",
33-
"bundle-analyze": "run-s clean fetch content && webpack --config webpack.prod.js --node-env production && run-s printable content && webpack --config webpack.ssg.js --node-env production --env ssg --profile --json > stats.json && webpack-bundle-analyzer stats.json",
34-
"fetch-repos": "node src/utilities/fetch-package-repos.js",
31+
"start": "npm run clean-dist && webpack serve --config webpack.dev.mjs --env dev --progress --node-env development",
32+
"content": "node src/scripts/build-content-tree.mjs ./src/content ./src/_content.json",
33+
"bundle-analyze": "run-s clean fetch content && webpack --config webpack.prod.mjs --node-env production && run-s printable content && webpack --config webpack.ssg.mjs --node-env production --env ssg --profile --json > stats.json && webpack-bundle-analyzer stats.json",
34+
"fetch-repos": "node src/utilities/fetch-package-repos.mjs",
3535
"fetch": "run-p fetch:*",
36-
"fetch:readmes": "node src/utilities/fetch-package-readmes.js",
37-
"fetch:supporters": "node src/utilities/fetch-supporters.js",
36+
"fetch:readmes": "node src/utilities/fetch-package-readmes.mjs",
37+
"fetch:supporters": "node src/utilities/fetch-supporters.mjs",
3838
"prebuild": "npm run clean",
39-
"build": "run-s fetch-repos fetch content && webpack --config webpack.prod.js --node-env production && run-s printable content && webpack --config webpack.ssg.js --node-env production --env ssg",
39+
"build": "run-s fetch-repos fetch content && webpack --config webpack.prod.mjs --node-env production && run-s printable content && webpack --config webpack.ssg.mjs --node-env production --env ssg",
4040
"postbuild": "npm run sitemap",
4141
"build-test": "npm run build && http-server --port 4200 dist/",
4242
"test": "npm run lint",
@@ -50,8 +50,8 @@
5050
"sitemap": "cd dist && sitemap-static --ignore-file=../sitemap-ignore.json --pretty --prefix=https://webpack.js.org/ > sitemap.xml",
5151
"serve": "npm run build && sirv start ./dist --port 4000",
5252
"preprintable": "npm run clean-printable",
53-
"printable": "node ./src/scripts/concatenate-docs.js",
54-
"jest": "jest",
53+
"printable": "node ./src/scripts/concatenate-docs.mjs",
54+
"jest": "NODE_OPTIONS=--experimental-vm-modules jest --config=jest.config.mjs",
5555
"cypress:open": "cypress open",
5656
"cypress:run": "cypress run",
5757
"cypress:ci": "start-server-and-test build-test http://localhost:4200 cypress:run",
@@ -108,22 +108,22 @@
108108
"mini-css-extract-plugin": "^2.3.0",
109109
"mkdirp": "^1.0.4",
110110
"modularscale-sass": "^3.0.3",
111-
"node-fetch": "^2.6.1",
111+
"node-fetch": "^3.0.0",
112112
"npm-run-all": "^4.1.1",
113113
"postcss": "^8.3.6",
114114
"postcss-loader": "^6.1.1",
115115
"prettier": "^2.4.1",
116116
"react-refresh": "^0.10.0",
117117
"redirect-webpack-plugin": "^1.0.0",
118-
"remark": "^13.0.0",
119-
"remark-autolink-headings": "^6.1.0",
120-
"remark-emoji": "^2.2.0",
118+
"remark": "^14.0.1",
119+
"remark-autolink-headings": "7.0.1",
120+
"remark-emoji": "^3.0.1",
121121
"remark-extract-anchors": "1.1.1",
122122
"remark-frontmatter": "^3.0.0",
123123
"remark-gfm": "^1.0.0",
124-
"remark-html": "^13.0.2",
124+
"remark-html": "^14.0.0",
125125
"remark-refractor": "montogeek/remark-refractor",
126-
"remark-slug": "^6.1.0",
126+
"remark-slug": "^7.0.0",
127127
"rimraf": "^3.0.2",
128128
"sass": "^1.41.1",
129129
"sass-loader": "^12.1.0",
@@ -134,7 +134,7 @@
134134
"style-loader": "^3.2.1",
135135
"tailwindcss": "^2.2.15",
136136
"tap-spot": "^1.1.1",
137-
"unist-util-visit": "^2.0.3",
137+
"unist-util-visit": "^4.0.0",
138138
"webpack": "^5.53.0",
139139
"webpack-bundle-analyzer": "^4.4.2",
140140
"webpack-cli": "^4.8.0",
@@ -155,7 +155,6 @@
155155
"react-tiny-popover": "5",
156156
"react-use": "^17.3.1",
157157
"react-visibility-sensor": "^5.0.2",
158-
"unist-util-visit": "^2.0.3",
159158
"webpack-pwa-manifest": "^4.3.0",
160159
"webpack.vote": "https://github.com/webpack/voting-app.git",
161160
"workbox-window": "^6.3.0"

src/ProdAssetsManifest.js renamed to src/ProdAssetsManifest.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { Compilation, sources } = require('webpack');
1+
import webpack from 'webpack';
2+
const { Compilation, sources } = webpack;
23

34
// collect assets data (vendor.[contenthash].js and index.[contenthash].js) for ssg
45
class ProdAssetsManifest {
@@ -46,4 +47,4 @@ class ProdAssetsManifest {
4647
});
4748
}
4849
}
49-
module.exports = ProdAssetsManifest;
50+
export default ProdAssetsManifest;

src/components/Site/Site.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
extractSections,
1111
getPageDescription,
1212
getPageTitle,
13-
} from '../../utilities/content-utils';
13+
} from '../../utilities/content-utils.mjs';
1414
import isClient from '../../utilities/is-client';
15-
import getAdjacentPages from '../../utilities/get-adjacent-pages';
15+
import getAdjacentPages from '../../utilities/get-adjacent-pages/index.mjs';
1616

1717
// Import Components
1818
import NotificationBar from '../NotificationBar/NotificationBar';

src/remark-plugins/remark-cleanup-readme/index.js renamed to src/remark-plugins/remark-cleanup-readme/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const visit = require('unist-util-visit');
1+
import { visit } from 'unist-util-visit';
22
// remove items other than paragraphs in div[align="center"]
33
// e.g., logo, title and so on.
44
// see the original version at https://github.com/webpack/webpack.js.org/blob/webpack-4/src/utilities/process-readme.js#L71
5-
module.exports = function remarkCleanupReadme() {
5+
export default function remarkCleanupReadme() {
66
return function transformer(tree) {
77
visit(tree, 'mdxJsxFlowElement', visitor);
88
function visitor(node) {
@@ -20,4 +20,4 @@ module.exports = function remarkCleanupReadme() {
2020
}
2121
}
2222
};
23-
};
23+
}

src/remark-plugins/remark-cleanup-readme/index.test.js renamed to src/remark-plugins/remark-cleanup-readme/index.test.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const mdx = require('@mdx-js/mdx');
1+
import mdx from '@mdx-js/mdx';
2+
import CleanupReadme from './index.mjs';
23
describe('cleanup readme', () => {
34
it('should clean up div[align="center"] block without paragraph', () => {
45
const mdxText = `
@@ -12,7 +13,7 @@ describe('cleanup readme', () => {
1213
</div>
1314
`;
1415
const html = mdx.sync(mdxText, {
15-
remarkPlugins: [require('./index.js')],
16+
remarkPlugins: [CleanupReadme],
1617
});
1718
expect(html).toMatchSnapshot();
1819
});
@@ -32,7 +33,7 @@ describe('cleanup readme', () => {
3233
</div>
3334
`;
3435
const html = mdx.sync(mdxText, {
35-
remarkPlugins: [require('./index.js')],
36+
remarkPlugins: [CleanupReadme],
3637
});
3738
expect(html).toMatchSnapshot();
3839
});
@@ -67,7 +68,7 @@ describe('cleanup readme', () => {
6768
</div>
6869
`;
6970
const html = mdx.sync(mdxText, {
70-
remarkPlugins: [require('./index.js')],
71+
remarkPlugins: [CleanupReadme],
7172
});
7273
expect(html).toMatchSnapshot();
7374
});

src/remark-plugins/remark-custom-asides/index.js renamed to src/remark-plugins/remark-custom-asides/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* based on https://github.com/montogeek/remark-custom-blockquotes
33
*/
44

5-
const visit = require('unist-util-visit');
6-
module.exports = function customAsides(
5+
import { visit } from 'unist-util-visit';
6+
export default function customAsides(
77
options = {
88
mapping: {},
99
}
@@ -67,4 +67,4 @@ module.exports = function customAsides(
6767
}
6868
}
6969
};
70-
};
70+
}

src/remark-plugins/remark-custom-asides/index.test.js renamed to src/remark-plugins/remark-custom-asides/index.test.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import remark from 'remark';
1+
import { remark } from 'remark';
2+
import remarkHtml from 'remark-html';
3+
import Plugin from './index.mjs';
24
describe('customize blockquote', () => {
35
it('should transform W> into aside of warning', () => {
46
remark()
5-
.use(require('./index.js'), {
7+
.use(Plugin, {
68
mapping: {
79
'W>': 'warning',
810
},
911
})
10-
.use(require('remark-html'))
12+
.use(remarkHtml)
1113
.process(
1214
`
1315
W> hello world
1416
`,
15-
function (err, { contents }) {
17+
function (err, { value: contents }) {
1618
expect(err).toBeNull();
1719
expect(contents).toContain('<aside class="warning"');
1820
expect(contents).toContain('<h6 class="warning__prefix"');

src/remark-plugins/remark-responsive-table/remark-responsive-table.js renamed to src/remark-plugins/remark-responsive-table/remark-responsive-table.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// this plugin was first based on https://github.com/montogeek/remark-responsive-tables
2-
const visit = require('unist-util-visit');
3-
module.exports = function remarkResponsiveTable() {
2+
import { visit } from 'unist-util-visit';
3+
export default function remarkResponsiveTable() {
44
return function transformer(tree) {
55
visit(tree, 'table', visitor);
66
function visitor(node) {
@@ -40,4 +40,4 @@ module.exports = function remarkResponsiveTable() {
4040
});
4141
}
4242
};
43-
};
43+
}

src/remark-plugins/remark-responsive-table/remark-responsive-table.test.js renamed to src/remark-plugins/remark-responsive-table/remark-responsive-table.test.mjs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import remark from 'remark';
1+
import { describe, expect } from '@jest/globals';
2+
3+
import { remark } from 'remark';
4+
import remarkHtml from 'remark-html';
5+
import remarkGfm from 'remark-gfm';
6+
import remarkResponsiveTable from './remark-responsive-table.mjs';
27
describe('responsive table', () => {
38
const processor = remark()
4-
.use(require('remark-gfm'))
5-
.use(require('./remark-responsive-table.js'))
6-
.use(require('remark-html'));
9+
.use(remarkGfm)
10+
.use(remarkResponsiveTable)
11+
.use(remarkHtml);
712
it('should add data-th', () => {
813
processor.process(
914
`
@@ -12,7 +17,7 @@ describe('responsive table', () => {
1217
| baz | |
1318
| sam | chen |
1419
`,
15-
(error, { contents }) => {
20+
(error, { value: contents }) => {
1621
expect(error).toBeNull();
1722
expect(contents).toContain('data-th="foo"');
1823
expect(contents).toContain('data-th="bar"');
@@ -29,7 +34,7 @@ describe('responsive table', () => {
2934
| baz | |
3035
| sam | chen |
3136
`,
32-
(error, { contents }) => {
37+
(error, { value: contents }) => {
3338
expect(error).toBeNull();
3439
expect(contents).toMatchSnapshot();
3540
}

src/scripts/build-content-tree.js renamed to src/scripts/build-content-tree.mjs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#!/usr/bin/env node
22
// ./build-content-tree source output
33
// ./build-content-tree "./src/content" ".src/_content.json"
4-
5-
const directoryTree = require('directory-tree');
6-
const fs = require('fs');
7-
const path = require('path');
4+
import directoryTree from 'directory-tree';
5+
import fs from 'fs';
6+
import path from 'path';
87

98
// Import Utils
10-
const { restructure } = require('../utilities/content-tree-enhancers.js');
9+
import { restructure } from '../utilities/content-tree-enhancers.mjs';
1110

12-
if (require.main === module) {
13-
main();
14-
} else {
15-
module.exports = buildContentTree;
16-
}
11+
// if (require.main === module) {
12+
// main();
13+
// } else {
14+
// module.exports = buildContentTree;
15+
// }
1716

1817
function main() {
1918
const source = process.argv[2];
2019
const output = process.argv[3];
2120
buildContentTree(source, output);
2221
}
2322

23+
main();
24+
2425
function buildContentTree(source, output) {
2526
if (!source) {
2627
return console.error('build-content-tree: you must provide a source path');

src/scripts/concatenate-docs.js renamed to src/scripts/concatenate-docs.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const os = require('os');
4-
const front = require('front-matter');
1+
import fs from 'fs';
2+
import path from 'path';
3+
import os from 'os';
4+
import front from 'front-matter';
55

66
// root path
77
const rootPath = path.join('src', 'content');

src/utilities/constants.js renamed to src/utilities/constants.mjs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const excludedLoaders = [
1+
export const excludedLoaders = [
22
'webpack-contrib/config-loader',
33
'webpack-contrib/eslint-loader',
44
'webpack-contrib/transform-loader',
@@ -25,7 +25,7 @@ const excludedLoaders = [
2525
'webpack-contrib/istanbul-instrumenter-loader',
2626
'webpack-contrib/worker-loader',
2727
];
28-
const excludedPlugins = [
28+
export const excludedPlugins = [
2929
'webpack-contrib/component-webpack-plugin',
3030
'webpack-contrib/extract-text-webpack-plugin',
3131
'webpack-contrib/i18n-webpack-plugin',
@@ -34,8 +34,3 @@ const excludedPlugins = [
3434
'webpack-contrib/zopfli-webpack-plugin',
3535
'webpack-contrib/closure-webpack-plugin',
3636
];
37-
38-
module.exports = {
39-
excludedLoaders,
40-
excludedPlugins,
41-
};

0 commit comments

Comments
 (0)