Skip to content

Commit

Permalink
changed sku context to single instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanDroryAu committed Dec 22, 2024
1 parent 3931a4b commit 2568101
Show file tree
Hide file tree
Showing 136 changed files with 1,221 additions and 1,035 deletions.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const seek = require('eslint-config-seek');
const jsdoc = require('eslint-plugin-jsdoc');
const unicorn = require('eslint-plugin-unicorn');
const globals = require('globals');
const importPlugin = require('eslint-plugin-import');

module.exports = [
{
Expand All @@ -26,6 +27,7 @@ module.exports = [
plugins: {
jsdoc,
unicorn,
import: importPlugin,
},

languageOptions: {
Expand Down Expand Up @@ -60,6 +62,7 @@ module.exports = [
node: {
moduleDirectory: ['node_modules'],
},
typescript: true,
},
},

Expand Down Expand Up @@ -107,6 +110,9 @@ module.exports = [
project: true,
},
},
parserOptions: {
project: true,
},
},
{
files: ['tests/**', 'test-utils/**'],
Expand Down
1 change: 0 additions & 1 deletion fixtures/sku-with-https/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# managed by sku
.eslintcache
.prettierrc
.ssl
coverage/
dist/
eslint.config.mjs
Expand Down
1 change: 1 addition & 0 deletions fixtures/sku-with-https/started.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Server started, here's your callback
2 changes: 1 addition & 1 deletion fixtures/ssr-hello-world/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.eslintcache
.prettierrc
coverage/
dist-build/
dist/
eslint.config.mjs
report/
tsconfig.json
Expand Down
2 changes: 1 addition & 1 deletion fixtures/ssr-hello-world/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.eslintcache
.prettierrc
coverage/
dist-build/
dist/
eslint.config.mjs
pnpm-lock.yaml
report/
Expand Down
1 change: 1 addition & 0 deletions fixtures/ssr-hello-world/started.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Server started, here's your callback
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = {
noInterop: false,
preserveImportMeta: true,
},
jsc: {
baseUrl: '.',
paths: {
'@/*': ['./packages/sku/src/*'],
},
},
},
],
},
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"dedent": "^1.5.1",
"eslint": "^9.10.0",
"eslint-config-seek": "^14.2.0",
"eslint-import-resolver-typescript": "^3.7.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jsdoc": "^50.2.2",
"eslint-plugin-unicorn": "^56.0.1",
"gh-pages": "^3.1.0",
Expand All @@ -70,7 +72,7 @@
"lint-staged": "^11.1.1",
"plop": "^4.0.1",
"prettier": "^3.4.1",
"puppeteer": "^22.10.0",
"puppeteer": "^22.15.0",
"renovate-config-seek": "^0.4.0",
"typescript": "~5.6.0"
},
Expand Down
15 changes: 9 additions & 6 deletions packages/sku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
".": "./sku-types.d.ts",
"./jest-preset": "./dist/config/jest/jest-preset.js",
"./config/eslint": {
"default": "./dist/config/eslint/index.js",
"types": "./dist/config/eslint/index.d.ts"
"default": "./dist/services/eslint/config/index.js",
"types": "./dist/services/eslint/config/index.d.ts"
},
"./config/storybook": {
"default": "./dist/config/storybook/index.cjs",
"types": "./dist/config/storybook/index.d.ts"
},
"./@loadable/component": "./src/@loadable/component/index.ts",
"./webpack-plugin": "./dist/config/webpack/plugins/sku-webpack-plugin/index.js",
"./webpack-plugin": "./dist/services/webpack/config/plugins/sku-webpack-plugin/index.js",
"./package.json": "./package.json"
},
"engines": {
Expand All @@ -35,8 +35,9 @@
"type": "module",
"scripts": {
"postinstall": "node ./scripts/postinstall.js",
"build": "tsc --project tsconfig.build.json",
"lint:tsc": "tsc --noEmit"
"build": "tspc --project tsconfig.build.json",
"lint:tsc": "tsc --noEmit",
"prepare": "ts-patch install -s"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -187,6 +188,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-helmet": "^6.1.0",
"react-router-dom": "^6.0.0"
"react-router-dom": "^6.0.0",
"ts-patch": "^3.3.0",
"typescript-transform-paths": "^3.5.2"
}
}
4 changes: 2 additions & 2 deletions packages/sku/src/bin/sku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import { program } from '../lib/program/index.js';
import { program } from '@/program/index.js';

