Skip to content

Commit

Permalink
refactor: migrate packages to ESM (fwouts#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts authored Mar 8, 2023
1 parent dff55e0 commit ebedf83
Show file tree
Hide file tree
Showing 145 changed files with 480 additions and 465 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .eslintrc.react.js → .eslintrc.react.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const base = require("./.eslintrc");
const base = require("./.eslintrc.cjs");

module.exports = {
...base,
Expand Down
18 changes: 3 additions & 15 deletions .github/workflows/test-app-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,13 @@ jobs:
os: [ubuntu-20.04, macos-11, windows-2022]
group: [1, 2, 3]
include:
# Important: Node 14 version must match minimum checked
# for CLI package.json, as well as IntelliJ and VS Code
# node --version checks.
- node-version: 14.18.0
- node-version: 16.x
os: ubuntu-latest
group: 1
- node-version: 14.18.0
- node-version: 16.x
os: ubuntu-latest
group: 2
- node-version: 14.18.0
os: ubuntu-latest
group: 3
- node-version: 16.9.0
os: ubuntu-latest
group: 1
- node-version: 16.9.0
os: ubuntu-latest
group: 2
- node-version: 16.9.0
- node-version: 16.x
os: ubuntu-latest
group: 3
steps:
Expand Down
3 changes: 0 additions & 3 deletions api/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
declaration: true,
clean: true,
};
11 changes: 2 additions & 9 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/api --no-deps --include-dependencies",
"build": "tsc && unbuild"
Expand Down
1 change: 1 addition & 0 deletions app/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../.eslintrc.react.cjs");
1 change: 0 additions & 1 deletion app/.eslintrc.js

This file was deleted.

3 changes: 0 additions & 3 deletions app/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
externals: ["@previewjs/api", "@previewjs/core"],
clean: true,
failOnWarn: false,
Expand Down
11 changes: 3 additions & 8 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
}
},
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/app --no-deps --include-dependencies",
"build": "tsc && unbuild && tsc -p client/tsconfig.json && vite build client",
Expand Down
2 changes: 2 additions & 0 deletions app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { SetupPreviewEnvironment } from "@previewjs/core";
import express from "express";
import path from "path";
import url from "url";

