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

Add "type": "module" to @apollo/client/core and friends #8396

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function eachFile(dir: string, callback: (

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".cjs.js")) return;
if (relPath.endsWith(".cjs")) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".min.js")) return;
Expand Down
3 changes: 2 additions & 1 deletion config/prepareDist.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ entryPoints.forEach(function buildPackageJson({
path.join(distRoot, ...dirs, 'package.json'),
JSON.stringify({
name: path.posix.join('@apollo', 'client', ...dirs),
main: `${bundleName}.cjs.js`,
type: "module",
main: `${bundleName}.cjs`,
module: 'index.js',
types: 'index.d.ts',
sideEffects,
Expand Down
30 changes: 27 additions & 3 deletions config/resolveModuleIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,44 @@ function isRelative(id: string) {
}

function normalizeSourceString(file: string, source?: Node | null) {
if (source && n.StringLiteral.check(source) && isRelative(source.value)) {
if (source && n.StringLiteral.check(source)) {
try {
source.value = normalizeId(source.value, file);
source.value = isRelative(source.value)
? normalizeId(source.value, file)
: normalizeNonRelativeId(source.value, file);
} catch (error) {
console.error(`Failed to resolve ${source.value} in ${file}`);
console.error(`Failed to resolve ${source.value} in ${file} with error ${error}`);
process.exit(1);
}
}
}

function normalizeNonRelativeId(id: string, file: string) {
const normal = normalizeId(id, file);
const normalParts = normal.split("/");
const sourceParts = id.split("/");
const nodeModulesIndex = normalParts.lastIndexOf("node_modules");
if (
nodeModulesIndex >= 0 &&
normalParts[nodeModulesIndex + 1] === sourceParts[0]
) {
const bareModuleIdentifier =
normalParts.slice(nodeModulesIndex + 1).join("/");
if (normal === normalizeId(bareModuleIdentifier, file)) {
return bareModuleIdentifier;
}
console.error(`Leaving ${id} import in ${file} unchanged because ${
bareModuleIdentifier
} does not resolve to the same module`);
}
return id;
}

function normalizeId(id: string, file: string) {
const basedir = path.dirname(file);
const absPath = resolve.sync(id, {
basedir,
extensions: [".mjs", ".js"],
packageFilter(pkg) {
return pkg.module ? {
...pkg,
Expand Down
8 changes: 4 additions & 4 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function prepareCJSMinified(input) {
return {
input,
output: {
file: input.replace('.js', '.min.js'),
file: input.replace('.cjs', '.min.cjs'),
format: 'cjs',
},
plugins: [
Expand Down Expand Up @@ -108,7 +108,7 @@ function prepareBundle({
return isExternal(id, parentId, true);
},
output: {
file: `${dir}/${bundleName}.cjs.js`,
file: `${dir}/${bundleName}.cjs`,
format: 'cjs',
sourcemap: true,
exports: 'named',
Expand All @@ -125,10 +125,10 @@ export default [
// Convert the ESM entry point to a single CJS bundle.
prepareCJS(
'./dist/core/index.js',
'./dist/apollo-client.cjs.js',
'./dist/apollo-client.cjs',
),
// Minify that single CJS bundle.
prepareCJSMinified(
'./dist/apollo-client.cjs.js',
'./dist/apollo-client.cjs',
),
];
16 changes: 8 additions & 8 deletions config/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ switch (process.argv[2]) {
const {
ApolloClient,
InMemoryCache,
} = require(path.join(distRoot, "core", "core.cjs.js"));
} = require(path.join(distRoot, "core", "core.cjs"));

// Though this may seem like overkill, verifying that ApolloClient is
// constructible in Node.js is actually pretty useful, too!
Expand All @@ -43,15 +43,15 @@ switch (process.argv[2]) {
// the client might have acquired during its construction.
client.stop();

// The CommonJS dist/core/core.cjs.js file is generated from ESM modules
// generated by tsc, including dist/version.js, so verifying core.cjs.js
// exports an ApolloClient class that defines client.version also serves
// to verify that dist/version.js must have been correctly updated,
// which is convenient because dist/version.js uses ECMAScript module
// syntax, and is thus not importable in all versions of Node.js.
// The CommonJS dist/core/core.cjs file is generated from ESM modules
// generated by tsc, including dist/version.js, so verifying core.cjs
// exports an ApolloClient class that defines client.version also serves to
// verify that dist/version.js must have been correctly updated, which is
// convenient because dist/version.js uses ECMAScript module syntax, and is
// thus not importable in all versions of Node.js.
assert.strictEqual(
client.version, version,
"Failed to update dist/version.js and dist/core/core.cjs.js",
"Failed to update dist/version.js and dist/core/core.cjs",
);

break;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"author": "opensource@apollographql.com",
"license": "MIT",
"main": "./dist/main.cjs.js",
"main": "./dist/main.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"sideEffects": false,
Expand Down Expand Up @@ -54,7 +54,7 @@
"bundlesize": [
{
"name": "apollo-client",
"path": "./dist/apollo-client.cjs.min.js",
"path": "./dist/apollo-client.min.cjs",
"maxSize": "24.6 kB"
}
],
Expand Down