@ecopages/codemod 0.0.0-stage → 0.9.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -2
- package/package.json +61 -4
- package/src/transforms/migrate-core-entrypoints.d.ts +3 -0
- package/src/transforms/migrate-core-entrypoints.js +303 -0
- package/src/transforms/migrate-data-hooks.d.ts +36 -0
- package/src/transforms/migrate-data-hooks.js +477 -0
- package/src/transforms/migrate-to-eco-component.d.ts +18 -0
- package/src/transforms/migrate-to-eco-component.js +224 -0
- package/src/transforms/migrate-to-eco-page.d.ts +23 -0
- package/src/transforms/migrate-to-eco-page.js +444 -0
- package/src/transforms/print-options.d.ts +13 -0
- package/src/transforms/print-options.js +15 -0
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ecopages/codemod
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
Automated code transformations for upgrading Ecopages projects. This is an internal workspace package and is not published to npm. To run a transform, check out the Ecopages repository at the release ref matching the installed `@ecopages/core` version, then run the examples below from that checkout with an absolute path to the project being migrated.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install --filter @ecopages/codemod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Run transforms through the workspace package so they use the version from the matching Ecopages checkout. Pass absolute paths to the project files being migrated.
|
|
12
|
+
|
|
13
|
+
## Complete Migration Script
|
|
14
|
+
|
|
15
|
+
Run this from the Ecopages checkout to migrate components, layouts, includes, and pages in another project:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Migrate components, layouts, and includes
|
|
19
|
+
pnpm --filter @ecopages/codemod exec jscodeshift \
|
|
20
|
+
-t src/transforms/migrate-to-eco-component.ts \
|
|
21
|
+
--parser tsx \
|
|
22
|
+
--extensions=ts,tsx,js,jsx \
|
|
23
|
+
/path/to/project/src/components \
|
|
24
|
+
/path/to/project/src/layouts \
|
|
25
|
+
/path/to/project/src/includes
|
|
26
|
+
|
|
27
|
+
# Migrate pages
|
|
28
|
+
pnpm --filter @ecopages/codemod exec jscodeshift \
|
|
29
|
+
-t src/transforms/migrate-to-eco-page.ts \
|
|
30
|
+
--parser tsx \
|
|
31
|
+
--extensions=ts,tsx,js,jsx \
|
|
32
|
+
/path/to/project/src/pages
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Individual Migrations
|
|
36
|
+
|
|
37
|
+
### Migrate the Page Data Hooks to `props` and `paths`
|
|
38
|
+
|
|
39
|
+
Renames the `staticProps` and `staticPaths` options of `eco.page({ ... })` to `props` and `paths`, and drops the envelopes they returned: `props` returns the props object instead of `{ props }`, and `paths` returns `[{ params }]` instead of `{ paths }`. Core fails a page that still declares the old names and points here. The transform also renames the `GetStaticProps`, `GetStaticPaths` and `StaticPath` types imported from `@ecopages/*` to `GetProps`, `GetPaths` and `PagePath`. The props context passes `params` where it passed `pathname: { params }`, so it rewrites a `props` parameter written as `{ pathname: { params } }`, `{ pathname }` or `ctx.pathname.params` to read `params`. A hook that reads `pathname` any other way, or already has a `params` binding, keeps its `pathname` and is reported.
|
|
40
|
+
|
|
41
|
+
It unwraps an object literal that holds only the envelope key, in an inline hook or a same-file function the option names. Any other return value in an `async` hook becomes `(await value).props` or `(await value).paths`. A hook it cannot unwrap, such as one imported from another file or a synchronous one with a computed return, is left unchanged and reported, so the old name still fails loudly. Rename those by hand.
|
|
42
|
+
|
|
43
|
+
This transform is one self-contained file, so a project outside this repository runs it straight from GitHub, with no checkout. Replace `<version>` with the Ecopages version you upgrade to (tags look like `v0.8.0`), and run it from the project root:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx jscodeshift@17 --parser tsx --extensions=ts,tsx,js,jsx -t https://raw.githubusercontent.com/ecopages/ecopages/refs/tags/v<version>/packages/codemod/src/transforms/migrate-data-hooks.ts src
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The raw output indents rewritten lines with extra tabs, so run your formatter afterwards. Inside this repository, run `pnpm --filter @ecopages/codemod exec jscodeshift -t src/transforms/migrate-data-hooks.ts --parser tsx --extensions=ts,tsx,js,jsx /path/to/project/src`.
|
|
50
|
+
|
|
51
|
+
### Migrate Pages to `eco.page()`
|
|
52
|
+
|
|
53
|
+
Transforms pages from the removed module-level data exports to the `eco.page()` options. Core no longer reads `getStaticPaths`, `getStaticProps` or `getMetadata` exports and fails a page that still has them, so run this on every page before you upgrade. It finishes by running the data hooks migration above, so a migrated page declares `props` and `paths`. It migrates `export const` and `export function` declarations, `export { local as getStaticProps }` lists, the removed `eco.staticPaths()`, `eco.staticProps()` and `eco.metadata()` wrappers, legacy `Page.config` pages, and pages that already call `eco.page()`. A `typeof getStaticProps` type keeps the declaration in the file, unexported:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pnpm --filter @ecopages/codemod exec jscodeshift -t src/transforms/migrate-to-eco-page.ts --parser tsx --extensions=ts,tsx,js,jsx /path/to/project/src/pages
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Migrate Components to `eco.component()`
|
|
60
|
+
|
|
61
|
+
Transforms components from the legacy `.config` assignment to the consolidated API:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pnpm --filter @ecopages/codemod exec jscodeshift -t src/transforms/migrate-to-eco-component.ts --parser tsx --extensions=ts,tsx,js,jsx /path/to/project/src/components
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Migrate Core Entry Points
|
|
68
|
+
|
|
69
|
+
When upgrading to the role-based `@ecopages/core` exports, rewrite static imports and re-exports whose requested names are present in the destination entry. The transform also rewrites matching string-literal paths in `import.meta.resolve()` and `require.resolve()` calls. It leaves matching string-literal dynamic `import()` and `require()` calls unchanged and reports them for manual review because it cannot verify which exports the caller reads:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pnpm --filter @ecopages/codemod exec jscodeshift \
|
|
73
|
+
-t src/transforms/migrate-core-entrypoints.ts \
|
|
74
|
+
--parser tsx \
|
|
75
|
+
--extensions=ts,tsx,js,jsx \
|
|
76
|
+
/path/to/project/src
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The transform leaves imports unchanged and reports them when a requested named export is not present in the destination role entry. Review those imports manually. Matching string-literal dynamic `import()` and `require()` calls stay unchanged with a warning, since their accessed names cannot be checked safely. Computed or other nonliteral module paths are left unchanged without a warning. `@ecopages/core/config/write-types` and `@ecopages/core/hmr/client/hmr-runtime` remain unchanged because core loads those entries by path.
|
|
80
|
+
|
|
81
|
+
## Options
|
|
82
|
+
|
|
83
|
+
| Option | Description |
|
|
84
|
+
| --------- | ---------------------------------------- |
|
|
85
|
+
| `--dry` | Preview changes without writing to files |
|
|
86
|
+
| `--print` | Print transformed output to stdout |
|
|
87
|
+
| `-v 2` | Verbose output showing all changes |
|
|
88
|
+
|
|
89
|
+
## Transform Details
|
|
90
|
+
|
|
91
|
+
### Pages
|
|
92
|
+
|
|
93
|
+
**Before:**
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
export const getStaticPaths = async () => ({ paths: [...] });
|
|
97
|
+
export const getStaticProps = async ({ pathname }) => ({ props: {...} });
|
|
98
|
+
export const getMetadata = ({ props }) => ({ title: '...' });
|
|
99
|
+
|
|
100
|
+
const Page: EcoComponent = () => <h1>...</h1>;
|
|
101
|
+
Page.config = { layout: BaseLayout };
|
|
102
|
+
export default Page;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**After:**
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
export default eco.page({
|
|
109
|
+
layout: BaseLayout,
|
|
110
|
+
paths: async () => [...],
|
|
111
|
+
props: async ({ params }) => ({...}),
|
|
112
|
+
metadata: ({ props }) => ({ title: '...' }),
|
|
113
|
+
render: () => <h1>...</h1>,
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Components
|
|
118
|
+
|
|
119
|
+
**Before:**
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
export const Counter: EcoComponent<Props> = ({ count }) => <my-counter />;
|
|
123
|
+
Counter.config = { dependencies: { scripts: ['./script.ts'] } };
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**After:**
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
export const Counter = eco.component<Props>({
|
|
130
|
+
dependencies: { scripts: ['./script.ts'] },
|
|
131
|
+
render: ({ count }) => <my-counter />,
|
|
132
|
+
});
|
|
133
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/codemod",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.9.0-rc.0",
|
|
4
|
+
"description": "Codemods for migrating ecopages to latest patterns",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./migrate-core-entrypoints": {
|
|
9
|
+
"import": "./src/transforms/migrate-core-entrypoints.js",
|
|
10
|
+
"types": "./src/transforms/migrate-core-entrypoints.d.ts",
|
|
11
|
+
"default": "./src/transforms/migrate-core-entrypoints.js"
|
|
12
|
+
},
|
|
13
|
+
"./migrate-data-hooks": {
|
|
14
|
+
"import": "./src/transforms/migrate-data-hooks.js",
|
|
15
|
+
"types": "./src/transforms/migrate-data-hooks.d.ts",
|
|
16
|
+
"default": "./src/transforms/migrate-data-hooks.js"
|
|
17
|
+
},
|
|
18
|
+
"./migrate-to-eco-component": {
|
|
19
|
+
"import": "./src/transforms/migrate-to-eco-component.js",
|
|
20
|
+
"types": "./src/transforms/migrate-to-eco-component.d.ts",
|
|
21
|
+
"default": "./src/transforms/migrate-to-eco-component.js"
|
|
22
|
+
},
|
|
23
|
+
"./migrate-to-eco-page": {
|
|
24
|
+
"import": "./src/transforms/migrate-to-eco-page.js",
|
|
25
|
+
"types": "./src/transforms/migrate-to-eco-page.d.ts",
|
|
26
|
+
"default": "./src/transforms/migrate-to-eco-page.js"
|
|
27
|
+
},
|
|
28
|
+
"./migrate-core-entrypoints.ts": {
|
|
29
|
+
"import": "./src/transforms/migrate-core-entrypoints.js",
|
|
30
|
+
"types": "./src/transforms/migrate-core-entrypoints.d.ts",
|
|
31
|
+
"default": "./src/transforms/migrate-core-entrypoints.js"
|
|
32
|
+
},
|
|
33
|
+
"./migrate-data-hooks.ts": {
|
|
34
|
+
"import": "./src/transforms/migrate-data-hooks.js",
|
|
35
|
+
"types": "./src/transforms/migrate-data-hooks.d.ts",
|
|
36
|
+
"default": "./src/transforms/migrate-data-hooks.js"
|
|
37
|
+
},
|
|
38
|
+
"./migrate-to-eco-component.ts": {
|
|
39
|
+
"import": "./src/transforms/migrate-to-eco-component.js",
|
|
40
|
+
"types": "./src/transforms/migrate-to-eco-component.d.ts",
|
|
41
|
+
"default": "./src/transforms/migrate-to-eco-component.js"
|
|
42
|
+
},
|
|
43
|
+
"./migrate-to-eco-page.ts": {
|
|
44
|
+
"import": "./src/transforms/migrate-to-eco-page.js",
|
|
45
|
+
"types": "./src/transforms/migrate-to-eco-page.d.ts",
|
|
46
|
+
"default": "./src/transforms/migrate-to-eco-page.js"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
55
|
+
"directory": "packages/codemod"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"jscodeshift": "^17.3.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"typescript": "^5.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { printOptions } from './print-options.js';
|
|
5
|
+
export const parser = 'tsx';
|
|
6
|
+
const coreEntryBySubpath = {
|
|
7
|
+
'assets/hashed-browser-asset': 'processor',
|
|
8
|
+
'build/browser-runtime-manifest': 'build',
|
|
9
|
+
'build/browser-runtime-plugin': 'build',
|
|
10
|
+
'build/build-adapter': 'build',
|
|
11
|
+
'build/build-queue': 'build',
|
|
12
|
+
'build/contracts/content-virtual-modules': 'build/contracts',
|
|
13
|
+
'build/contracts/server-only-specifier': 'build/contracts',
|
|
14
|
+
'build/jsx-ownership-plugins': 'build',
|
|
15
|
+
'build/lit-static-render-worker-context': 'build',
|
|
16
|
+
'build/server-module': 'build',
|
|
17
|
+
cache: 'build',
|
|
18
|
+
'client/navigation-scripts': 'client',
|
|
19
|
+
'client/scroll': 'client',
|
|
20
|
+
'client/view-transitions': 'client',
|
|
21
|
+
'config/app-runtime': 'integration',
|
|
22
|
+
constants: 'build',
|
|
23
|
+
'dev-toolbar/build-error-contract': 'client',
|
|
24
|
+
'dev-toolbar/define-dev-tool': 'client',
|
|
25
|
+
'dev-toolbar/dev-toolbar-manifest-contract': 'client',
|
|
26
|
+
'dev/development-restart': 'build',
|
|
27
|
+
'dev/transform-server': 'integration',
|
|
28
|
+
'diagnostics/request-pipeline-metrics': 'build',
|
|
29
|
+
'eco/page-layout-normalization': 'client',
|
|
30
|
+
hash: 'build',
|
|
31
|
+
'hmr/hmr-entrypoint-output': 'integration',
|
|
32
|
+
'hmr/hmr-registered-entrypoints-context': 'integration',
|
|
33
|
+
'hmr/hmr-strategy': 'integration',
|
|
34
|
+
'internal/finalize-config': 'config',
|
|
35
|
+
'plugins/define-integration': 'integration',
|
|
36
|
+
'plugins/integration-plugin': 'integration',
|
|
37
|
+
'plugins/jsx-import-source.utils': 'integration',
|
|
38
|
+
'plugins/package-specifier': 'build',
|
|
39
|
+
'plugins/processor': 'processor',
|
|
40
|
+
'plugins/tsconfig-import-resolver': 'build',
|
|
41
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader': 'integration',
|
|
42
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer': 'integration',
|
|
43
|
+
'route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint': 'integration',
|
|
44
|
+
'route-renderer/orchestration/document-shell/document-shell-render.service': 'integration',
|
|
45
|
+
'route-renderer/orchestration/document-shell/layout-shell-props.service': 'integration',
|
|
46
|
+
'route-renderer/orchestration/foreign-child/component-render-context': 'integration',
|
|
47
|
+
'route-renderer/orchestration/foreign-child/foreign-subtree-execution.service': 'integration',
|
|
48
|
+
'route-renderer/orchestration/integration-renderer': 'integration',
|
|
49
|
+
'route-renderer/orchestration/page-browser-graph/page-browser-graph-merge.utils': 'integration',
|
|
50
|
+
'route-renderer/orchestration/string-markup-renderer': 'integration',
|
|
51
|
+
'route-renderer/page-loading/file-scoped-dependency-components': 'integration',
|
|
52
|
+
'route-renderer/route-renderer': 'integration',
|
|
53
|
+
'router/link-intent': 'client',
|
|
54
|
+
'router/link-navigation-policy': 'client',
|
|
55
|
+
'router/navigation-coordinator': 'client',
|
|
56
|
+
'router/navigation-lifecycle': 'client',
|
|
57
|
+
'router/navigation-pipeline': 'client',
|
|
58
|
+
'services/asset-processing-service': 'integration',
|
|
59
|
+
'utils/path-containment': 'build',
|
|
60
|
+
'utils/resolve-entry-file': 'build',
|
|
61
|
+
};
|
|
62
|
+
function entrypointFor(specifier) {
|
|
63
|
+
const prefix = '@ecopages/core/';
|
|
64
|
+
if (!specifier.startsWith(prefix)) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const entry = coreEntryBySubpath[specifier.slice(prefix.length)];
|
|
68
|
+
return entry ? `${prefix}${entry}` : undefined;
|
|
69
|
+
}
|
|
70
|
+
function findCoreSourceRoot() {
|
|
71
|
+
let directory = process.cwd();
|
|
72
|
+
while (true) {
|
|
73
|
+
const sourceRoot = path.join(directory, 'packages/core/src');
|
|
74
|
+
if (existsSync(path.join(sourceRoot, 'client.ts'))) {
|
|
75
|
+
return sourceRoot;
|
|
76
|
+
}
|
|
77
|
+
const parent = path.dirname(directory);
|
|
78
|
+
if (parent === directory) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
directory = parent;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function resolveCoreModule(sourceRoot, fromFile, moduleSpecifier) {
|
|
85
|
+
const resolvedPath = path.resolve(path.dirname(fromFile), moduleSpecifier);
|
|
86
|
+
const candidates = [
|
|
87
|
+
resolvedPath,
|
|
88
|
+
`${resolvedPath}.ts`,
|
|
89
|
+
`${resolvedPath}.tsx`,
|
|
90
|
+
path.join(resolvedPath, 'index.ts'),
|
|
91
|
+
path.join(resolvedPath, 'index.tsx'),
|
|
92
|
+
];
|
|
93
|
+
return candidates.find((candidate) => candidate.startsWith(sourceRoot) && existsSync(candidate) && statSync(candidate).isFile());
|
|
94
|
+
}
|
|
95
|
+
function namesFromExportDeclaration(statement, sourceFile, sourceRoot, visited) {
|
|
96
|
+
const names = new Set();
|
|
97
|
+
if (!statement.moduleSpecifier || !ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
98
|
+
if (statement.exportClause && ts.isNamedExports(statement.exportClause)) {
|
|
99
|
+
for (const specifier of statement.exportClause.elements) {
|
|
100
|
+
names.add(specifier.name.text);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return names;
|
|
104
|
+
}
|
|
105
|
+
const target = resolveCoreModule(sourceRoot, sourceFile, statement.moduleSpecifier.text);
|
|
106
|
+
if (!target) {
|
|
107
|
+
return names;
|
|
108
|
+
}
|
|
109
|
+
if (!statement.exportClause) {
|
|
110
|
+
return exportedNames(target, sourceRoot, visited);
|
|
111
|
+
}
|
|
112
|
+
if (ts.isNamedExports(statement.exportClause)) {
|
|
113
|
+
for (const specifier of statement.exportClause.elements) {
|
|
114
|
+
names.add(specifier.name.text);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
names.add(statement.exportClause.name.text);
|
|
119
|
+
}
|
|
120
|
+
return names;
|
|
121
|
+
}
|
|
122
|
+
function namesFromDeclaredExport(statement) {
|
|
123
|
+
const names = new Set();
|
|
124
|
+
const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;
|
|
125
|
+
if (!modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
126
|
+
return names;
|
|
127
|
+
}
|
|
128
|
+
if (modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword)) {
|
|
129
|
+
names.add('default');
|
|
130
|
+
}
|
|
131
|
+
else if (ts.isVariableStatement(statement)) {
|
|
132
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
133
|
+
if (ts.isIdentifier(declaration.name)) {
|
|
134
|
+
names.add(declaration.name.text);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (ts.isFunctionDeclaration(statement) ||
|
|
139
|
+
ts.isClassDeclaration(statement) ||
|
|
140
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
141
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
142
|
+
ts.isEnumDeclaration(statement)) {
|
|
143
|
+
if (statement.name) {
|
|
144
|
+
names.add(statement.name.text);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return names;
|
|
148
|
+
}
|
|
149
|
+
function exportedNames(sourceFile, sourceRoot, visited = new Set()) {
|
|
150
|
+
if (visited.has(sourceFile)) {
|
|
151
|
+
return new Set();
|
|
152
|
+
}
|
|
153
|
+
visited.add(sourceFile);
|
|
154
|
+
const source = readFileSync(sourceFile, 'utf8');
|
|
155
|
+
const parsed = ts.createSourceFile(sourceFile, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
156
|
+
const names = new Set();
|
|
157
|
+
for (const statement of parsed.statements) {
|
|
158
|
+
const statementNames = ts.isExportDeclaration(statement)
|
|
159
|
+
? namesFromExportDeclaration(statement, sourceFile, sourceRoot, visited)
|
|
160
|
+
: namesFromDeclaredExport(statement);
|
|
161
|
+
for (const name of statementNames) {
|
|
162
|
+
names.add(name);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return names;
|
|
166
|
+
}
|
|
167
|
+
function coreEntrypointNames(entrypoint, sourceRoot) {
|
|
168
|
+
const entryPath = entrypoint.replace('@ecopages/core/', '');
|
|
169
|
+
const entryFile = resolveCoreModule(sourceRoot, path.join(sourceRoot, '__entry__.ts'), `./${entryPath}`);
|
|
170
|
+
return entryFile ? exportedNames(entryFile, sourceRoot) : undefined;
|
|
171
|
+
}
|
|
172
|
+
function requestedNames(node) {
|
|
173
|
+
const declaration = node;
|
|
174
|
+
if (declaration.type === 'ExportAllDeclaration') {
|
|
175
|
+
return { reason: 'wildcard re-exports cannot be verified' };
|
|
176
|
+
}
|
|
177
|
+
const specifiers = declaration.specifiers ?? [];
|
|
178
|
+
if (specifiers.length === 0) {
|
|
179
|
+
return { reason: 'side-effect imports cannot be verified' };
|
|
180
|
+
}
|
|
181
|
+
const names = [];
|
|
182
|
+
for (const specifier of specifiers) {
|
|
183
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
184
|
+
return { reason: 'namespace imports cannot be verified' };
|
|
185
|
+
}
|
|
186
|
+
const imported = specifier.imported ??
|
|
187
|
+
(specifier.type === 'ExportSpecifier' ? specifier.local : undefined) ??
|
|
188
|
+
(specifier.type === 'ImportDefaultSpecifier' ? { name: 'default' } : undefined);
|
|
189
|
+
const name = imported?.name ?? imported?.value;
|
|
190
|
+
if (!name) {
|
|
191
|
+
return { reason: 'the imported name cannot be verified' };
|
|
192
|
+
}
|
|
193
|
+
names.push(name);
|
|
194
|
+
}
|
|
195
|
+
return { names };
|
|
196
|
+
}
|
|
197
|
+
function warnUnsupported(file, specifier, entrypoint, detail) {
|
|
198
|
+
console.warn(`[migrate-core-entrypoints] ${file.path}: left ${specifier} unchanged for ${entrypoint}; ${detail}. Review this import manually.`);
|
|
199
|
+
}
|
|
200
|
+
function literalValue(node) {
|
|
201
|
+
if (!node || typeof node !== 'object') {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
const literal = node;
|
|
205
|
+
return (literal.type === 'StringLiteral' || literal.type === 'Literal') && typeof literal.value === 'string'
|
|
206
|
+
? literal.value
|
|
207
|
+
: undefined;
|
|
208
|
+
}
|
|
209
|
+
function rewriteLiteral(node) {
|
|
210
|
+
const value = literalValue(node);
|
|
211
|
+
const entrypoint = value ? entrypointFor(value) : undefined;
|
|
212
|
+
if (!entrypoint || !node || typeof node !== 'object') {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
node.value = entrypoint;
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
function warnUnverifiedDynamicAccess(node, file) {
|
|
219
|
+
const specifier = literalValue(node);
|
|
220
|
+
const entrypoint = specifier ? entrypointFor(specifier) : undefined;
|
|
221
|
+
if (specifier && entrypoint) {
|
|
222
|
+
warnUnsupported(file, specifier, entrypoint, 'dynamic module access cannot be verified');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function isImportMetaResolve(callee) {
|
|
226
|
+
const call = callee;
|
|
227
|
+
return (call?.type === 'MemberExpression' &&
|
|
228
|
+
call.property?.name === 'resolve' &&
|
|
229
|
+
call.object?.type === 'MetaProperty' &&
|
|
230
|
+
call.object.property?.name === 'meta' &&
|
|
231
|
+
call.object.meta?.type === 'Identifier' &&
|
|
232
|
+
call.object.meta.name === 'import');
|
|
233
|
+
}
|
|
234
|
+
function isRequireResolve(callee) {
|
|
235
|
+
const call = callee;
|
|
236
|
+
return (call?.type === 'MemberExpression' &&
|
|
237
|
+
call.property?.name === 'resolve' &&
|
|
238
|
+
call.object?.type === 'Identifier' &&
|
|
239
|
+
call.object.name === 'require');
|
|
240
|
+
}
|
|
241
|
+
function rewriteModuleImports(root, j, file, sourceRoot) {
|
|
242
|
+
let changed = false;
|
|
243
|
+
const staticDeclarations = [
|
|
244
|
+
root.find(j.ImportDeclaration),
|
|
245
|
+
root.find(j.ExportNamedDeclaration),
|
|
246
|
+
root.find(j.ExportAllDeclaration),
|
|
247
|
+
];
|
|
248
|
+
for (const collection of staticDeclarations) {
|
|
249
|
+
collection.forEach((nodePath) => {
|
|
250
|
+
const declaration = nodePath.node;
|
|
251
|
+
const specifier = literalValue(declaration.source);
|
|
252
|
+
const entrypoint = specifier ? entrypointFor(specifier) : undefined;
|
|
253
|
+
if (!specifier || !entrypoint) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (!sourceRoot) {
|
|
257
|
+
warnUnsupported(file, specifier, entrypoint, 'the matching Ecopages source checkout was not found');
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const requested = requestedNames(nodePath.node);
|
|
261
|
+
if ('reason' in requested) {
|
|
262
|
+
warnUnsupported(file, specifier, entrypoint, requested.reason);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const availableNames = coreEntrypointNames(entrypoint, sourceRoot);
|
|
266
|
+
if (!availableNames) {
|
|
267
|
+
warnUnsupported(file, specifier, entrypoint, `the ${entrypoint} entry could not be inspected`);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const missing = requested.names.filter((name) => !availableNames.has(name));
|
|
271
|
+
if (missing.length > 0) {
|
|
272
|
+
warnUnsupported(file, specifier, entrypoint, `${missing.map((name) => `'${name}'`).join(', ')} ${missing.length === 1 ? 'is' : 'are'} not exported`);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
changed = rewriteLiteral(declaration.source) || changed;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
root.find(j.CallExpression).forEach((path) => {
|
|
279
|
+
const call = path.node;
|
|
280
|
+
if (!call.arguments?.length) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (isImportMetaResolve(call.callee) || isRequireResolve(call.callee)) {
|
|
284
|
+
changed = rewriteLiteral(call.arguments[0]) || changed;
|
|
285
|
+
}
|
|
286
|
+
else if (call.callee?.type === 'Import' ||
|
|
287
|
+
(call.callee?.type === 'Identifier' && call.callee.name === 'require')) {
|
|
288
|
+
warnUnverifiedDynamicAccess(call.arguments[0], file);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
root.find(j.ImportExpression).forEach((path) => {
|
|
292
|
+
warnUnverifiedDynamicAccess(path.node.source, file);
|
|
293
|
+
});
|
|
294
|
+
return changed;
|
|
295
|
+
}
|
|
296
|
+
export default function transformer(file, api, _options) {
|
|
297
|
+
const j = api.jscodeshift;
|
|
298
|
+
const root = j(file.source);
|
|
299
|
+
if (!rewriteModuleImports(root, j, file, findCoreSourceRoot())) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
return root.toSource(printOptions(file.source));
|
|
303
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codemod: migrate-data-hooks
|
|
3
|
+
*
|
|
4
|
+
* Renames the `eco.page()` data hooks and drops their envelopes.
|
|
5
|
+
*
|
|
6
|
+
* Before:
|
|
7
|
+
* eco.page({
|
|
8
|
+
* staticPaths: async () => ({ paths: [{ params: { slug: 'a' } }] }),
|
|
9
|
+
* staticProps: async ({ pathname: { params } }) => ({ props: { title: params.slug } }),
|
|
10
|
+
* render: ...
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* After:
|
|
14
|
+
* eco.page({
|
|
15
|
+
* paths: async () => [{ params: { slug: 'a' } }],
|
|
16
|
+
* props: async ({ params }) => ({ title: params.slug }),
|
|
17
|
+
* render: ...
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* A hook is rewritten only when every value it returns can be unwrapped: an object literal holding just the
|
|
21
|
+
* envelope key, or any expression inside an `async` function, which becomes `(await expr).props`. A hook it
|
|
22
|
+
* cannot rewrite is left unchanged and reported, so the old name still fails loudly with a pointer to the migration docs.
|
|
23
|
+
* The `GetStaticProps`, `GetStaticPaths` and `StaticPath` types are renamed to `GetProps`, `GetPaths` and `PagePath`.
|
|
24
|
+
*
|
|
25
|
+
* The props context passes `params` where it passed `pathname: { params }`, so in the `props` hook the first
|
|
26
|
+
* parameter is rewritten when it is destructured as `{ pathname: { params } }` or `{ pathname }`, or is a
|
|
27
|
+
* plain identifier read as `ctx.pathname.params`. A hook that reads `pathname` any other way, or declares a
|
|
28
|
+
* `params` binding already, keeps its `pathname` and is reported.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* This file is deliberately self-contained, with no import beyond `jscodeshift` types, because
|
|
32
|
+
* `@ecopages/codemod` is not published: users run it from its raw GitHub URL with `npx jscodeshift`.
|
|
33
|
+
*/
|
|
34
|
+
import type { API, FileInfo, Options } from 'jscodeshift';
|
|
35
|
+
export declare const parser = "tsx";
|
|
36
|
+
export default function transform(file: FileInfo, api: API, _options: Options): string | null;
|