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

feat: prioritize local alias resolution above polyfill #414

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/create-plasmo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-plasmo",
"version": "0.63.0",
"version": "0.63.1",
"description": "Create Plasmo Framework Browser Extension",
"main": "dist/index.js",
"bin": "bin/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion cli/plasmo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plasmo",
"version": "0.63.0",
"version": "0.63.1",
"description": "The Plasmo Platform CLI",
"main": "dist/index.js",
"types": "dist/type.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/parcel-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@plasmohq/parcel-config",
"version": "0.30.1",
"version": "0.30.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/parcel-resolver/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import glob from "fast-glob"

const commonConfig = {
bundle: true,
minify: true,
// minify: true,

platform: "browser",
format: "cjs",
Expand Down
6 changes: 3 additions & 3 deletions packages/parcel-resolver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@plasmohq/parcel-resolver",
"version": "0.10.3",
"version": "0.11.0",
"description": "Plasmo Parcel Resolver",
"files": [
"dist"
Expand Down Expand Up @@ -29,8 +29,8 @@
"@plasmo/utils": "workspace:*",
"@plasmohq/rps": "workspace:*",
"assert": "2.0.0",
"browserify-zlib": "0.2.0",
"buffer": "6.0.3",
"browserify-zlib": "0.2.0",
"console-browserify": "1.2.0",
"constants-browserify": "1.0.0",
"crc-32": "1.2.2",
Expand All @@ -48,7 +48,6 @@
"stream-browserify": "3.0.0",
"stream-http": "3.2.0",
"string_decoder": "1.3.0",
"timers": "0.1.1",
"timers-browserify": "2.0.12",
"tsup": "6.5.0",
"tty-browserify": "0.0.1",
Expand All @@ -62,6 +61,7 @@
"@parcel/plugin": "2.8.3",
"@parcel/types": "2.8.3",
"fast-glob": "3.2.12",
"fs-extra": "11.1.0",
"got": "12.5.3"
}
}
26 changes: 26 additions & 0 deletions packages/parcel-resolver/src/handle-alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { resolve } from "path"

import { isFileOk } from "@plasmo/utils/fs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update


import { ResolverProps, ResolverResult, state } from "./shared"

export async function handleAlias({
specifier
}: ResolverProps): Promise<ResolverResult> {
if (!state.aliasMap.has(specifier)) {
return null
}

const absPath = resolve(state.aliasMap.get(specifier))

const hasLocalAlias = await isFileOk(absPath)

if (!hasLocalAlias) {
return null
}

return {
filePath: absPath,
priority: "sync"
}
}
4 changes: 3 additions & 1 deletion packages/parcel-resolver/src/handle-module-exports.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { ResolverProps, ResolverResult } from "./shared"

const knownEsmPackageSet = new Set(["firebase-admin"])

export async function handleModuleExport({
specifier,
dependency
}: ResolverProps): Promise<ResolverResult> {
try {
const segments = specifier.split("/")

if (segments.length > 2) {
if (segments.length > 2 || knownEsmPackageSet.has(segments[0])) {
const filePath = require.resolve(specifier, {
paths: [dependency.resolveFrom]
})
Expand Down
4 changes: 3 additions & 1 deletion packages/parcel-resolver/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Resolver } from "@parcel/plugin"

import { handleAbsoluteRoot } from "./handle-absolute-root"
import { handleAlias } from "./handle-alias"
import { handleModuleExport } from "./handle-module-exports"
import { handlePlasmoInternal } from "./handle-plasmo-internal"
import { handlePolyfill } from "./handle-polyfill"
import { handleRemoteCaching } from "./handle-remote-caching"
import { handleTildeSrc } from "./handle-tilde-src"
import { initializeState } from "./shared"
import { initializeState, state } from "./shared"

export default new Resolver({
async resolve(props) {
await initializeState(props)

return (
(await handleAlias(props)) ||
(await handlePlasmoInternal(props)) ||
(await handlePolyfill(props)) ||
(await handleRemoteCaching(props)) ||
Expand Down
12 changes: 11 additions & 1 deletion packages/parcel-resolver/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Resolver } from "@parcel/plugin"
import type { ResolveResult } from "@parcel/types"
import glob from "fast-glob"
import { statSync } from "fs"
import { readJson } from "fs-extra"
import type { Got } from "got"
import { join, resolve } from "path"

Expand All @@ -26,7 +27,8 @@ export type ResolverProps = Parameters<ResolveFx>[0]
export const state = {
got: null as Got,
dotPlasmoDirectory: null as string,
polyfillMap: null as Map<string, string>
polyfillMap: null as Map<string, string>,
aliasMap: null as Map<string, string>
}

export const initializeState = async (props: ResolverProps) => {
Expand Down Expand Up @@ -55,6 +57,14 @@ export const initializeState = async (props: ResolverProps) => {
])
)
}

if (!state.aliasMap) {
const packageJson = await readJson(
resolve(process.env.PLASMO_PROJECT_DIR, "package.json")
)

state.aliasMap = new Map(Object.entries(packageJson.alias || {}))
}
}

/**
Expand Down
11 changes: 2 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.