diff --git a/config/helpers.ts b/config/helpers.ts index 2c881a98b0e..2fcc9919b9d 100644 --- a/config/helpers.ts +++ b/config/helpers.ts @@ -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; diff --git a/config/prepareDist.js b/config/prepareDist.js index 0a06adf0812..5d35465337e 100644 --- a/config/prepareDist.js +++ b/config/prepareDist.js @@ -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, diff --git a/config/resolveModuleIds.ts b/config/resolveModuleIds.ts index 061fd7a4826..37dec13b646 100644 --- a/config/resolveModuleIds.ts +++ b/config/resolveModuleIds.ts @@ -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, diff --git a/config/rollup.config.js b/config/rollup.config.js index 90d381f7a5c..4e5892b6bae 100644 --- a/config/rollup.config.js +++ b/config/rollup.config.js @@ -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: [ @@ -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', @@ -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', ), ]; diff --git a/config/version.js b/config/version.js index f48278fc6e8..0abf54fe5c9 100644 --- a/config/version.js +++ b/config/version.js @@ -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! @@ -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; diff --git a/package.json b/package.json index 5cbefd39b1a..9fa926c0f56 100644 --- a/package.json +++ b/package.json @@ -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, @@ -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" } ],