From 1cb290ebed2ce8d797480923661b6e30aa7e703a Mon Sep 17 00:00:00 2001 From: L <6723574+louisgv@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:25:35 -0400 Subject: [PATCH] fix: resolver for relative, tilde, and absolute tilde (#880) * fix: ts resolver for relative files * fix tilde resolve * chore: update examples * done --- .vscode/settings.json | 1 + core/parcel-config/package.json | 2 +- core/parcel-resolver-post/package.json | 2 +- .../src/handle-ts-path.ts | 18 +++- core/parcel-resolver/package.json | 2 +- core/parcel-resolver/src/handle-tilde-src.ts | 19 +++- .../package.json | 2 +- .../src/index.ts | 4 +- core/parcel-transformer-manifest/package.json | 2 +- .../src/handle-background.ts | 5 +- core/parcel-transformer-manifest/src/index.ts | 2 + examples | 2 +- package.json | 2 +- pnpm-lock.yaml | 98 ++++++++++++++----- 14 files changed, 118 insertions(+), 43 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c1fb85a47..55394b44a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": true, "files.exclude": { "**/.git": true, "**/.svn": true, diff --git a/core/parcel-config/package.json b/core/parcel-config/package.json index 447e698fd..b604a1e57 100644 --- a/core/parcel-config/package.json +++ b/core/parcel-config/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/parcel-config", - "version": "0.40.1", + "version": "0.40.2", "license": "MIT", "repository": { "type": "git", diff --git a/core/parcel-resolver-post/package.json b/core/parcel-resolver-post/package.json index 0fe0092ed..d9b121926 100644 --- a/core/parcel-resolver-post/package.json +++ b/core/parcel-resolver-post/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/parcel-resolver-post", - "version": "0.4.2", + "version": "0.4.3", "description": "Plasmo Parcel Resolver Post-processing", "files": [ "dist" diff --git a/core/parcel-resolver-post/src/handle-ts-path.ts b/core/parcel-resolver-post/src/handle-ts-path.ts index 5bcf0e80a..03625a640 100644 --- a/core/parcel-resolver-post/src/handle-ts-path.ts +++ b/core/parcel-resolver-post/src/handle-ts-path.ts @@ -51,7 +51,7 @@ export async function handleTsPath( props: ResolverProps ): Promise { try { - const { dependency } = props + const { dependency, specifier } = props checkWebpackSpecificImportSyntax(dependency.specifier) @@ -61,6 +61,15 @@ export async function handleTsPath( return null } + if (specifier.startsWith(".")) { + return { + filePath: findModule( + resolve(dependency.resolveFrom, "..", specifier), + relevantExtList + ) + } + } + const compilerOptions = await getTsconfigCompilerOptions(props) if (compilerOptions.length === 0) { @@ -89,8 +98,10 @@ function loadTsPathsMap(tsConfigs: TSConfig[]) { return } - const tsPathsMap = new Map() - tsConfigs.forEach((tsConfig) => loadPathsFromTSConfig(tsConfig, tsPathsMap)) + const tsPathsMap = tsConfigs.reduce( + (c, tsConfig) => loadPathsFromTSConfig(tsConfig, c), + new Map() + ) state.pathsMap = tsPathsMap state.pathsMapRegex = Array.from(tsPathsMap.entries()).map((entry) => [ @@ -116,6 +127,7 @@ function loadPathsFromTSConfig( tsPaths[key].map((p) => join(tsConfigFolderPath, p)) ) } + return tsPathsMap } function attemptResolve({ specifier, dependency }: ResolverProps) { diff --git a/core/parcel-resolver/package.json b/core/parcel-resolver/package.json index 442ece3b4..5b7753b47 100644 --- a/core/parcel-resolver/package.json +++ b/core/parcel-resolver/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/parcel-resolver", - "version": "0.13.1", + "version": "0.13.2", "description": "Plasmo Parcel Resolver", "files": [ "dist" diff --git a/core/parcel-resolver/src/handle-tilde-src.ts b/core/parcel-resolver/src/handle-tilde-src.ts index 7cb1c560e..3d872128d 100644 --- a/core/parcel-resolver/src/handle-tilde-src.ts +++ b/core/parcel-resolver/src/handle-tilde-src.ts @@ -1,4 +1,4 @@ -import { extname, resolve } from "path" +import { extname, join, resolve } from "path" import { relevantExtensionList, @@ -9,6 +9,7 @@ import { } from "./shared" export async function handleTildeSrc({ + pipeline, specifier, dependency }: ResolverProps): Promise { @@ -17,10 +18,22 @@ export async function handleTildeSrc({ } const absoluteBaseFile = resolve( - process.env.PLASMO_SRC_DIR, - specifier.slice(1) + join(process.env.PLASMO_SRC_DIR, specifier.slice(1)) ) + if ( + pipeline === "data-text" || + pipeline === "data-base64" || + pipeline === "data-env" || + pipeline === "data-text-env" || + pipeline === "raw" || + pipeline === "raw-env" + ) { + return { + filePath: absoluteBaseFile + } + } + const importExt = extname(absoluteBaseFile) // TODO: Potentially resolve other type of files (less import etc...) that Parcel doesn't account for diff --git a/core/parcel-transformer-inline-css/package.json b/core/parcel-transformer-inline-css/package.json index d0e8195f9..2233b85ad 100644 --- a/core/parcel-transformer-inline-css/package.json +++ b/core/parcel-transformer-inline-css/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/parcel-transformer-inline-css", - "version": "0.3.10", + "version": "0.3.11", "description": "Plasmo Parcel Transformer for inline CSS", "files": [ "dist" diff --git a/core/parcel-transformer-inline-css/src/index.ts b/core/parcel-transformer-inline-css/src/index.ts index d59108804..a6b0b2138 100644 --- a/core/parcel-transformer-inline-css/src/index.ts +++ b/core/parcel-transformer-inline-css/src/index.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2023 Plasmo Corp. (https://www.plasmo.com) and contributors + * Copyright (c) 2024 Plasmo Corp. (https://www.plasmo.com) and contributors * MIT License * * Based on: https://github.com/parcel-bundler/parcel/blob/7023c08b7e99a9b8fd3c04995e4ef7ca92dee5c1/packages/transformers/css/src/CSSTransformer.js @@ -33,7 +33,7 @@ export default new Transformer({ sourceMap: !!asset.env.sourceMap }) - asset.setBuffer(res.code) + asset.setBuffer(Buffer.from(res.code)) if (res.dependencies) { for (let dep of res.dependencies) { diff --git a/core/parcel-transformer-manifest/package.json b/core/parcel-transformer-manifest/package.json index 11dce2d8c..ecb652494 100644 --- a/core/parcel-transformer-manifest/package.json +++ b/core/parcel-transformer-manifest/package.json @@ -1,6 +1,6 @@ { "name": "@plasmohq/parcel-transformer-manifest", - "version": "0.17.8", + "version": "0.17.9", "description": "Plasmo Parcel Transformer for Web Extension Manifest", "files": [ "dist", diff --git a/core/parcel-transformer-manifest/src/handle-background.ts b/core/parcel-transformer-manifest/src/handle-background.ts index 03e2e48cf..b496f0868 100644 --- a/core/parcel-transformer-manifest/src/handle-background.ts +++ b/core/parcel-transformer-manifest/src/handle-background.ts @@ -53,9 +53,9 @@ function handleFirefoxMV3Background(program: MV3Data) { function handleMV2BackgroundScript(program: MV2Data) { const { hot, asset } = getState() - vLog(`Handling background scripts`) if (program.background?.scripts) { + vLog(`Handling MV2 background scripts`) program.background.scripts = program.background.scripts.map((bgScript) => asset.addURLDependency(bgScript, { bundleBehavior: "isolated", @@ -79,10 +79,9 @@ function handleMV2BackgroundScript(program: MV2Data) { function handleMV3BackgroundServiceWorker(program: MV3Data) { const { hot, asset, filePath, ptrs } = getState() - vLog(`Handling background scripts`) if (program.background?.service_worker) { - vLog(`Handling background service worker`) + vLog(`Handling MV3 background service worker`) program.background.service_worker = asset.addURLDependency( program.background.service_worker, { diff --git a/core/parcel-transformer-manifest/src/index.ts b/core/parcel-transformer-manifest/src/index.ts index 098aefca0..716e987b5 100644 --- a/core/parcel-transformer-manifest/src/index.ts +++ b/core/parcel-transformer-manifest/src/index.ts @@ -110,6 +110,8 @@ export default new Transformer({ state.asset.setCode(JSON.stringify(data, null, 2)) state.asset.meta.webextEntry = true + + vLog("+ Finished transforming manifest") return state.getAssets() } }) diff --git a/examples b/examples index 09a4254a6..0b6be5d2b 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 09a4254a6241443ce3ac3ea60eb9c762dd7c3949 +Subproject commit 0b6be5d2b20321fb1cce5bf395b3489a933cc747 diff --git a/package.json b/package.json index fae2ce532..e6dd2a184 100644 --- a/package.json +++ b/package.json @@ -61,5 +61,5 @@ "npm": ">=9.5.0", "node": ">=18.0.0" }, - "packageManager": "pnpm@8.14.1" + "packageManager": "pnpm@8.15.0" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ad8d250ad..77e55d888 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1223,9 +1223,6 @@ importers: '@plasmohq/messaging': specifier: workspace:* version: link:../../api/messaging - '@types/tape': - specifier: 5.6.4 - version: 5.6.4 ajv: specifier: 8.12.0 version: 8.12.0 @@ -1244,25 +1241,28 @@ importers: devDependencies: '@ianvs/prettier-plugin-sort-imports': specifier: 4.1.1 - version: 4.1.1(prettier@3.0.3) + version: 4.1.1(prettier@3.2.4) '@types/chrome': - specifier: 0.0.251 - version: 0.0.251 + specifier: 0.0.258 + version: 0.0.258 '@types/node': - specifier: 20.9.0 - version: 20.9.0 + specifier: 20.11.5 + version: 20.11.5 '@types/react': - specifier: 18.2.37 - version: 18.2.37 + specifier: 18.2.48 + version: 18.2.48 '@types/react-dom': - specifier: 18.2.15 - version: 18.2.15 + specifier: 18.2.18 + version: 18.2.18 + '@types/tape': + specifier: 5.6.4 + version: 5.6.4 prettier: - specifier: 3.0.3 - version: 3.0.3 + specifier: 3.2.4 + version: 3.2.4 typescript: - specifier: 5.2.2 - version: 5.2.2 + specifier: 5.3.3 + version: 5.3.3 examples/with-content-script: dependencies: @@ -1913,9 +1913,6 @@ importers: examples/with-mantine: dependencies: - '@emotion/react': - specifier: 11.11.3 - version: 11.11.3(@types/react@18.2.48)(react@18.2.0) '@mantine/core': specifier: 7.4.2 version: 7.4.2(@mantine/hooks@7.4.2)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0) @@ -1950,6 +1947,15 @@ importers: '@types/react-dom': specifier: 18.2.18 version: 18.2.18 + postcss: + specifier: 8.4.33 + version: 8.4.33 + postcss-preset-mantine: + specifier: 1.12.3 + version: 1.12.3(postcss@8.4.33) + postcss-simple-vars: + specifier: 7.0.1 + version: 7.0.1(postcss@8.4.33) prettier: specifier: 3.2.4 version: 3.2.4 @@ -9699,10 +9705,10 @@ packages: /@types/prop-types@15.7.10: resolution: {integrity: sha512-mxSnDQxPqsZxmeShFH+uwQ4kO4gcJcGahjjMFeLbKE95IAZiiZyiEepGZjtXJ7hN/yfu0bu9xN2ajcU0JcxX6A==} + dev: true /@types/prop-types@15.7.11: resolution: {integrity: sha512-ga8y9v9uyeiLdpKddhxYQkxNDrfvuPrlFb0N1qnZZByvcElJaXthF1UhvCh9TLWJBEHeNtdnbysW7Y6Uq8CVng==} - dev: false /@types/pug@2.0.6: resolution: {integrity: sha512-SnHmG9wN1UVmagJOnyo/qkk0Z7gejYxOYYmaAwr5u2yFYfsupN3sg10kyzN8Hep/2zbHxCnsumxOoRIRMBwKCg==} @@ -9737,9 +9743,9 @@ packages: /@types/react@18.2.48: resolution: {integrity: sha512-qboRCl6Ie70DQQG9hhNREz81jqC1cs9EVNcjQ1AU+jH6NFfSAhVVbrrY/+nSF+Bsk4AOwm9Qa61InvMCyV+H3w==} dependencies: - '@types/prop-types': 15.7.10 + '@types/prop-types': 15.7.11 '@types/scheduler': 0.16.6 - csstype: 3.1.2 + csstype: 3.1.3 /@types/scheduler@0.16.6: resolution: {integrity: sha512-Vlktnchmkylvc9SnwwwozTv04L/e1NykF5vgoQ0XTmI8DD+wxfjQuHuvHS3p0r2jz2x2ghPs2h1FVeDirIteWA==} @@ -9764,12 +9770,13 @@ packages: dependencies: '@types/node': 20.11.5 '@types/through': 0.0.33 - dev: false + dev: true /@types/through@0.0.33: resolution: {integrity: sha512-HsJ+z3QuETzP3cswwtzt2vEIiHBk/dCcHGhbmG5X3ecnwFD/lPrMpliGXxSCg03L9AhrdwA4Oz/qfspkDW+xGQ==} dependencies: '@types/node': 20.11.5 + dev: true /@types/tough-cookie@4.0.5: resolution: {integrity: sha512-/Ad8+nIOV7Rl++6f1BdKxFSMgmoqEoYbHRpPcx3JEfv8VRsQe9Z4mCXeJBzxs7mbHY/XOZZuXlRNfhpVPbs6ZA==} @@ -9863,7 +9870,7 @@ packages: '@vue/shared': 3.3.8 estree-walker: 2.0.2 magic-string: 0.30.5 - postcss: 8.4.31 + postcss: 8.4.33 source-map-js: 1.0.2 /@vue/compiler-sfc@3.4.15: @@ -11462,7 +11469,6 @@ packages: /csstype@3.1.3: resolution: {integrity: sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==} - dev: false /data-urls@3.0.2: resolution: {integrity: sha512-Jy/tj3ldjZJo63sVAvg6LHt2mHvl4V6AgRAmNDtLdm7faqtsx+aJG42rsyCo9JCoRVKwPFzKlIPx3DIibwSIaQ==} @@ -15897,6 +15903,19 @@ packages: postcss: 8.4.33 yaml: 2.3.1 + /postcss-mixins@9.0.4(postcss@8.4.33): + resolution: {integrity: sha512-XVq5jwQJDRu5M1XGkdpgASqLk37OqkH4JCFDXl/Dn7janOJjCTEKL+36cnRVy7bMtoBzALfO7bV7nTIsFnUWLA==} + engines: {node: '>=14.0'} + peerDependencies: + postcss: ^8.2.14 + dependencies: + fast-glob: 3.3.2 + postcss: 8.4.33 + postcss-js: 4.0.1(postcss@8.4.33) + postcss-simple-vars: 7.0.1(postcss@8.4.33) + sugarss: 4.0.1(postcss@8.4.33) + dev: true + /postcss-nested@6.0.1(postcss@8.4.33): resolution: {integrity: sha512-mEp4xPMi5bSWiMbsgoPfcP74lsWLHkQbZc3sY+jWYd65CUwXrUaTp0fmNpa01ZcETKlIgUdFN/MpS2xZtqL9dQ==} engines: {node: '>=12.0'} @@ -15906,6 +15925,16 @@ packages: postcss: 8.4.33 postcss-selector-parser: 6.0.13 + /postcss-preset-mantine@1.12.3(postcss@8.4.33): + resolution: {integrity: sha512-cCwowf20mIyRXnV1cSVoMGfhYgy8ZqFJWsEJthdMZ3n7LijjucE9l/HO47gv5gAtr9nY1MkaEkpWS7ulhSTbSg==} + peerDependencies: + postcss: '>=8.0.0' + dependencies: + postcss: 8.4.33 + postcss-mixins: 9.0.4(postcss@8.4.33) + postcss-nested: 6.0.1(postcss@8.4.33) + dev: true + /postcss-selector-parser@6.0.13: resolution: {integrity: sha512-EaV1Gl4mUEV4ddhDnv/xtj7sxwrwxdetHdWUGnT4VJQf+4d05v6lHYZr8N573k5Z0BViss7BDhfWtKS3+sfAqQ==} engines: {node: '>=4'} @@ -15913,6 +15942,15 @@ packages: cssesc: 3.0.0 util-deprecate: 1.0.2 + /postcss-simple-vars@7.0.1(postcss@8.4.33): + resolution: {integrity: sha512-5GLLXaS8qmzHMOjVxqkk1TZPf1jMqesiI7qLhnlyERalG0sMbHIbJqrcnrpmZdKCLglHnRHoEBB61RtGTsj++A==} + engines: {node: '>=14.0'} + peerDependencies: + postcss: ^8.2.1 + dependencies: + postcss: 8.4.33 + dev: true + /postcss-value-parser@4.2.0: resolution: {integrity: sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==} @@ -15923,6 +15961,7 @@ packages: nanoid: 3.3.7 picocolors: 1.0.0 source-map-js: 1.0.2 + dev: false /postcss@8.4.33: resolution: {integrity: sha512-Kkpbhhdjw2qQs2O2DGX+8m5OVqEcbB9HRBvuYM9pgrjEFUg30A9LmXNlTAUj4S9kgtGyrMbTzVjH7E+s5Re2yg==} @@ -17957,6 +17996,15 @@ packages: resolution: {integrity: sha512-I5PWXAFKx3FYnI9a+dQMWNqTxoRt6vdBdb0O+BJ1sxXCWtSoQCusc13E58f+9p4MYx/qCnEMkD5jac6K2j3dgA==} dev: false + /sugarss@4.0.1(postcss@8.4.33): + resolution: {integrity: sha512-WCjS5NfuVJjkQzK10s8WOBY+hhDxxNt/N6ZaGwxFZ+wN3/lKKFSaaKUNecULcTTvE4urLcKaZFQD8vO0mOZujw==} + engines: {node: '>=12.0'} + peerDependencies: + postcss: ^8.3.3 + dependencies: + postcss: 8.4.33 + dev: true + /supports-color@5.5.0: resolution: {integrity: sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==} engines: {node: '>=4'}