Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: migrate all packages to ESM-only #5816

Closed
wants to merge 16 commits into from
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ examples/
packages/lqip-loader/lib/
packages/docusaurus/lib/
packages/docusaurus-*/lib/*
packages/docusaurus-*/lib-next/
packages/docusaurus-plugin-ideal-image/copyUntypedFiles.js

packages/create-docusaurus/lib/*
Expand Down
5 changes: 3 additions & 2 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module.exports = {
{
paths: [
{
name: 'lodash',
name: 'lodash-es',
importNames: [
// 'compact', // TODO: TS doesn't make Boolean a narrowing function yet, so filter(Boolean) is problematic type-wise
'filter',
Expand All @@ -165,6 +165,7 @@ module.exports = {
{
files: [
'packages/create-docusaurus/templates/**/*.js',
'packages/create-docusaurus/templates/**/*.cjs',
'packages/create-docusaurus/templates/**/*.ts',
'packages/create-docusaurus/templates/**/*.tsx',
],
Expand All @@ -181,7 +182,7 @@ module.exports = {
},
},
{
files: ['*.js'],
files: ['*.js', '*.cjs'],
rules: {
// Make JS code directly runnable in Node.
'@typescript-eslint/no-var-requires': OFF,
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ packages/create-docusaurus/lib/
packages/lqip-loader/lib/
packages/docusaurus/lib/
packages/docusaurus-*/lib/*
packages/docusaurus-*/lib-next/

website/netlifyDeployPreview/*
!website/netlifyDeployPreview/index.html
Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ coverage
packages/lqip-loader/lib/
packages/docusaurus/lib/
packages/docusaurus-*/lib/*
packages/docusaurus-*/lib-next/
packages/create-docusaurus/lib/*
packages/create-docusaurus/templates/*/docusaurus.config.js
__fixtures__
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions admin/new.docusaurus.io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "new.docusaurus.io",
"version": "2.0.0-beta.9",
"private": true,
"type": "module",
"scripts": {
"start": "netlify dev"
},
Expand Down
6 changes: 0 additions & 6 deletions babel.config.js → babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,4 @@ module.exports = {
'@babel/react',
'@babel/preset-typescript',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
],
};
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions generateExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
const rimraf = require('rimraf');
const {readFileSync, writeFileSync, readdirSync} = require('fs');
const {execSync} = require('child_process');
import rimraf from 'rimraf';
import {readFileSync, writeFileSync, readdirSync} from 'fs';
import {execSync} from 'child_process';

// Generate one example per init template
// We use those generated examples as CodeSandbox projects
Expand Down
7 changes: 2 additions & 5 deletions jest.config.js → jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

const path = require('path');

const ignorePatterns = [
'/node_modules/',
'__fixtures__',
Expand All @@ -17,19 +15,18 @@ const ignorePatterns = [
'/packages/docusaurus-plugin-content-docs/lib',
'/packages/docusaurus-plugin-content-pages/lib',
'/packages/docusaurus-theme-classic/lib',
'/packages/docusaurus-theme-classic/lib-next',
'/packages/docusaurus-migrate/lib',
];

module.exports = {
rootDir: path.resolve(__dirname),
rootDir: __dirname,
verbose: true,
testURL: 'http://localhost/',
testEnvironment: 'node',
testPathIgnorePatterns: ignorePatterns,
coveragePathIgnorePatterns: ignorePatterns,
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
'^.+\\.[cm]?[jt]sx?$': 'babel-jest',
},
setupFiles: ['./jest/stylelint-rule-test.js', './jest/polyfills.js'],
moduleNameMapper: {
Expand Down
20 changes: 10 additions & 10 deletions jest/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/no-extraneous-dependencies */
require('@formatjs/intl-pluralrules/polyfill');
require('@formatjs/intl-pluralrules/locale-data/en');
require('@formatjs/intl-pluralrules/locale-data/fr');
import '@formatjs/intl-pluralrules/polyfill';
import '@formatjs/intl-pluralrules/locale-data/en';
import '@formatjs/intl-pluralrules/locale-data/fr';

require('@formatjs/intl-numberformat/polyfill');
require('@formatjs/intl-numberformat/locale-data/en');
require('@formatjs/intl-numberformat/locale-data/fr');
import '@formatjs/intl-numberformat/polyfill';
import '@formatjs/intl-numberformat/locale-data/en';
import '@formatjs/intl-numberformat/locale-data/fr';

require('@formatjs/intl-datetimeformat/polyfill');
require('@formatjs/intl-datetimeformat/add-all-tz');
require('@formatjs/intl-datetimeformat/locale-data/en');
require('@formatjs/intl-datetimeformat/locale-data/fr');
import '@formatjs/intl-datetimeformat/polyfill';
import '@formatjs/intl-datetimeformat/add-all-tz';
import '@formatjs/intl-datetimeformat/locale-data/en';
import '@formatjs/intl-datetimeformat/locale-data/fr';
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "root",
"private": true,
"type": "module",
"workspaces": [
"packages/*",
"website",
Expand Down Expand Up @@ -53,10 +54,13 @@
"test": "cross-env TZ=UTC jest",
"test:build:website": "./admin/scripts/test-release.sh",
"watch": "yarn lerna run --parallel watch",
"clear": "(yarn workspace website clear || echo 'Failure while running docusaurus clear') && yarn lerna exec --ignore docusaurus yarn rimraf lib lib-next",
"clear": "(yarn workspace website clear || echo 'Failure while running docusaurus clear') && yarn lerna exec --ignore docusaurus yarn rimraf lib",
"test:baseUrl": "yarn build:website:baseUrl && yarn serve:website:baseUrl",
"lock:update": "npx yarn-deduplicate"
},
"dependencies": {
"rimraf": "^3.0.2"
},
"devDependencies": {
"@babel/cli": "^7.15.7",
"@babel/core": "^7.12.16",
Expand All @@ -74,7 +78,7 @@
"@types/fs-extra": "^9.0.6",
"@types/jest": "^26.0.20",
"@types/loader-utils": "^2.0.3",
"@types/lodash": "^4.14.168",
"@types/lodash-es": "^4.17.5",
"@types/node": "^14.14.22",
"@types/prismjs": "^1.16.2",
"@types/prompts": "^2.0.9",
Expand Down Expand Up @@ -113,11 +117,10 @@
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-test-renderer": "^17.0.2",
"rimraf": "^3.0.2",
"serve": "^12.0.1",
"stylelint": "^13.10.0",
"tslib": "^2.3.1",
"typescript": "^4.1.5"
"typescript": "^4.5.1-rc"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
16 changes: 9 additions & 7 deletions packages/create-docusaurus/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env node
#!/usr/bin/env node --experimental-specifier-resolution=node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some day, this flag is may be removed or renamed and completely break everything. I feel like it's not a good idea to hard-code it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is just to make sure things work. Soon after things start working we will try to explicitly specify file names & extensions (especially when TS 4.6 has better support for this)

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const chalk = require('chalk');
const semver = require('semver');
const path = require('path');
const program = require('commander');
const {default: init} = require('../lib');
const requiredVersion = require('../package.json').engines.node;
import chalk from 'chalk';
import semver from 'semver';
import path from 'path';
import program from 'commander';
import init from '../lib';
import packageJSON from '../package.json';

const requiredVersion = packageJSON.engines.node;

if (!semver.satisfies(process.version, requiredVersion)) {
console.log(
Expand Down
3 changes: 2 additions & 1 deletion packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "create-docusaurus",
"version": "2.0.0-beta.9",
"description": "Create Docusaurus apps easily.",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/facebook/docusaurus.git",
Expand All @@ -26,7 +27,7 @@
"chalk": "^4.1.2",
"commander": "^5.1.0",
"fs-extra": "^10.0.0",
"lodash": "^4.17.20",
"lodash-es": "^4.17.20",
"prompts": "^2.4.1",
"semver": "^7.3.4",
"shelljs": "^0.8.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {execSync} from 'child_process';
import prompts, {Choice} from 'prompts';
import path from 'path';
import shell from 'shelljs';
import {kebabCase, sortBy} from 'lodash';
import {kebabCase, sortBy} from 'lodash-es';
import supportsColor from 'supports-color';

const RecommendedTemplate = 'classic';
Expand Down Expand Up @@ -109,7 +109,7 @@ export default async function init(
}> = {},
): Promise<void> {
const useYarn = cliOptions.useNpm ? false : hasYarn();
const templatesDir = path.resolve(__dirname, '../templates');
const templatesDir = new URL('../templates', import.meta.url).pathname;
const templates = readTemplates(templatesDir);
const hasTS = (templateName: string) =>
fs.pathExistsSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "docusaurus-2-classic-typescript-template",
"version": "2.0.0-beta.9",
"private": true,
"type": "module",
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-check
// Note: type annotations allow type checking and IDEs autocompletion

const lightCodeTheme = require('prism-react-renderer/themes/github');
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
import lightCodeTheme from 'prism-react-renderer/themes/github';
import darkCodeTheme from 'prism-react-renderer/themes/dracula';

/** @type {import('@docusaurus/types').Config} */
const config = {
Expand All @@ -22,7 +22,7 @@ const config = {
/** @type {import('@docusaurus/preset-classic').Options} */
({
docs: {
sidebarPath: require.resolve('./sidebars.js'),
sidebarPath: './sidebars.js',
// Please change this to your repo.
editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/',
},
Expand All @@ -33,7 +33,7 @@ const config = {
'https://github.com/facebook/docusaurus/edit/main/website/blog/',
},
theme: {
customCss: require.resolve('./src/css/custom.css'),
customCss: './src/css/custom.css',
},
}),
],
Expand Down Expand Up @@ -115,4 +115,4 @@ const config = {
}),
};

module.exports = config;
export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "docusaurus-2-classic-template",
"version": "2.0.0-beta.9",
"private": true,
"type": "module",
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

const OFF = 0;
const WARNING = 1;
const ERROR = 2;

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const config = {
/** @type {import('@docusaurus/preset-classic').Options} */
({
docs: {
sidebarPath: require.resolve('./sidebars.js'),
sidebarPath: './sidebars.js',
// Please change this to your repo.
editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/',
},
Expand Down Expand Up @@ -149,4 +149,4 @@ const config = {
}),
};

module.exports = config;
export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "docusaurus-2-facebook-template",
"version": "2.0.0-beta.9",
"private": true,
"type": "module",
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Create as many sidebars as you want.
*/

module.exports = {
export default {
// By default, Docusaurus generates a sidebar from the docs folder structure
tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],

Expand Down
2 changes: 1 addition & 1 deletion packages/create-docusaurus/templates/shared/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ const sidebars = {
*/
};

module.exports = sidebars;
export default sidebars;
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@docusaurus/mdx-loader",
"version": "2.0.0-beta.9",
"description": "Docusaurus Loader for MDX",
"type": "module",
"main": "lib/index.js",
"types": "src/types.d.ts",
"publishConfig": {
Expand Down
13 changes: 7 additions & 6 deletions packages/docusaurus-migrate/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/env node
#!/usr/bin/env node --experimental-specifier-resolution=node
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const chalk = require('chalk');
const semver = require('semver');
const cli = require('commander');
const path = require('path');
import chalk from 'chalk';
import semver from 'semver';
import cli from 'commander';
import path from 'path';
import packageJSON from '../package.json';

const requiredVersion = require('../package.json').engines.node;
const requiredVersion = packageJSON.engines.node;

const {migrateDocusaurusProject, migrateMDToMDX} = require('../lib');

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@docusaurus/migrate",
"version": "2.0.0-beta.9",
"description": "A CLI tool to migrate from older versions of Docusaurus.",
"type": "module",
"main": "lib/index.js",
"license": "MIT",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import * as fs from 'fs-extra';
import fs from 'fs-extra';
import importFresh from 'import-fresh';
import chalk from 'chalk';
import glob from 'glob';
Expand Down