const setup: SetupPreviewEnvironment = async () => {
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
return {
middlewares: [express.static(path.join(__dirname, "..", "client", "dist"))],
};
Expand Down
3 changes: 0 additions & 3 deletions chromeless/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
declaration: true,
clean: true,
};
13 changes: 3 additions & 10 deletions chromeless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/chromeless --no-deps --include-dependencies",
"build": "rimraf dist && tsc && unbuild && vite build client"
Expand All @@ -35,7 +28,7 @@
"@previewjs/properties": "^1.0.8",
"@previewjs/vfs": "^1.2.16",
"express": "^4.18.2",
"playwright": "^1.31.2"
"playwright": "~1.29.2"
},
"devDependencies": {
"@types/express": "^4.17.17",
Expand Down
2 changes: 2 additions & 0 deletions chromeless/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { createFileSystemReader, Reader } from "@previewjs/vfs";
import express from "express";
import path from "path";
import url from "url";

export async function createChromelessWorkspace({
rootDirPath,
Expand All @@ -26,6 +27,7 @@ export async function createChromelessWorkspace({
`No compatible framework plugin found for directory: ${rootDirPath}`
);
}
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const clientDirPath = path.join(__dirname, "..", "client", "dist");
const workspace = await createWorkspace({
rootDirPath,
Expand Down
3 changes: 0 additions & 3 deletions config/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
externals: ["vite"],
declaration: true,
clean: true,
Expand Down
11 changes: 2 additions & 9 deletions config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"sideEffects": false,
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/config --no-deps --include-dependencies",
Expand Down
3 changes: 3 additions & 0 deletions config/src/read-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from "fs";
import { createRequire } from "module";
import path from "path";
import type { PreviewConfig } from "./config";

const require = createRequire(import.meta.url);

export const PREVIEW_CONFIG_NAME = "preview.config.js";

export function readConfig(rootDirPath: string): PreviewConfig {
Expand Down
3 changes: 0 additions & 3 deletions core/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
externals: ["vite"],
declaration: true,
clean: true,
Expand Down
11 changes: 2 additions & 9 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/core --no-deps --include-dependencies",
"build": "tsc && unbuild",
Expand Down
2 changes: 2 additions & 0 deletions core/src/caching.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from "fs";
import path from "path";
import url from "url";

export function getCacheDir(rootDirPath: string) {
try {
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const { version } = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "..", "package.json"), "utf8")
);
Expand Down
3 changes: 3 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Reader } from "@previewjs/vfs";
import express from "express";
import fs from "fs-extra";
import getPort from "get-port";
import { createRequire } from "module";
import path from "path";
import type * as vite from "vite";
import {
Expand All @@ -25,6 +26,8 @@ export type {
export { setupFrameworkPlugin } from "./plugins/setup-framework-plugin";
export type { SetupPreviewEnvironment } from "./preview-env";

const require = createRequire(import.meta.url);

process.on("uncaughtException", (e) => {
console.error("Uncaught Exception:", e);
});
Expand Down
3 changes: 3 additions & 0 deletions core/src/plugins/setup-framework-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from "fs-extra";
import { createRequire } from "module";
import path from "path";
import type { PackageDependencies } from "./dependencies";
import type { FrameworkPluginFactory } from "./framework";

const require = createRequire(import.meta.url);

export async function setupFrameworkPlugin({
rootDirPath,
frameworkPluginFactories,
Expand Down
4 changes: 2 additions & 2 deletions core/src/vite/plugins/component-loader-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PreviewConfig } from "@previewjs/config";
import { pathExistsSync } from "fs-extra";
import fs from "fs-extra";
import path from "path";
import { URLSearchParams } from "url";
import type { Plugin } from "vite";
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function refresh() {
const shouldAbortRender = () => renderId !== (getLatestComponentLoadId() + "-" + refreshId);
let loadingError = null;
${
wrapper && pathExistsSync(path.join(rootDirPath, wrapper.path))
wrapper && fs.pathExistsSync(path.join(rootDirPath, wrapper.path))
? `
let wrapperModulePromise;
if (import.meta.hot.data.preloadedWrapperModule) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/vite/vite-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ export class ViteManager {
])
);
const vitePlugins: Array<vite.PluginOption | vite.PluginOption[]> = [
// @ts-expect-error
viteTsconfigPaths.default({
viteTsconfigPaths({
root: this.options.rootDirPath,
projects: validTypeScriptFilePaths,
}),
Expand Down
2 changes: 1 addition & 1 deletion core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"lib": ["ES2019", "DOM"],
"lib": ["ES2020", "DOM"],
"moduleResolution": "node",
"rootDir": ".",
"outDir": "dist",
Expand Down
3 changes: 0 additions & 3 deletions csf3/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
declaration: true,
clean: true,
};
11 changes: 2 additions & 9 deletions csf3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prepublish": "cd .. && turbo run build --scope=@previewjs/csf3 --no-deps --include-dependencies",
"build": "tsc && unbuild",
Expand Down
6 changes: 1 addition & 5 deletions daemon/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
export default {
entries: ["./src/client", "./src/index"],
rollup: {
emitCJS: true,
inlineDependencies: true,
resolve: {
browser: false
},
commonjs: {
ignoreDynamicRequires: true,
browser: false,
},
},
declaration: true,
Expand Down
7 changes: 2 additions & 5 deletions daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"type": "commonjs",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./client": {
"require": "./dist/client.cjs",
"import": "./dist/client.mjs",
"types": "./dist/client.d.ts"
}
Expand Down
4 changes: 2 additions & 2 deletions daemon/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import type {
UpdateClientStatusResponse,
UpdatePendingFileRequest,
UpdatePendingFileResponse,
} from "./api";
} from "./api.js";
import { waitForSuccessfulPromise } from "./wait-for-successful-promise";
export * from "./api";
export * from "./api.js";

export function createClient(baseUrl: string): Client {
const locking = exclusivePromiseRunner();
Expand Down
4 changes: 2 additions & 2 deletions daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import type {
UpdateClientStatusResponse,
UpdatePendingFileRequest,
UpdatePendingFileResponse,
} from "./api";
import { createClient } from "./client";
} from "./api.js";
import { createClient } from "./client.js";

const AUTOMATIC_SHUTDOWN_DELAY_SECONDS = 30;

Expand Down
2 changes: 1 addition & 1 deletion daemon/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { startDaemon } from ".";
import { startDaemon } from "./index.js";

const port = parseInt(process.env.PORT || "9100");

Expand Down
Loading

0 comments on commit ebedf83

Please sign in to comment.