Skip to content

Commit f0db6bf

Browse files
authored
Convert scripts package to type: "module" (#1579)
1 parent 688110a commit f0db6bf

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

scripts/build-dts.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'fs';
22
import fs from 'fs/promises';
33
import path from 'path';
4+
import { pathToFileURL } from 'url';
45

56
import glob from 'fast-glob';
67
import { legacy, resolve } from 'resolve.exports';
@@ -90,41 +91,45 @@ async function removePreconstructDeclarations(
9091
});
9192
}
9293

93-
(async () => {
94-
const packages = await glob('packages/*', {
95-
onlyDirectories: true,
96-
absolute: true,
97-
});
94+
const packages = await glob('packages/*', {
95+
onlyDirectories: true,
96+
absolute: true,
97+
});
9898

99-
const entryPaths: [string, string][] = [];
99+
const entryPaths: [string, string][] = [];
100100

101-
for (const packageDir of packages) {
102-
const pkg = require(path.resolve(packageDir, 'package.json'));
101+
for (const packageDir of packages) {
102+
const pkg = await import(
103+
// oathToFileURL enables the result of `path.resolve` to work with `import()` on windows
104+
pathToFileURL(path.resolve(packageDir, 'package.json')).toString(),
105+
{
106+
with: { type: 'json' },
107+
}
108+
);
103109

104-
if (pkg.exports) {
105-
const pkgExports = Object.keys(pkg.exports);
110+
if (pkg.exports) {
111+
const pkgExports = Object.keys(pkg.exports);
106112

107-
for (const entryName of pkgExports) {
108-
if (entryName.endsWith('package.json')) continue;
113+
for (const entryName of pkgExports) {
114+
if (entryName.endsWith('package.json')) continue;
109115

110-
entryPaths.push([packageDir, resolveEntry(pkg, entryName)]);
111-
}
112-
} else {
113-
entryPaths.push([packageDir, resolveEntry(pkg)]);
116+
entryPaths.push([packageDir, resolveEntry(pkg, entryName)]);
114117
}
118+
} else {
119+
entryPaths.push([packageDir, resolveEntry(pkg)]);
115120
}
121+
}
116122

117-
await Promise.all(
118-
entryPaths.map(([packageDir, entryPath]) =>
119-
buildEntry(packageDir, entryPath),
120-
),
121-
).then((writes) => writes.map((write) => write?.()));
122-
123-
// Entry points might reference each other so remove old declaration files
124-
// after we're done with everything
125-
await Promise.all(
126-
entryPaths.map(([packageDir, entryPath]) =>
127-
removePreconstructDeclarations(packageDir, entryPath),
128-
),
129-
);
130-
})();
123+
await Promise.all(
124+
entryPaths.map(([packageDir, entryPath]) =>
125+
buildEntry(packageDir, entryPath),
126+
),
127+
).then((writes) => writes.map((write) => write?.()));
128+
129+
// Entry points might reference each other so remove old declaration files
130+
// after we're done with everything
131+
await Promise.all(
132+
entryPaths.map(([packageDir, entryPath]) =>
133+
removePreconstructDeclarations(packageDir, entryPath),
134+
),
135+
);

scripts/copy-next-plugin.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
import { glob } from 'fast-glob';
1+
import glob from 'fast-glob';
22
import { existsSync } from 'fs';
33
import fs from 'fs/promises';
44
import path from 'path';
55

66
// We need to use distinct next plugins for each next fixutre
77
// due to different next versions / mini-css-extract-plugin serializer registration
8-
(async () => {
9-
const nextPluginDistDir = path.join(
10-
__dirname,
11-
'../packages/next-plugin/dist',
12-
);
8+
const nextPluginDistDir = path.join(
9+
import.meta.dirname,
10+
'../packages/next-plugin/dist',
11+
);
1312

14-
if (!existsSync(nextPluginDistDir)) {
15-
throw new Error('packages/next-plugin/dist is missing.');
16-
}
13+
if (!existsSync(nextPluginDistDir)) {
14+
throw new Error('packages/next-plugin/dist is missing.');
15+
}
1716

18-
const nextFixtureDirs = await glob('fixtures/next-*', {
19-
onlyDirectories: true,
20-
absolute: true,
21-
});
17+
const nextFixtureDirs = await glob('fixtures/next-*', {
18+
onlyDirectories: true,
19+
absolute: true,
20+
});
2221

23-
if (nextFixtureDirs.length === 0) {
24-
throw new Error('No next fixtures found.');
25-
}
22+
if (nextFixtureDirs.length === 0) {
23+
throw new Error('No next fixtures found.');
24+
}
2625

27-
for (const dir of nextFixtureDirs) {
28-
await fs.cp(nextPluginDistDir, path.join(dir, 'next-plugin', 'dist'), {
29-
recursive: true,
30-
});
31-
}
32-
})();
26+
for (const dir of nextFixtureDirs) {
27+
await fs.cp(nextPluginDistDir, path.join(dir, 'next-plugin', 'dist'), {
28+
recursive: true,
29+
});
30+
}

scripts/copy-readme-to-packages.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import path from 'path';
33

44
import glob from 'fast-glob';
55

6-
(async () => {
7-
const packages = await glob('packages/*', {
8-
onlyDirectories: true,
9-
absolute: true,
10-
ignore: ['packages/sprinkles', 'packages/integration'],
11-
});
6+
const packages = await glob('packages/*', {
7+
onlyDirectories: true,
8+
absolute: true,
9+
ignore: ['packages/sprinkles', 'packages/integration', 'packages/compiler'],
10+
});
1211

13-
for (const packageDir of packages) {
14-
await fs.copyFile(
15-
path.join(__dirname, '../README.md'),
16-
path.join(packageDir, 'README.md'),
17-
);
18-
}
19-
})();
12+
for (const packageDir of packages) {
13+
await fs.copyFile(
14+
path.join(import.meta.dirname, '../README.md'),
15+
path.join(packageDir, 'README.md'),
16+
);
17+
}

scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@vanilla-extract/scripts",
33
"private": true,
4+
"type": "module",
45
"devDependencies": {
56
"fast-glob": "^3.2.7",
67
"resolve.exports": "^2.0.2",

0 commit comments

Comments
 (0)