Skip to content

Commit 5198442

Browse files
authored
Support Node ESM (#3630)
* Support Node ESM
1 parent 09c71dd commit 5198442

File tree

187 files changed

+6923
-4134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+6923
-4134
lines changed

.circleci/api-comment.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ async function run() {
5353

5454
if (pr != null) {
5555
let commentId = await findDifferComment(pr);
56-
let diffs = fs.readFileSync('/tmp/dist/ts-diff.txt');
56+
let diffs;
57+
try {
58+
diffs = fs.readFileSync('/tmp/dist/ts-diff.txt');
59+
} catch (e) {
60+
console.log('No TS Diff output to run on.')
61+
return;
62+
}
5763
if (diffs.length > 0) {
5864
if (commentId != null) {
5965
// delete existing comment

.circleci/config.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ executors:
2727
CACHE_VERSION: v1
2828
working_directory: ~/react-spectrum
2929

30+
rsp-xlarge-nodeupdate:
31+
docker:
32+
- image: cimg/node:16.18.0
33+
resource_class: xlarge
34+
environment:
35+
CACHE_VERSION: v1
36+
working_directory: ~/react-spectrum
37+
3038
jobs:
3139
install:
3240
executor: rsp-large
@@ -206,6 +214,20 @@ jobs:
206214
- store_artifacts:
207215
path: ~/junit
208216

217+
218+
test-esm:
219+
executor: rsp-xlarge-nodeupdate
220+
steps:
221+
- restore_cache:
222+
key: react-spectrum-{{ .Environment.CACHE_VERSION }}-{{ .Environment.CIRCLE_SHA1 }}
223+
224+
- run:
225+
name: test
226+
command: |
227+
make build
228+
yarn lerna run prepublishOnly
229+
node --loader ./scripts/esm-support/loader.mjs ./scripts/esm-support/testESM.mjs
230+
209231
lint:
210232
executor: rsp
211233
steps:
@@ -450,6 +472,9 @@ workflows:
450472
- test-17:
451473
requires:
452474
- install-17
475+
- test-esm:
476+
requires:
477+
- install
453478
- lint:
454479
requires:
455480
- install
@@ -505,6 +530,7 @@ workflows:
505530
- test-16
506531
- test-ssr-17
507532
- test-17
533+
- test-esm
508534
- storybook
509535
- storybook-16
510536
- storybook-17

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ node_modules
77
packages/*/*/dist
88
packages/react-aria/dist
99
packages/react-stately/dist
10-
packages/dev/storybook-builder-preview/preview.js
10+
packages/dev/storybook-builder-parcel/preview.js

babel-esm.config.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"presets": [
3+
"@babel/preset-typescript",
4+
"@babel/preset-react",
5+
["@babel/preset-env",
6+
{
7+
"loose": true,
8+
"modules": false
9+
}
10+
]
11+
],
12+
"env": {
13+
"storybook": {
14+
"presets": [
15+
[
16+
"@babel/preset-env",
17+
{
18+
"loose": true,
19+
"targets": {
20+
"esmodules": true
21+
}
22+
}
23+
]
24+
]
25+
},
26+
"cover": {
27+
"plugins": [
28+
"istanbul"
29+
]
30+
},
31+
"production": {
32+
"plugins": [
33+
[
34+
"react-remove-properties",
35+
{
36+
"properties": [
37+
"data-testid"
38+
]
39+
}
40+
]
41+
]
42+
}
43+
},
44+
"plugins": [
45+
[
46+
"@babel/plugin-transform-runtime",
47+
{
48+
"version": "^7.6.2"
49+
}
50+
],
51+
[
52+
"@babel/plugin-proposal-decorators",
53+
{
54+
"legacy": true
55+
}
56+
],
57+
"transform-glob-import",
58+
"babel-plugin-macros"
59+
],
60+
"sourceType": "unambiguous"
61+
}

bin/imports.js

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,78 @@ const fs = require('fs');
1616
const Module = require('module');
1717
const substrings = ['-', '+'];
1818

19-
module.exports = function (context) {
20-
let processNode = (node) => {
21-
if (!node.source || node.importKind === 'type') {
22-
return;
23-
}
24-
25-
let source = node.source.value.replace(/^[a-z]+:/, '');
26-
if (source.startsWith('.') || Module.builtinModules.includes(source)) {
27-
return;
28-
}
29-
30-
// Split the import specifier on slashes. If it starts with an @ then it's
31-
// a scoped package, otherwise just take the first part.
32-
let parts = source.split('/');
33-
let pkgName = source.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
34-
35-
// Search for a package.json starting from the current filename
36-
let pkgPath = findUp.sync('package.json', {cwd: path.dirname(context.getFilename())});
37-
if (!pkgPath) {
38-
return;
39-
}
40-
41-
let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
42-
43-
// The only dev dependency should be spectrum-css.
44-
if (exists(pkg.devDependencies, pkgName) && pkgName === '@adobe/spectrum-css-temp') {
45-
return;
46-
}
47-
48-
if (!exists(pkg.dependencies, pkgName) && !exists(pkg.peerDependencies, pkgName)) {
49-
context.report({
50-
node,
51-
message: `Missing dependency on ${pkgName}.`,
52-
fix(fixer) {
53-
// Attempt to find a package in the monorepo. If the dep is for an external library,
54-
// then we cannot auto fix it because we don't know the version to add.
55-
let depPath = __dirname + '/../packages/' + pkgName + '/package.json';
56-
if (!fs.existsSync(depPath)) {
57-
return;
19+
module.exports = {
20+
meta: {
21+
fixable: 'code'
22+
},
23+
create: function (context) {
24+
let processNode = (node) => {
25+
if (!node.source || node.importKind === 'type') {
26+
return;
27+
}
28+
29+
let source = node.source.value.replace(/^[a-z]+:/, '');
30+
if (source.startsWith('.') || Module.builtinModules.includes(source)) {
31+
return;
32+
}
33+
34+
// Split the import specifier on slashes. If it starts with an @ then it's
35+
// a scoped package, otherwise just take the first part.
36+
let parts = source.split('/');
37+
let pkgName = source.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
38+
39+
// Search for a package.json starting from the current filename
40+
let pkgPath = findUp.sync('package.json', {cwd: path.dirname(context.getFilename())});
41+
if (!pkgPath) {
42+
return;
43+
}
44+
45+
let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
46+
47+
// The only dev dependency should be spectrum-css.
48+
if (exists(pkg.devDependencies, pkgName) && pkgName === '@adobe/spectrum-css-temp') {
49+
return;
50+
}
51+
52+
if (!exists(pkg.dependencies, pkgName) && !exists(pkg.peerDependencies, pkgName) && pkgName !== pkg.name) {
53+
context.report({
54+
node,
55+
message: `Missing dependency on ${pkgName}.`,
56+
fix(fixer) {
57+
// Attempt to find a package in the monorepo. If the dep is for an external library,
58+
// then we cannot auto fix it because we don't know the version to add.
59+
let depPath = __dirname + '/../packages/' + pkgName + '/package.json';
60+
if (!fs.existsSync(depPath)) {
61+
return;
62+
}
63+
64+
let depPkg = JSON.parse(fs.readFileSync(depPath, 'utf8'));
65+
let pkgVersion = substrings.some(v => depPkg.version.includes(v)) ? depPkg.version : `^${depPkg.version}`;
66+
67+
if (pkgName === '@react-spectrum/provider') {
68+
pkg.peerDependencies = insertObject(pkg.peerDependencies, pkgName, pkgVersion);
69+
} else {
70+
pkg.dependencies = insertObject(pkg.dependencies, pkgName, pkgVersion);
71+
}
72+
73+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, false, 2) + '\n');
74+
75+
// Fake fix so eslint doesn't show the error.
76+
return {
77+
range: [0, 0],
78+
text: ''
79+
};
5880
}
59-
60-
let depPkg = JSON.parse(fs.readFileSync(depPath, 'utf8'));
61-
let pkgVersion = substrings.some(v => depPkg.version.includes(v)) ? depPkg.version : `^${depPkg.version}`;
62-
63-
if (pkgName === '@react-spectrum/provider') {
64-
pkg.peerDependencies = insertObject(pkg.peerDependencies, pkgName, pkgVersion);
65-
} else {
66-
pkg.dependencies = insertObject(pkg.dependencies, pkgName, pkgVersion);
67-
}
68-
69-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, false, 2) + '\n');
70-
71-
// Fake fix so eslint doesn't show the error.
72-
return {
73-
range: [0, 0],
74-
text: ''
75-
};
76-
}
77-
});
78-
}
79-
};
80-
81-
return {
82-
ImportDeclaration: processNode,
83-
ExportNamedDeclaration: processNode,
84-
ExportAllDeclaration: processNode
85-
};
81+
});
82+
}
83+
};
84+
85+
return {
86+
ImportDeclaration: processNode,
87+
ExportNamedDeclaration: processNode,
88+
ExportAllDeclaration: processNode
89+
};
90+
}
8691
};
8792

8893
function exists(deps, name) {

examples/rsp-next-ts/next.config.js renamed to examples/rsp-next-ts/next.config.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const withTM = require("next-transpile-modules")([
1+
import ntm from 'next-transpile-modules';
2+
const withTM = ntm([
23
"@adobe/react-spectrum",
34
"@react-spectrum/actiongroup",
45
"@react-spectrum/badge",
@@ -51,9 +52,10 @@ const withTM = require("next-transpile-modules")([
5152
"@spectrum-icons/workflow",
5253
]);
5354

54-
module.exports = withTM({
55+
let result = withTM({
5556
basePath:
5657
process.env.VERDACCIO && process.env.CIRCLE_SHA1
5758
? `/reactspectrum/${process.env.CIRCLE_SHA1}/verdaccio/next`
5859
: "",
5960
});
61+
export default result;

examples/rsp-next-ts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"start": "next start",
1111
"lint": "next lint"
1212
},
13+
"type": "module",
1314
"dependencies": {
1415
"@adobe/react-spectrum": "^3.22.0",
1516
"@react-spectrum/color": "^3.0.0-beta.16",

0 commit comments

Comments
 (0)