Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types, better exports and use tsup for build, remove parcel #6461

Merged
merged 12 commits into from
Nov 4, 2024
1 change: 1 addition & 0 deletions packages/registry/news/6461.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix types for addons TS, fix tsconfig for node side @sneridagh
sneridagh marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/registry/news/6461.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace `parcel` with `tsup` for build @sneridagh
sneridagh marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 14 additions & 23 deletions packages/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,28 @@
"access": "public"
},
"type": "module",
"source": "src/index.ts",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/types.d.ts",
"main": "dist/index.js",
"exports": {
"./src/*": "./src/*.cjs",
"./package.json": "./package.json",
"./addon-registry": {
"require": "./dist/cjs/addon-registry.cjs",
"import": "./dist/esm/addon-registry.js",
"types": "./dist/esm/addon-registry.d.ts"
"import": "./dist/addon-registry/addon-registry.js",
"default": "./dist/addon-registry/addon-registry.cjs"
},
"./create-addons-loader": {
"require": "./dist/cjs/create-addons-loader.cjs",
"import": "./dist/esm/create-addons-loader.js",
"types": "./dist/esm/create-addons-loader.d.ts"
"import": "./dist/addon-registry/create-addons-loader.js",
"default": "./dist/addon-registry/create-addons-loader.cjs"
},
"./create-theme-loader": {
"require": "./dist/cjs/create-theme-loader.cjs",
"import": "./dist/esm/create-theme-loader.js",
"types": "./dist/esm/create-theme-loader.d.ts"
"import": "./dist/addon-registry/create-theme-loader.js",
"default": "./dist/addon-registry/create-theme-loader.cjs"
},
"./vite-plugin": {
"import": "./vite-plugin.js",
"types": "./vite-plugin.d.ts"
},
".": {
"types": "./dist/types.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"default": "./dist/index.cjs"
}
},
"targets": {
Expand All @@ -70,10 +63,9 @@
},
"scripts": {
"watch": "parcel watch",
sneridagh marked this conversation as resolved.
Show resolved Hide resolved
"build": "parcel build && pnpm build:node:esm && pnpm build:node:cjs",
"build:force": "rm -rf dist && parcel build --no-cache && pnpm build:node:esm && pnpm build:node:cjs",
"build:node:esm": "tsc --project tsconfig.node.json || true",
"build:node:cjs": "tsc --project tsconfig.node.json --module commonjs --moduleResolution Node --outDir dist/cjs || true && make fix-build",
"build": "tsup",
"build:force": "rm -rf dist && tsup",
"check:exports": "attw --pack .",
"test": "vitest",
"test:debug": "vitest --inspect-brk --no-file-parallelism registry",
"dry-release": "release-it --dry-run",
Expand All @@ -99,20 +91,19 @@
"tmp": "0.2.1"
},
"devDependencies": {
"@parcel/packager-ts": "^2.12.0",
"@parcel/transformer-typescript-types": "^2.12.0",
"@arethetypeswrong/cli": "^0.16.4",
"@plone/types": "workspace:*",
"@types/debug": "^4.1.12",
"@types/glob": "^8.1.0",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/tmp": "^0.2.6",
"parcel": "^2.12.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"release-it": "16.2.1",
"tsconfig": "workspace:*",
"tsup": "^8.0.2",
"typescript": "^5.6.3",
"vite": "^5.4.8",
"vitest": "^2.1.3"
Expand Down
47 changes: 26 additions & 21 deletions packages/registry/src/addon-registry/addon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ function buildDependencyGraph(
}

addons.forEach((loaderString) => {
const [name, extra] = loaderString.split(':');
const [name, extra] = loaderString.split(':') as [
string,
string | undefined,
];
if (!graph.hasNode(name)) {
graph.addNode(name, []);
}
Expand Down Expand Up @@ -199,7 +202,7 @@ class AddonRegistry {

this.addonNames = this.resultantMergedAddons.map(
(s: string) => s.split(':')[0],
);
) as Array<string>;
this.packages = {};
this.customizations = new Map();

Expand All @@ -215,14 +218,14 @@ class AddonRegistry {
this.dependencyGraph = buildDependencyGraph(
[
...(Object.keys(this.coreAddons).map(
(key) => this.coreAddons[key].package,
(key) => this.coreAddons[key]?.package as string,
) || []),
...this.resultantMergedAddons,
...(process.env.ADDONS ? process.env.ADDONS.split(';') : []),
],
(name) => {
this.initPublishedPackage(name);
return this.packages[name].addons || [];
return this.packages[name]?.addons || [];
},
);

Expand Down Expand Up @@ -361,14 +364,15 @@ class AddonRegistry {
initDevelopmentPackage(name: string) {
const [baseUrl, pathsConfig] = this.getTSConfigPaths();
if (pathsConfig && pathsConfig.hasOwnProperty(name)) {
const packagePath = `${this.projectRootPath}/${baseUrl}/${pathsConfig[name][0]}`;
const packagePath = `${this.projectRootPath}/${baseUrl}/${pathsConfig[name]![0]}`;
const packageJsonPath = `${getPackageBasePath(packagePath)}/package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const innerAddons: Array<string> = packageJson.addons || [];
const innerAddonsNormalized = innerAddons.map((s) => s.split(':')[0]);
if (this.addonNames.includes(name) && innerAddonsNormalized.length > 0) {
innerAddonsNormalized.forEach((name) => {
if (!this.addonNames.includes(name)) this.addonNames.push(name);
if (!this.addonNames.includes(name as string))
this.addonNames.push(name as string);
});
}
const pkg = {
Expand Down Expand Up @@ -415,7 +419,8 @@ class AddonRegistry {
const innerAddonsNormalized = innerAddons.map((s) => s.split(':')[0]);
if (this.addonNames.includes(name) && innerAddonsNormalized.length > 0) {
innerAddonsNormalized.forEach((name) => {
if (!this.addonNames.includes(name)) this.addonNames.push(name);
if (!this.addonNames.includes(name as string))
this.addonNames.push(name as string);
});
}
const packageTSConfig = this.getTSConfigPaths(basePath);
Expand Down Expand Up @@ -444,7 +449,7 @@ class AddonRegistry {

// An add-on from the ADDONS env var can only be a published one
initAddonFromEnvVar(name: string) {
const normalizedAddonName = name.split(':')[0];
const normalizedAddonName = name.split(':')[0] as string;
this.initPublishedPackage(normalizedAddonName);
}

Expand All @@ -460,14 +465,14 @@ class AddonRegistry {
*/
initAddonExtenders() {
this.getAddons().forEach((addon) => {
const base = path.dirname(addon.packageJson);
const base = path.dirname(addon!.packageJson);
const razzlePath = path.resolve(`${base}/razzle.extend.js`);
if (fs.existsSync(razzlePath)) {
addon.razzleExtender = razzlePath;
addon!.razzleExtender = razzlePath;
}
const eslintPath = path.resolve(`${base}/eslint.extend.js`);
if (fs.existsSync(eslintPath)) {
addon.eslintExtender = eslintPath;
addon!.eslintExtender = eslintPath;
}
});
}
Expand All @@ -483,13 +488,13 @@ class AddonRegistry {

getAddonExtenders() {
return this.getAddons()
.map((o) => o.razzleExtender)
.map((o) => o?.razzleExtender)
.filter((e) => e);
}

getEslintExtenders() {
return this.getAddons()
.map((o) => o.eslintExtender)
.map((o) => o?.eslintExtender)
.filter((e) => e);
}

Expand All @@ -503,11 +508,11 @@ class AddonRegistry {
};

this.getAddonDependencies().forEach((addon) => {
const normalizedAddonName = addon.split(':')[0];
const normalizedAddonName = addon.split(':')[0] as string;
// We have two possible insertion points, variables and main

const customThemeVariables = `${this.packages[normalizedAddonName].modulePath}/theme/_variables.scss`;
const customThemeMain = `${this.packages[normalizedAddonName].modulePath}/theme/_main.scss`;
const customThemeVariables = `${this.packages[normalizedAddonName]?.modulePath}/theme/_variables.scss`;
const customThemeMain = `${this.packages[normalizedAddonName]?.modulePath}/theme/_main.scss`;
if (
fs.existsSync(customThemeVariables) &&
normalizedAddonName !== this.theme
Expand Down Expand Up @@ -555,12 +560,12 @@ class AddonRegistry {
getResolveAliases() {
const pairs: [string, string][] = Object.keys(this.packages).map((o) => [
o,
this.packages[o].modulePath,
this.packages[o]?.modulePath || '',
]);

let aliasesFromTSPaths = {};
Object.keys(this.packages).forEach((o) => {
if (this.packages[o].tsConfigPaths) {
if (this.packages[o]?.tsConfigPaths) {
aliasesFromTSPaths = {
...aliasesFromTSPaths,
...this.getAliasesFromTSConfig(
Expand Down Expand Up @@ -734,8 +739,8 @@ class AddonRegistry {
aliases = {
...aliases,
...this.getCustomizationPaths(
JSON.parse(fs.readFileSync(addon.packageJson, 'utf-8')),
getPackageBasePath(addon.modulePath),
JSON.parse(fs.readFileSync(addon!.packageJson, 'utf-8')),
getPackageBasePath(addon!.modulePath),
),
};
});
Expand Down Expand Up @@ -805,7 +810,7 @@ class AddonRegistry {

if (!seen.has(dep)) {
seen.add(dep);
queue.push(dep);
queue.push(dep as string);
}
out += ` "${name}" -> "${dep}"\n`;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/registry/src/addon-registry/create-addons-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ Instead, change the "addons" setting in your package.json file.
let extras: string[] | string[][] = []; // TODO: Improve this typing
const addonConfigLoadInfo = addonConfigString.split(':');
const pkgName = addonConfigLoadInfo[0];
const defaultImport = nameFromPackage(pkgName);
const defaultImport = nameFromPackage(pkgName as string);
if (addonConfigLoadInfo.length > 1) {
extras = addonConfigLoadInfo[1].split(',');
}
// @ts-expect-error This forEach is a complete mess
extras = extras.map((name) => [name, `${name}${counter++}`]);
const line = `import ${defaultImport}${
extras.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function nameFromPackage(name: string) {
* Creates a static file with code necessary to load the addons configuration
*
*/
function getThemeLoaderCode(name, customThemeAddons = []) {
function getThemeLoaderCode(name: string, customThemeAddons = []) {
let buf = `/*
This file is autogenerated. Don't change it directly.
Add a ./theme/_${name}.scss in your add-on to load your theme customizations in the current theme.
Expand Down
5 changes: 3 additions & 2 deletions packages/registry/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"jsx": "react-jsx",
"paths": {}
},
"include": ["src/index.ts", "src/**/*.js", "src/**/*.cjs"],
"include": ["src/index.ts"],
"exclude": [
"node_modules",
"build",
Expand All @@ -26,5 +26,6 @@
"src/**/*.test.{js,jsx,ts,tsx}",
"src/**/*.spec.{js,jsx,ts,tsx}",
"src/**/*.stories.{js,jsx,ts,tsx}"
]
],
"references": [{ "path": "./tsconfig.node.json" }]
}
7 changes: 4 additions & 3 deletions packages/registry/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"composite": true,
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
Expand All @@ -11,10 +12,10 @@
/* Strictness */
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
/* If transpiling with TypeScript: */
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "dist/esm",
"module": "Preserve",
"outDir": "dist",
"sourceMap": true,
/* If your code doesn't run in the DOM: */
"lib": ["es2022"],
Expand Down
9 changes: 9 additions & 0 deletions packages/registry/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entryPoints: ['src/index.ts', 'src/addon-registry/**/*.ts'],
format: ['cjs', 'esm'],
dts: true,
outDir: 'dist',
clean: true,
});
4 changes: 2 additions & 2 deletions packages/volto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
"@plone/volto/babel": "<rootDir>/babel.js",
"@plone/volto/(.*)$": "<rootDir>/src/$1",
"@plone/volto-slate/(.*)$": "<rootDir>/../volto-slate/src/$1",
"@plone/registry/addon-registry$": "<rootDir>/node_modules/@plone/registry/dist/esm/addon-registry.js",
"@plone/registry/create-addons-loader$": "<rootDir>/node_modules/@plone/registry/dist/esm/create-addons-loader.js",
"@plone/registry/addon-registry$": "<rootDir>/node_modules/@plone/registry/dist/addon-registry/addon-registry.js",
"@plone/registry/create-addons-loader$": "<rootDir>/node_modules/@plone/registry/dist/addon-registry/create-addons-loader.js",
"@plone/registry": "<rootDir>/../registry/src",
"@plone/registry/(.*)$": "<rootDir>/../registry/src/$1",
"@plone/volto": "<rootDir>/src/index.js",
Expand Down
Loading
Loading