program.parse();
program.parseAsync();
2 changes: 1 addition & 1 deletion packages/sku/src/config/babel/babelConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cwd } from '../../lib/cwd.js';
import { cwd } from '@/utils/cwd.js';
import { createRequire } from 'node:module';
import type { PluginItem } from '@babel/core';

Expand Down
18 changes: 16 additions & 2 deletions packages/sku/src/config/jest/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import escapeRegex from 'escape-string-regexp';
import { fileURLToPath } from 'node:url';
import { cwd } from '../../lib/cwd.js';
import { paths, rootResolution, jestDecorator } from '../../context/index.js';
import { cwd } from '@/utils/cwd.js';
import { getSkuContext } from '@/context/createSkuContext.js';

const { paths, rootResolution, jestDecorator } = await getSkuContext();

const slash = '[/\\\\]'; // Cross-platform path delimiter regex
const compilePackagesRegex = paths.compilePackages
.map((pkg) => `.*${escapeRegex(pkg)}`)
.join('|');

const asyncFunction = async () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('asyncFunction');
resolve();
}, 2000);
});
};

await asyncFunction();
console.log('returning the decorator');

/** @type {import('jest').Config} */
export default jestDecorator({
testEnvironment: 'jsdom',
Expand Down
5 changes: 4 additions & 1 deletion packages/sku/src/config/jest/jsBabelTransform.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import babelJest from 'babel-jest';

import { rootResolution } from '../../context/index.js';
import babelConfig from '../babel/babelConfig.js';
import targets from '../targets.json' with { type: 'json' };

import { getSkuContext } from '@/context/createSkuContext.js';

const { rootResolution } = await getSkuContext();

export default babelJest.createTransformer(
babelConfig({
target: 'jest',
Expand Down
4 changes: 3 additions & 1 deletion packages/sku/src/config/jest/tsBabelTransform.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import babelJest from 'babel-jest';

import { rootResolution } from '../../context/index.js';
import babelConfig from '../babel/babelConfig.js';
import targets from '../targets.json' with { type: 'json' };
import { getSkuContext } from '@/context/createSkuContext.js';

const { rootResolution } = await getSkuContext();

export default babelJest.createTransformer(
babelConfig({
Expand Down
4 changes: 2 additions & 2 deletions packages/sku/src/config/lintStaged/lintStagedConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
isYarn,
getCommand,
} from '../../services/packageManager/packageManager.js';
import { lintExtensions } from '../../services/eslint/lint.js';
} from '@/services/packageManager/packageManager.js';
import { lintExtensions } from '@/services/eslint/lint.js';
import type { Config } from 'lint-staged';

const config: Config = {
Expand Down
2 changes: 1 addition & 1 deletion packages/sku/src/config/storybook/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const webpackFinal = async (config, { configType }) => {

return makeStorybookWebpackConfig(config, {
// sku storybook -> configType === 'DEVELOPMENT'
// sku build-storybook -> configType === 'PRODUCTION'
// sku build-storybook -> configType === 'PRODUCTION'g
isDevServer: configType === 'DEVELOPMENT',
});
};
Expand Down
11 changes: 7 additions & 4 deletions packages/sku/src/config/storybook/storybookWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { paths } from '../../context/index.js';
import { merge as webpackMerge } from 'webpack-merge';
import makeWebpackConfig from '../webpack/webpack.config.js';
import { resolvePackage } from '../webpack/utils/resolvePackage.js';
import makeWebpackConfig from '@/services/webpack/config/webpack.config.js';
import { resolvePackage } from '@/services/webpack/config/utils/resolvePackage.js';
import { getSkuContext } from '@/context/createSkuContext.js';

const hot = process.env.SKU_HOT !== 'false';

Expand All @@ -12,11 +12,14 @@ const EXAMPLE_MDX_FILE = 'example.mdx';
* @param {import("webpack").Configuration} config
* @param {{isDevServer: boolean}}
*/
export default (config, { isDevServer }) => {
export default async (config, { isDevServer }) => {
const skuContext = await getSkuContext();
const { paths } = skuContext;
const clientWebpackConfig = makeWebpackConfig({
isIntegration: true,
isDevServer,
hot: isDevServer && hot,
skuContext,
}).find(({ name }) => name === 'client');

// Ensure Storybook's webpack loaders ignore our code :(
Expand Down
6 changes: 3 additions & 3 deletions packages/sku/src/config/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cwd } from '../../lib/cwd.js';
import { rootResolution, tsconfigDecorator } from '../../context/index.js';
import { cwd } from '@/utils/cwd.js';
import { SkuContext } from '@/context/createSkuContext.js';

export default () => {
export default ({ rootResolution, tsconfigDecorator }: SkuContext) => {
const config = {
compilerOptions: {
// Don't compile anything, only perform type checking
Expand Down
90 changes: 79 additions & 11 deletions packages/sku/src/context/createSkuContext.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
import type { SkuConfig, SkuRoute, SkuRouteObject } from '../../sku-types.js';
import { getPathFromCwd } from '../lib/cwd.js';
import type { SkuConfig, SkuRoute, SkuRouteObject } from '../../sku-types.d.ts';
import { getPathFromCwd } from '@/utils/cwd.js';
import { getConfigPath } from './configPath.js';
import { existsSync } from 'node:fs';
import defaultSkuConfig from './defaultSkuConfig.js';
import validateConfig from './validateConfig.js';
import isCompilePackage from '../lib/isCompilePackage.js';
import isCompilePackage from '@/utils/isCompilePackage.js';
import chalk from 'chalk';
import { join } from 'node:path';
import defaultCompilePackages from './defaultCompilePackages.js';
import defaultClientEntry from './defaultClientEntry.js';
import { createJiti } from 'jiti';

const getSkuConfig = async (): Promise<{
const jiti = createJiti(import.meta.url);

let storedSkuContext: SkuContext | null = null;

export const getSkuContext = async ({
isStartScript,
configPath,
}: {
isStartScript?: boolean;
configPath?: string;
} = {}) => {
if (storedSkuContext) {
return storedSkuContext;
}
storedSkuContext = await createSkuContext({ isStartScript, configPath });
return storedSkuContext;
};

const getSkuConfig = async ({
configPath,
}: {
configPath?: string;
}): Promise<{
appSkuConfig: SkuConfig;
appSkuConfigPath: string | null;
}> => {
let appSkuConfigPath;
const tsPath = getPathFromCwd('sku.config.ts');
const jsPath = getPathFromCwd('sku.config.js');

const customSkuConfig = getConfigPath() || process.env.SKU_CONFIG;
const customSkuConfig = configPath || process.env.SKU_CONFIG;

if (customSkuConfig) {
appSkuConfigPath = getPathFromCwd(customSkuConfig);
Expand Down Expand Up @@ -49,8 +72,16 @@ const getSkuConfig = async (): Promise<{
};
};

export const createSkuContext = async () => {
const { appSkuConfig, appSkuConfigPath } = await getSkuConfig();
export type NormalizedRoute = SkuRouteObject & { siteIndex?: number };

export const createSkuContext = async ({
isStartScript,
configPath,
}: {
isStartScript?: boolean;
configPath?: string;
}) => {
const { appSkuConfig, appSkuConfigPath } = await getSkuConfig({ configPath });

const skuConfig = {
...defaultSkuConfig,
Expand All @@ -70,11 +101,8 @@ export const createSkuContext = async () => {
process.exit(1);
}

const firstArg = process.argv[2];

const isStartScript = firstArg === 'start' || firstArg === 'start-ssr';

type NormalizedRoute = SkuRouteObject & { siteIndex?: number };

const normalizeRoute = (route: SkuRoute): NormalizedRoute =>
typeof route === 'string' ? { route } : route;

Expand Down Expand Up @@ -187,4 +215,44 @@ export const createSkuContext = async () => {
const skipPackageCompatibilityCompilation =
skuConfig.skipPackageCompatibilityCompilation!;
const externalizeNodeModules = skuConfig.externalizeNodeModules!;

const skuContext = {
publicPath,
skuConfig,
paths,
hosts,
port,
libraryName,
libraryFile,
isLibrary,
polyfills,
webpackDecorator,
jestDecorator,
eslintDecorator,
tsconfigDecorator,
eslintIgnore,
routes,
environments,
supportedBrowsers,
sourceMapsProd,
displayNamesProd,
cspEnabled,
cspExtraScriptSrcHosts,
httpsDevServer,
rootResolution,
languages,
initialPath,
transformOutputPath,
sites,
useDevServerMiddleware,
skipPackageCompatibilityCompilation,
externalizeNodeModules,
defaultClientEntry,
};

storedSkuContext = skuContext;

return skuContext;
};

export type SkuContext = Awaited<ReturnType<typeof createSkuContext>>;
Loading

0 comments on commit 2568101

Please sign in to comment.