Skip to content

Commit

Permalink
Update prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
timstallmann committed Dec 17, 2024
1 parent 0c08c78 commit 7e13bc4
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 204 deletions.
3 changes: 1 addition & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
npx lint-staged
npx lint-staged
yarn test
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"license": "MIT",
"scripts": {
"test": "ts-node --transpile-only test/test.ts",
"prepare": "husky install",
"prepare": "husky init",
"watch": "esbuild ./src/index.js --outdir=build --bundle --platform=node --target=node16 --external:source-map --main-fields=main --sourcemap=linked --watch",
"build-plugin": "rm -rf build && esbuild src/index.ts --outdir=build --bundle --platform=node --target=node16 --external:source-map --main-fields=main --sourcemap=linked && cp src/types.ts build/index.d.ts",
"example": "./bundle.ts --main example/main.ts --worker example/worker.ts --outfile example/bundle.js && open example/index.html",
Expand Down Expand Up @@ -42,10 +42,10 @@
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"esbuild": "^0.21.0",
"eslint": "^8.12.0",
"husky": ">=6",
"eslint": "^9.0.0",
"husky": ">=9",
"lint-staged": ">=10",
"prettier": "^2.6.1",
"prettier": "^3.4.2",
"puppeteer": "^19.5.2"
}
}
8 changes: 5 additions & 3 deletions src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export function assert(x: unknown, message: string): asserts x {
if (!x) {
throw new Error(message.split(' ').length ? message : `Expected ${message} to be truthy`);
}
if (!x) {
throw new Error(
message.split(" ").length ? message : `Expected ${message} to be truthy`,
);
}
}
2 changes: 1 addition & 1 deletion src/escope.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare module "escope" {
ignoreEval?: boolean;
sourceType?: "script" | "module";
ecmaVersion: number;
}
},
): ScopeManager;

