diff --git a/packages/plugin-pha/package.json b/packages/plugin-pha/package.json index 8d307cdef..bf6878c5f 100644 --- a/packages/plugin-pha/package.json +++ b/packages/plugin-pha/package.json @@ -18,6 +18,7 @@ "@babel/types": "^7.18.6", "@babel/generator": "^7.18.7", "@babel/parser": "^7.18.6", + "@babel/traverse": "^7.18.6", "chalk": "^4.0.0", "consola": "^2.15.3", "humps": "^2.0.1", diff --git a/packages/plugin-pha/src/generateManifest.ts b/packages/plugin-pha/src/generateManifest.ts index 5141ecdb5..11431d41b 100644 --- a/packages/plugin-pha/src/generateManifest.ts +++ b/packages/plugin-pha/src/generateManifest.ts @@ -33,7 +33,7 @@ export async function compileEntires( compiler({ entry: path.join(rootDir, '.ice/routes-config.ts'), outfile: path.join(outputDir, 'routes-config.mjs'), - removeExportExprs: ['default', 'getData'], + removeCode: true, }), ]); } diff --git a/packages/plugin-pha/src/index.ts b/packages/plugin-pha/src/index.ts index 91d3aed4a..c02c7bd02 100644 --- a/packages/plugin-pha/src/index.ts +++ b/packages/plugin-pha/src/index.ts @@ -6,7 +6,7 @@ import type { Plugin } from '@ice/types'; import generateManifest from './generateManifest.js'; import createPHAMiddleware from './phaMiddleware.js'; import { templateFile } from './constants.js'; -import annotatePureCallsPlugin from './annotatePureCalls.js'; +import removeCodePlugin from './removeCodePlugin.js'; import type { Manifest } from './types.js'; @@ -14,7 +14,7 @@ export type Compiler = (options: { entry: string; outfile: string; timestamp?: boolean; - removeExportExprs?: string[]; + removeCode?: boolean; }) => Promise; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -42,14 +42,14 @@ const plugin: Plugin = ({ onGetConfig, onHook, context, generator urlPrefix = command === 'start' ? urls.lanUrlForTerminal : process.env.URL_PREFIX; compiler = async (options) => { - const { entry, outfile, removeExportExprs, timestamp = true } = options; + const { entry, outfile, removeCode, timestamp = true } = options; await serverCompiler({ entryPoints: [entry], format: 'esm', outfile, inject: [], - plugins: [annotatePureCallsPlugin()], - }, removeExportExprs ? { removeExportExprs } : null); + plugins: removeCode ? [removeCodePlugin()] : [], + }); return `${outfile}${timestamp ? `?version=${new Date().getTime()}` : ''}`; }; }); diff --git a/packages/plugin-pha/src/removeCodePlugin.ts b/packages/plugin-pha/src/removeCodePlugin.ts new file mode 100644 index 000000000..667c66cfc --- /dev/null +++ b/packages/plugin-pha/src/removeCodePlugin.ts @@ -0,0 +1,46 @@ +import * as fs from 'fs'; +import type { Plugin } from 'esbuild'; +import { parse, type ParserOptions } from '@babel/parser'; +import { default as traverse } from '@babel/traverse'; +import { default as generate } from '@babel/generator'; +import removeTopLevelCode from './removeTopLevelCode.js'; + +const removeCodePlugin = (): Plugin => { + const parserOptions: ParserOptions = { + sourceType: 'module', + plugins: [ + 'jsx', + 'importMeta', + 'topLevelAwait', + 'classProperties', + 'classPrivateMethods', + ], + }; + return { + name: 'esbuild-remove-top-level-code', + setup(build) { + build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async ({ path: id }) => { + // TODO: read route file from route-manifest + if (!id.includes('src/pages')) { + return; + } + const source = fs.readFileSync(id, 'utf-8'); + let isTS = false; + if (id.match(/\.(ts|tsx)$/)) { + isTS = true; + parserOptions.plugins.push('typescript', 'decorators-legacy'); + } + const ast = parse(source, parserOptions); + traverse(ast, removeTopLevelCode()); + const contents = generate(ast).code; + console.log('contents ==>', contents); + return { + contents, + loader: isTS ? 'tsx' : 'jsx', + }; + }); + }, + }; +}; + +export default removeCodePlugin; diff --git a/packages/plugin-pha/src/removeTopLevelCode.ts b/packages/plugin-pha/src/removeTopLevelCode.ts index adeb7b61d..a9ad5f0ed 100644 --- a/packages/plugin-pha/src/removeTopLevelCode.ts +++ b/packages/plugin-pha/src/removeTopLevelCode.ts @@ -27,7 +27,7 @@ const removeUnreferencedCode = (nodePath: NodePath) => { } }; -const removeTopLevelCodePlugin = () => { +const removeTopLevelCode = () => { return { ExportNamedDeclaration: { enter(nodePath: NodePath) { @@ -90,4 +90,4 @@ const removeTopLevelCodePlugin = () => { }; }; -export default removeTopLevelCodePlugin; +export default removeTopLevelCode; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e32bd6f97..e1995ca4e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -466,6 +466,7 @@ importers: specifiers: '@babel/generator': ^7.18.7 '@babel/parser': ^7.18.6 + '@babel/traverse': ^7.18.6 '@babel/types': ^7.18.6 '@ice/types': ^1.0.0 '@types/babel__generator': ^7.6.4 @@ -481,6 +482,7 @@ importers: dependencies: '@babel/generator': 7.18.7 '@babel/parser': 7.18.6 + '@babel/traverse': 7.18.6 '@babel/types': 7.18.6 chalk: 4.1.2 consola: 2.15.3 @@ -870,6 +872,13 @@ packages: '@babel/highlight': 7.17.9 dev: true + /@babel/code-frame/7.18.6: + resolution: {integrity: sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/highlight': 7.18.6 + dev: false + /@babel/compat-data/7.17.10: resolution: {integrity: sha512-GZt/TCsG70Ms19gfZO1tM4CVnXsPgEPBCpJu+Qz3L0LUDsY5nZqFZglIoPC1kIYOtNBZlrnFT+klg12vFGZXrw==} engines: {node: '>=6.9.0'} @@ -1069,6 +1078,11 @@ packages: engines: {node: '>=6.9.0'} dev: true + /@babel/helper-environment-visitor/7.18.6: + resolution: {integrity: sha512-8n6gSfn2baOY+qlp+VSzsosjCVGFqWKmDF0cCWOybh52Dw3SEyoWR1KrhMJASjLwIEkkAufZ0xvr+SxLHSpy2Q==} + engines: {node: '>=6.9.0'} + dev: false + /@babel/helper-explode-assignable-expression/7.16.7: resolution: {integrity: sha512-KyUenhWMC8VrxzkGP0Jizjo4/Zx+1nNZhgocs+gLzyZyB8SHidhoq9KK/8Ato4anhwsivfkBLftky7gvzbZMtQ==} engines: {node: '>=6.9.0'} @@ -1084,6 +1098,14 @@ packages: '@babel/types': 7.18.4 dev: true + /@babel/helper-function-name/7.18.6: + resolution: {integrity: sha512-0mWMxV1aC97dhjCah5U5Ua7668r5ZmSC2DLfH2EZnf9c3/dHZKiFa5pRLMH5tjSl471tY6496ZWk/kjNONBxhw==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/template': 7.18.6 + '@babel/types': 7.18.7 + dev: false + /@babel/helper-hoist-variables/7.16.7: resolution: {integrity: sha512-m04d/0Op34H5v7pbZw6pSKP7weA6lsMvfiIAMeIvkY/R4xQtBSMFEigu9QTZ2qB/9l22vsxtM8a+Q8CzD255fg==} engines: {node: '>=6.9.0'} @@ -1091,6 +1113,13 @@ packages: '@babel/types': 7.18.4 dev: true + /@babel/helper-hoist-variables/7.18.6: + resolution: {integrity: sha512-UlJQPkFqFULIcyW5sbzgbkxn2FKRgwWiRexcuaR8RNJRy8+LLveqPjwZV/bwrLZCN0eUHD/x8D0heK1ozuoo6Q==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/types': 7.18.7 + dev: false + /@babel/helper-member-expression-to-functions/7.17.7: resolution: {integrity: sha512-thxXgnQ8qQ11W2wVUObIqDL4p148VMxkt5T/qpN5k2fboRyzFGFmKsTGViquyM5QHKUy48OZoca8kw4ajaDPyw==} engines: {node: '>=6.9.0'} @@ -1205,6 +1234,13 @@ packages: '@babel/types': 7.18.4 dev: true + /@babel/helper-split-export-declaration/7.18.6: + resolution: {integrity: sha512-bde1etTx6ZyTmobl9LLMMQsaizFVZrquTEHOqKeQESMKo4PlObf+8+JA25ZsIpZhT/WEd39+vOdLXAFG/nELpA==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/types': 7.18.7 + dev: false + /@babel/helper-validator-identifier/7.16.7: resolution: {integrity: sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw==} engines: {node: '>=6.9.0'} @@ -1262,8 +1298,25 @@ packages: js-tokens: 4.0.0 dev: true - /@babel/parser/7.18.5: - resolution: {integrity: sha512-YZWVaglMiplo7v8f1oMQ5ZPQr0vn7HPeZXxXWsxXJRjGVrzUFn9OxFQl1sb5wzfootjA/yChhW84BV+383FSOw==} + /@babel/highlight/7.18.6: + resolution: {integrity: sha512-u7stbOuYjaPezCuLj29hNW1v64M2Md2qupEKP1fHc7WdOA3DgLh37suiSrZYY7haUB7iBeQZ9P1uiRF359do3g==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/helper-validator-identifier': 7.18.6 + chalk: 2.4.2 + js-tokens: 4.0.0 + dev: false + + /@babel/parser/7.17.9: + resolution: {integrity: sha512-vqUSBLP8dQHFPdPi9bc5GK9vRkYHJ49fsZdtoJ8EQ8ibpwk5rPKfvNIwChB0KVXcIjcepEBBd2VHC5r9Gy8ueg==} + engines: {node: '>=6.0.0'} + hasBin: true + dependencies: + '@babel/types': 7.17.0 + dev: true + + /@babel/parser/7.18.4: + resolution: {integrity: sha512-FDge0dFazETFcxGw/EXzOkN8uJp0PC7Qbm+Pe9T+av2zlBpOgunFHkQPPn+eRuClU73JF+98D531UgayY89tow==} engines: {node: '>=6.0.0'} hasBin: true dependencies: @@ -2305,6 +2358,15 @@ packages: '@babel/types': 7.17.0 dev: true + /@babel/template/7.18.6: + resolution: {integrity: sha512-JoDWzPe+wgBsTTgdnIma3iHNFC7YVJoPssVBDjiHfNlyt4YcunDtcDOUmfVDfCK5MfdsaIoX9PkijPhjH3nYUw==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/code-frame': 7.18.6 + '@babel/parser': 7.18.6 + '@babel/types': 7.18.7 + dev: false + /@babel/traverse/7.17.9: resolution: {integrity: sha512-PQO8sDIJ8SIwipTPiR71kJQCKQYB5NGImbOviK8K+kg5xkNSYXLBupuX9QhatFowrsvo9Hj8WgArg3W7ijNAQw==} engines: {node: '>=6.9.0'} @@ -2341,6 +2403,24 @@ packages: - supports-color dev: true + /@babel/traverse/7.18.6: + resolution: {integrity: sha512-zS/OKyqmD7lslOtFqbscH6gMLFYOfG1YPqCKfAW5KrTeolKqvB8UelR49Fpr6y93kYkW2Ik00mT1LOGiAGvizw==} + engines: {node: '>=6.9.0'} + dependencies: + '@babel/code-frame': 7.18.6 + '@babel/generator': 7.18.7 + '@babel/helper-environment-visitor': 7.18.6 + '@babel/helper-function-name': 7.18.6 + '@babel/helper-hoist-variables': 7.18.6 + '@babel/helper-split-export-declaration': 7.18.6 + '@babel/parser': 7.18.6 + '@babel/types': 7.18.7 + debug: 4.3.4 + globals: 11.12.0 + transitivePeerDependencies: + - supports-color + dev: false + /@babel/types/7.17.0: resolution: {integrity: sha512-TmKSNO4D5rzhL5bjWFcVHHLETzfQ/AmbKpKPOSjlP0WoHZ6L911fgoOKY4Alp/emzG4cHJdyN49zpgkbXFEHHw==} engines: {node: '>=6.9.0'} @@ -5328,7 +5408,6 @@ packages: engines: {node: '>=4'} dependencies: color-convert: 1.9.3 - dev: true /ansi-styles/4.3.0: resolution: {integrity: sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==} @@ -6055,7 +6134,6 @@ packages: ansi-styles: 3.2.1 escape-string-regexp: 1.0.5 supports-color: 5.5.0 - dev: true /chalk/4.1.2: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} @@ -6234,7 +6312,6 @@ packages: resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==} dependencies: color-name: 1.1.3 - dev: true /color-convert/2.0.1: resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==} @@ -6244,7 +6321,6 @@ packages: /color-name/1.1.3: resolution: {integrity: sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==} - dev: true /color-name/1.1.4: resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} @@ -8904,7 +8980,6 @@ packages: /globals/11.12.0: resolution: {integrity: sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==} engines: {node: '>=4'} - dev: true /globals/13.15.0: resolution: {integrity: sha512-bpzcOlgDhMG070Av0Vy5Owklpv1I6+j96GhUI7Rh7IzDCKLzboflLrrfqMu8NquDbiR4EOQk7XzJwqVJxicxog==} @@ -9057,7 +9132,6 @@ packages: /has-flag/3.0.0: resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==} engines: {node: '>=4'} - dev: true /has-flag/4.0.0: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} @@ -14657,7 +14731,6 @@ packages: engines: {node: '>=4'} dependencies: has-flag: 3.0.0 - dev: true /supports-color/7.2.0: resolution: {integrity: sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==}