Skip to content

Commit

Permalink
fix: please webpack module resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jun 20, 2022
1 parent dd77d39 commit 2e18f50
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": []
}
}
18 changes: 14 additions & 4 deletions src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fse from "fs-extra";
import { createCommand } from "../command";

/** The default bob fields that should be within a package.json */
const presetFields = {
export const presetFields = Object.freeze({
main: "dist/index.js",
module: "dist/index.mjs",
typings: "dist/index.d.ts",
Expand All @@ -13,22 +13,32 @@ const presetFields = {
exports: {
".": {
require: {
default: "./dist/index.js",
types: "./dist/index.d.ts",
default: "./dist/index.js",
},
import: {
types: "./dist/index.d.ts",
default: "./dist/index.mjs",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/index.d.ts",
default: "./dist/index.js",
},
},
"./*": {
require: {
default: "./dist/*.js",
types: "./dist/*.d.ts",
default: "./dist/*.js",
},
import: {
types: "./dist/*.d.ts",
default: "./dist/*.mjs",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/*.d.ts",
default: "./dist/*.mjs",
},
},
"./package.json": "./package.json",
Expand All @@ -37,7 +47,7 @@ const presetFields = {
directory: "dist",
access: "public",
},
};
});

export const bootstrapCommand = createCommand<{}, {}>((api) => {
return {
Expand Down
23 changes: 8 additions & 15 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mkdirp from "mkdirp";
import { createCommand } from "../command";
import { BobConfig } from "../config";
import { rewriteExports } from "../utils/rewrite-exports";
import { presetFields } from "./bootstrap";

interface BuildOptions {
external?: string[];
Expand Down Expand Up @@ -406,17 +407,7 @@ function rewritePackageJson(pkg: Record<string, any>, distPath: string) {
if (pkg.exports) {
newPkg.exports = rewriteExports(pkg.exports, DIST_DIR);
} else {
newPkg.exports = {
".": {
require: "./index.js",
import: "./index.mjs",
},
"./*": {
require: "./*.js",
import: "./*.mjs",
},
"./package.json": "./package.json",
};
newPkg.exports = presetFields.exports;
}

if (pkg.bin) {
Expand Down Expand Up @@ -449,21 +440,23 @@ export function validatePackageJson(pkg: any) {
expect("typescript.definition", `${DIST_DIR}/index.d.ts`);

expect("exports['.'].require", {
default: `./${DIST_DIR}/index.js`,
types: `./${DIST_DIR}/index.d.ts`,
default: `./${DIST_DIR}/index.js`,
});
expect("exports['.'].import", {
default: `./${DIST_DIR}/index.mjs`,
types: `./${DIST_DIR}/index.d.ts`,
default: `./${DIST_DIR}/index.mjs`,
});
expect("exports['.'].default", `./${DIST_DIR}/index.mjs`);
expect("exports['./*'].require", {
default: `./${DIST_DIR}/*.js`,
types: `./${DIST_DIR}/*.d.ts`,
default: `./${DIST_DIR}/*.js`,
});
expect("exports['./*'].import", {
default: `./${DIST_DIR}/*.mjs`,
types: `./${DIST_DIR}/*.d.ts`,
default: `./${DIST_DIR}/*.mjs`,
});
expect("exports['./*'].default", `./${DIST_DIR}/*.mjs`);
}

async function copyToDist(cwd: string, files: string[], distDir: string) {
Expand Down
50 changes: 37 additions & 13 deletions src/utils/rewrite-exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function rewriteExports(
exports: Record<string, string | { require?: string | { [key: string]: string }; import?: string | { [key: string]: string } }>,
exports: Record<
string,
| string
| {
require?: string | { [key: string]: string };
import?: string | { [key: string]: string };
default?: string | { [key: string]: string };
}
>,
distDir: string
) {
const newExports = { ...exports };
Expand All @@ -8,52 +16,68 @@ export function rewriteExports(

newExports["."] = {
require: {
types: "./index.d.ts",
default: "./index.js",
types: "./index.d.ts"
},
import: {
types: "./index.d.ts",
default: "./index.mjs",
},
default: {
types: "./index.d.ts",
default: "./index.mjs",
},
};

newExports["./*"] = {
require: {
require: {
types: "./*.d.ts",
default: "./*.js",
types: "./*.d.ts"
},
import: {
types: "./*.d.ts",
default: "./*.mjs",
},
default: {
types: "./*.d.ts",
default: "./*.mjs",
types: "./*.d.ts"
},
};

for (const [key, value] of Object.entries(newExports)) {
if (!value) continue;

let newValue = value as string | { require?: string | { [key: string]: string }; import?: string | { [key: string]: string } };
let newValue = value as
| string
| {
require?: string | { [key: string]: string };
import?: string | { [key: string]: string };
default?: string | { [key: string]: string };
};

if (typeof newValue === "string") {
newValue = newValue.replace(`${distDir}/`, "");
} else if (typeof newValue === "object" && newValue != null) {

function transformValue(value: string | { [key: string]: string } | undefined) {
function transformValue(
value: string | { [key: string]: string } | undefined
) {
if (value == null) {
return undefined
return undefined;
}
if (typeof value === "object") {
const newValue: Record<string, string> = {}
const newValue: Record<string, string> = {};
for (const [key, path] of Object.entries(value)) {
newValue[key] = path.replace(`${distDir}/`, "")
newValue[key] = path.replace(`${distDir}/`, "");
}
return newValue
return newValue;
}
return value.replace(`${distDir}/`, "")
return value.replace(`${distDir}/`, "");
}

newValue = {
require: transformValue(newValue.require),
import: transformValue(newValue.import),
default: transformValue(newValue.import),
};
}
newExports[key.replace(`${distDir}/`, "")] = newValue;
Expand Down
22 changes: 12 additions & 10 deletions test/__fixtures__/simple-monorepo/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
"exports": {
".": {
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"default": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"default": "./dist/index.mjs"
},
"./*": {
"require": {
"default": "./dist/*.js",
"types": "./dist/*.d.ts"
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
},
"import": {
"default": "./dist/*.mjs",
"types": "./dist/*.d.ts"
}
"types": "./dist/*.d.ts",
"default": "./dist/*.mjs"
},
"default": "./dist/*.mjs"
},
"./package.json": "./package.json"
},
Expand Down
22 changes: 12 additions & 10 deletions test/__fixtures__/simple-monorepo/packages/b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
"exports": {
".": {
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"default": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"default": "./dist/index.mjs"
},
"./*": {
"require": {
"default": "./dist/*.js",
"types": "./dist/*.d.ts"
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
},
"import": {
"default": "./dist/*.mjs",
"types": "./dist/*.d.ts"
}
"types": "./dist/*.d.ts",
"default": "./dist/*.mjs"
},
"default": "./dist/*.mjs"
},
"./package.json": "./package.json"
},
Expand Down
24 changes: 13 additions & 11 deletions test/__fixtures__/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@
"exports": {
".": {
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"default": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"default": "./dist/index.mjs"
},
"./*": {
"require": {
"default": "./dist/*.js",
"types": "./dist/*.d.ts"
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
},
"import": {
"default": "./dist/*.mjs",
"types": "./dist/*.d.ts"
}
"types": "./dist/*.d.ts",
"default": "./dist/*.mjs"
},
"default": "./dist/*.mjs"
},
"./package.json": "./package.json"
},
"publishConfig": {
"directory": "dist",
"access": "public"
}
}
}
Loading

0 comments on commit 2e18f50

Please sign in to comment.