Skip to content

Commit f5c005b

Browse files
authored
Add support for Node ESM (#4038)
1 parent 72ee92f commit f5c005b

File tree

197 files changed

+1568
-96
lines changed

Some content is hidden

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

197 files changed

+1568
-96
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:
@@ -451,6 +473,9 @@ workflows:
451473
- test-17:
452474
requires:
453475
- install-17
476+
- test-esm:
477+
requires:
478+
- install
454479
- lint:
455480
requires:
456481
- install
@@ -506,6 +531,7 @@ workflows:
506531
- test-16
507532
- test-ssr-17
508533
- test-17
534+
- test-esm
509535
- storybook
510536
- storybook-16
511537
- storybook-17

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ publish-nightly: build
8383

8484
build:
8585
parcel build packages/@react-{spectrum,aria,stately}/*/ packages/@internationalized/{message,string,date,number}/ --no-optimize
86+
yarn lerna run prepublishOnly
87+
for pkg in packages/@react-{spectrum,aria,stately}/*/ packages/@internationalized/{message,string,date,number}/ packages/@adobe/react-spectrum/ packages/react-aria/ packages/react-stately/; \
88+
do cp $$pkg/dist/module.js $$pkg/dist/import.mjs; \
89+
done
90+
sed -i.bak s/\.js/\.mjs/ packages/@react-aria/i18n/dist/import.mjs
91+
rm packages/@react-aria/i18n/dist/import.mjs.bak
8692

8793
website:
8894
yarn build:docs --public-url /reactspectrum/$$(git rev-parse HEAD)/docs --dist-dir dist/$$(git rev-parse HEAD)/docs

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
transpilePackages: [
33
"@adobe/react-spectrum",
44
"@react-spectrum/actiongroup",

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",

examples/rsp-webpack-4/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/env", "@babel/preset-react"]
3+
}

examples/rsp-webpack-4/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
moduleNameMapper: {
3+
'\\.(css|styl)$': 'identity-obj-proxy'
4+
}
5+
};

examples/rsp-webpack-4/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "rsp-cra-18-webpack-4",
3+
"version": "1.0.0",
4+
"description": "test esm with webpack 4",
5+
"main": "src/index.jsx",
6+
"scripts": {
7+
"build": "webpack --mode production",
8+
"start": "webpack-dev-server --mode development --open",
9+
"test": "test"
10+
},
11+
"private": true,
12+
"workspaces": [
13+
"../../packages/*/*"
14+
],
15+
"dependencies": {
16+
"@adobe/react-spectrum": "^3.24.1",
17+
"@spectrum-icons/workflow": "^4.0.6",
18+
"react": "^18.2.0",
19+
"react-dom": "^18.2.0"
20+
},
21+
"devDependencies": {
22+
"@babel/core": "^7.1.0",
23+
"@babel/cli": "^7.1.0",
24+
"@babel/preset-env": "^7.1.0",
25+
"@babel/preset-react": "^7.0.0",
26+
"webpack": "4.19.1",
27+
"webpack-cli": "3.1.1",
28+
"webpack-dev-server": "3.1.8",
29+
"style-loader": "0.23.0",
30+
"css-loader": "1.0.0",
31+
"babel-loader": "8.0.2",
32+
"jest": "^26"
33+
}
34+
}

examples/rsp-webpack-4/src/App.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body{
2+
height: 100%;
3+
}
4+
5+
.no-bullets{
6+
list-style-type: none;
7+
padding: 0px;
8+
}
9+
10+
#root{
11+
padding: 0;
12+
margin: 0;
13+
height: 100%;
14+
}
15+
16+
html {
17+
height: 100%;
18+
}
19+
20+
.content-padding{
21+
padding: 50px;
22+
}

examples/rsp-webpack-4/src/App.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import './App.css';
2+
import {Provider, defaultTheme} from '@adobe/react-spectrum'
3+
import Lighting from './Lighting';
4+
import {useState} from 'react'
5+
import BodyContent from './BodyContent';
6+
7+
function App() {
8+
let [selected, setSelection] = useState(false);
9+
10+
return (
11+
<Provider theme={defaultTheme}
12+
colorScheme={selected ? "light" : "dark"}
13+
height="100%">
14+
<div className="content-padding">
15+
<Lighting selected={selected} switch={setSelection} />
16+
<BodyContent />
17+
</div>
18+
</Provider>
19+
);
20+
}
21+
22+
export default App;

0 commit comments

Comments
 (0)