Skip to content

Commit

Permalink
Add a script to normalize module identifiers.
Browse files Browse the repository at this point in the history
Follow-up to #6657.
Fixes #6687.
  • Loading branch information
benjamn committed Jul 24, 2020
1 parent 752dd5c commit 1aa4768
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 32 deletions.
81 changes: 81 additions & 0 deletions config/resolveModuleIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as fs from "fs";
import * as path from "path";
import resolve from "resolve";
import { distDir, eachFile, reparse, reprint } from './helpers';

eachFile(distDir, (file, relPath) => new Promise((resolve, reject) => {
fs.readFile(file, "utf8", (error, source) => {
if (error) return reject(error);
const output = transform(source, file);
if (source === output) {
resolve(file);
} else {
console.log("Resolved module identifiers in " + relPath);
fs.writeFile(file, output, "utf8", error => {
error ? reject(error) : resolve(file);
});
}
});
}));

import * as recast from "recast";
const n = recast.types.namedTypes;
type Node = recast.types.namedTypes.Node;

function transform(code: string, file: string) {
const ast = reparse(code);

recast.visit(ast, {
visitImportDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitImportExpression(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitExportAllDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},

visitExportNamedDeclaration(path) {
this.traverse(path);
normalizeSourceString(file, path.node.source);
},
});

return reprint(ast);
}

function isRelative(id: string) {
return id.startsWith("./") || id.startsWith("../");
}

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

function normalizeId(id: string, file: string) {
const basedir = path.dirname(file);
const absPath = resolve.sync(id, {
basedir,
packageFilter(pkg) {
if (pkg.module) {
return { ...pkg, main: pkg.module };
}
},
});
const relPath = path.relative(basedir, absPath);
const relId = relPath.split(path.sep).join('/');
return isRelative(relId) ? relId : "./" + relId;
}
25 changes: 0 additions & 25 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,6 @@ function prepareBundle({
};
}

// Resolve indirect imports and exports to the original exporting module,
// so that more imports are explicitly named (fewer *s), and all source
// module identifiers have file extensions.
function resolveESMImportsAndFileExtensions(input, outputDir) {
return {
input,
external(id) {
return externalPackages.has(id);
},
output: {
dir: outputDir,
format: 'esm',
sourcemap: true,
},
// By setting preserveModules to true, we're making sure Rollup
// doesn't attempt to create a single combined ESM bundle with the
// final result of running this job.
preserveModules: true,
plugins: [
nodeResolve(),
],
};
}

export default [
...entryPoints.map(prepareBundle),
// Convert the ESM entry point to a single CJS bundle.
Expand All @@ -128,5 +104,4 @@ export default [
prepareCJSMinified(
'./dist/apollo-client.cjs.js',
),
resolveESMImportsAndFileExtensions(packageJson.module, distDir),
];
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@
"homepage": "https://www.apollographql.com",
"scripts": {
"prebuild": "npm run clean",
"build": "tsc && ts-node-script config/processInvariants.ts",
"postbuild": "npm run bundle && npm run prepdist",
"build": "tsc",
"postbuild": "npm run invariants && npm run rollup && npm run prepdist && npm run resolve",
"invariants": "ts-node-script config/processInvariants.ts",
"rollup": "rollup -c ./config/rollup.config.js",
"prepdist": "node ./config/prepareDist.js",
"resolve": "ts-node-script config/resolveModuleIds.ts",
"watch": "tsc-watch --onSuccess \"npm run postbuild\"",
"clean": "rimraf -r dist coverage lib",
"test": "jest --config ./config/jest.config.js",
"test:debug": "BABEL_ENV=server node --inspect-brk node_modules/.bin/jest --config ./config/jest.config.js --runInBand",
"test:ci": "npm run coverage -- --ci --maxWorkers=2 --reporters=default --reporters=jest-junit",
"test:watch": "jest --config ./config/jest.config.js --watch",
"bundle": "rollup -c ./config/rollup.config.js",
"coverage": "jest --config ./config/jest.config.js --verbose --coverage",
"bundlesize": "npm run build && bundlesize",
"prepdist": "node ./config/prepareDist.js",
"predeploy": "npm run build",
"deploy": "cd dist && npm publish --tag beta"
},
Expand Down Expand Up @@ -105,6 +107,7 @@
"react-dom": "^16.13.1",
"recast": "0.19.1",
"recompose": "^0.30.0",
"resolve": "^1.17.0",
"rimraf": "3.0.2",
"rollup": "1.31.1",
"rollup-plugin-terser": "5.1.3",
Expand Down

0 comments on commit 1aa4768

Please sign in to comment.