class ScopeManager {
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const inlineDedupedWorker: typeof public_types.inlineDedupedWorker =

assert(
!Array.isArray(initialOptions.entryPoints),
"entryPoints should be an object"
"entryPoints should be an object",
);

if (
Expand All @@ -53,14 +53,14 @@ export const inlineDedupedWorker: typeof public_types.inlineDedupedWorker =
Object.keys(initialOptions.entryPoints).length !== 2
) {
throw new Error(
`Expected entryPoints to be an object of the form {main: ..., worker: ...}.`
`Expected entryPoints to be an object of the form {main: ..., worker: ...}.`,
);
}

const mainEntryPoint = initialOptions.entryPoints.main;

const createWorkerPattern = new RegExp(
"^" + escapeRegExp(createWorkerModule) + "$"
"^" + escapeRegExp(createWorkerModule) + "$",
);

let foundCreateWorker = false;
Expand Down Expand Up @@ -95,19 +95,19 @@ export const inlineDedupedWorker: typeof public_types.inlineDedupedWorker =
result.outputFiles.filter((o) => o.path.endsWith(".js")).length ===
3,
`Expected exactly 3 JS output files but found ${result.outputFiles.map(
(f) => f.path
)}`
(f) => f.path,
)}`,
);

const mainBundle = result.outputFiles.find((o) =>
o.path.endsWith("main.js")
o.path.endsWith("main.js"),
);
const workerBundle = result.outputFiles.find((o) =>
o.path.endsWith("worker.js")
o.path.endsWith("worker.js"),
);
const sharedBundle = result.outputFiles.find(
(o) =>
o.path.endsWith(".js") && o.path.indexOf("__shared_chunk") >= 0
o.path.endsWith(".js") && o.path.indexOf("__shared_chunk") >= 0,
);
assert(workerBundle, "workerBundle");
assert(mainBundle, "mainBundle");
Expand All @@ -116,13 +116,13 @@ export const inlineDedupedWorker: typeof public_types.inlineDedupedWorker =
let sourcemaps;
if (initialOptions.sourcemap) {
const main = result.outputFiles.find(
(o) => o.path === mainBundle.path + ".map"
(o) => o.path === mainBundle.path + ".map",
)?.text;
const worker = result.outputFiles.find(
(o) => o.path === workerBundle.path + ".map"
(o) => o.path === workerBundle.path + ".map",
)?.text;
const shared = result.outputFiles.find(
(o) => o.path === sharedBundle.path + ".map"
(o) => o.path === sharedBundle.path + ".map",
)?.text;
assert(main, "main sourcemap");
assert(worker, "worker sourcemap");
Expand All @@ -145,11 +145,11 @@ export const inlineDedupedWorker: typeof public_types.inlineDedupedWorker =
initialOptions.outfile ||
path.resolve(
initialOptions.outdir || process.cwd(),
basename.replace(/(\.ts|\.js)?$/, ".js")
basename.replace(/(\.ts|\.js)?$/, ".js"),
);

const cssFiles = result.outputFiles.filter((f) =>
f.path.endsWith(".css")
f.path.endsWith(".css"),
);

if (splitOutdir) {
Expand Down
10 changes: 5 additions & 5 deletions src/inline-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function inlineWorkerWithEvalStyle(opts: {
const WORKER_MODULE_SRC = variable("worker_module");
const WORKER_SOURCE = variable("worker_source");
const WORKER_SHARED_MODULE_EXPORTS_REFERENCE = variable(
"worker_shared_module_exports"
"worker_shared_module_exports",
);

mainMs.prepend(
Expand Down Expand Up @@ -124,7 +124,7 @@ async function inlineWorkerWithEvalStyle(opts: {
return {createWorker, default: {createWorker}};
})();
`
`,
);

mainMs.prepend(`(() => {`).append(`})()`);
Expand Down Expand Up @@ -229,7 +229,7 @@ async function inlineWorkerWithClosureStyle(opts: {
return {createWorker, default: {createWorker}};
})();
`
`,
);

const bundle = new Bundle();
Expand Down Expand Up @@ -262,12 +262,12 @@ async function inlineWorkerWithClosureStyle(opts: {
addSourceMappingLocations(sourcemaps.worker, workerMs);

const bundleMap = SourceMapGenerator.fromSourceMap(
await new SourceMapConsumer(bundle.generateMap({ includeContent: true }))
await new SourceMapConsumer(bundle.generateMap({ includeContent: true })),
);
for (const chunk in sourcemaps) {
bundleMap.applySourceMap(
sourcemaps[chunk as keyof typeof sourcemaps],
`<inline-worker-dedupe:${chunk}>`
`<inline-worker-dedupe:${chunk}>`,
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/replace-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function replaceExports(
context: Context,
code: MagicString,
namedExportsObjectVariable: string | undefined,
defaultExportVariable: string | undefined
defaultExportVariable: string | undefined,
) {
context.time("repaceExports::acorn");
const node = acorn.parse(code.original, {
Expand Down Expand Up @@ -40,7 +40,7 @@ export function replaceExports(

function compileExport(
e: estree.ExportNamedDeclaration,
target: string | undefined
target: string | undefined,
) {
if (!e.declaration) {
// If we aren't assigning the exports to anything, we don't actually need to write any code,
Expand All @@ -50,7 +50,7 @@ function compileExport(
return e.specifiers
.map(
(spec) =>
`Object.defineProperty(${target}, '${spec.exported.name}', { get: () => ${spec.local.name} });`
`Object.defineProperty(${target}, '${spec.exported.name}', { get: () => ${spec.local.name} });`,
)
.join("\n");
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/replace-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Context } from "./context";
export function replaceImports(
context: Context,
code: MagicString,
getExportsObjectName: (i: estree.ImportDeclaration) => string | false
getExportsObjectName: (i: estree.ImportDeclaration) => string | false,
) {
context.time("replaceImports::acorn");
const ast = acorn.parse(code.original, {
Expand All @@ -30,7 +30,7 @@ export function replaceImports(

assert(ast.type === "Program", `Unexpected top-level node ${ast.type}`);
const imports = ast.body.filter(
(n): n is estree.ImportDeclaration => n.type === "ImportDeclaration"
(n): n is estree.ImportDeclaration => n.type === "ImportDeclaration",
);
const importIdentifiers = new Map<estree.Identifier, string>();
for (const declaration of imports) {
Expand All @@ -46,8 +46,8 @@ export function replaceImports(
spec.type === "ImportNamespaceSpecifier"
? exportsObjectName
: spec.type === "ImportDefaultSpecifier"
? `${exportsObjectName}.default`
: `${exportsObjectName}['${spec.imported.name}']`;
? `${exportsObjectName}.default`
: `${exportsObjectName}['${spec.imported.name}']`;

importIdentifiers.set(spec.local, replacement);
}
Expand All @@ -71,7 +71,7 @@ export function replaceImports(
const replacement = importIdentifiers.get(identifier);
if (replacement) {
const parent = parents.get(
ref.identifier as unknown as acorn.Node
ref.identifier as unknown as acorn.Node,
) as estree.Node;
if (parent?.type === "Property" && parent.shorthand) {
code.appendRight(range[1], `: ${replacement}`);
Expand Down
2 changes: 1 addition & 1 deletion src/source-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RawSourceMap } from "source-map";

export function inlineSourceMapComment(map: string | RawSourceMap) {
const b64 = Buffer.from(
typeof map === "string" ? map : JSON.stringify(map)
typeof map === "string" ? map : JSON.stringify(map),
).toString("base64");
return `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${b64}`;
}
6 changes: 3 additions & 3 deletions test/basic/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module 'create-worker' {
export const createWorker: () => Worker;
}
declare module "create-worker" {
export const createWorker: () => Worker;
}
2 changes: 1 addition & 1 deletion test/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Driver {
constructor(
private options: {
debug?: boolean;
}
},
) {}

async destroy() {
Expand Down
10 changes: 5 additions & 5 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const perStyleTests: {
assert.deepEqual(errors, [], "no errors");
assert.ok(
result.main && result.worker && result.main !== result.worker,
"Worker loaded and worked"
"Worker loaded and worked",
);
},
},
Expand Down Expand Up @@ -95,7 +95,7 @@ const perStyleTests: {
assert.deepEqual(errors, [], "no errors");
assert.ok(
result.main && result.worker && result.main !== result.worker,
"Worker loaded and worked"
"Worker loaded and worked",
);
},
},
Expand Down Expand Up @@ -138,7 +138,7 @@ const perStyleTests: {
assert.deepEqual(errors, [], "no errors");
assert.ok(
result.main && result.worker && result.main !== result.worker,
"Worker loaded and worked"
"Worker loaded and worked",
);
},
},
Expand All @@ -153,12 +153,12 @@ const API = { prop };`;
replaceImports(
new Context({ logLevel: "silent" }),
ms,
() => `__sharedModuleExports`
() => `__sharedModuleExports`,
);

assert.equal(
ms.toString().trim(),
`const API = { prop: __sharedModuleExports['prop'] };`
`const API = { prop: __sharedModuleExports['prop'] };`,
);
},
},
Expand Down
12 changes: 6 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
Expand All @@ -24,7 +24,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand Down Expand Up @@ -69,12 +69,12 @@
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
Expand All @@ -96,6 +96,6 @@

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
Loading

0 comments on commit 7e13bc4

Please sign in to comment.