fix pnp config module resolution

master
Brad Cornes 2020-12-14 13:59:22 +00:00
parent 4b10581005
commit e963d68f42
2 changed files with 31 additions and 32 deletions

View File

@ -1,13 +1,27 @@
import * as path from 'path' import * as path from 'path'
import Module from 'module'
import findUp from 'find-up' import findUp from 'find-up'
import resolveFrom from 'resolve-from' import resolveFrom from 'resolve-from'
import importFrom from 'import-from' import importFrom from 'import-from'
let isPnp
let pnpApi
export function withUserEnvironment(base, root, cb) { export function withUserEnvironment(base, root, cb) {
if (isPnp === true) {
return withPnpEnvironment(base, cb)
}
if (isPnp === false) {
return withNonPnpEnvironment(base, cb)
}
const pnpPath = findUp.sync( const pnpPath = findUp.sync(
(dir) => { (dir) => {
const pnpFile = path.join(dir, '.pnp.js') let pnpFile = path.join(dir, '.pnp.js')
if (findUp.sync.exists(pnpFile)) {
return pnpFile
}
pnpFile = path.join(dir, '.pnp.cjs')
if (findUp.sync.exists(pnpFile)) { if (findUp.sync.exists(pnpFile)) {
return pnpFile return pnpFile
} }
@ -17,62 +31,47 @@ export function withUserEnvironment(base, root, cb) {
}, },
{ cwd: base } { cwd: base }
) )
if (pnpPath) { if (pnpPath) {
return withPnpEnvironment(pnpPath, cb) isPnp = true
pnpApi = __non_webpack_require__(pnpPath)
pnpApi.setup()
} else {
isPnp = false
} }
return withNonPnpEnvironment(base, cb)
return withUserEnvironment(base, root, cb)
} }
function withPnpEnvironment(pnpPath, cb) { function withPnpEnvironment(base, cb) {
const basePath = path.dirname(pnpPath) const pnpResolve = (request, from = base) => {
return pnpApi.resolveRequest(request, from.replace(/\/$/, '') + '/')
// pnp will patch `module` and `fs` to load package in pnp environment
// backup the functions which will be patched here
const originalModule = Object.create(null)
originalModule._load = Module._load
originalModule._resolveFilename = Module._resolveFilename
originalModule._findPath = Module._findPath
const pnpapi = __non_webpack_require__(pnpPath)
// get into pnp environment
pnpapi.setup()
// restore the patched function, we can not load any package after called this
const restore = () => Object.assign(Module, originalModule)
const pnpResolve = (request, from = basePath) => {
return pnpapi.resolveRequest(request, from + '/')
} }
const pnpRequire = (request, from) => { const pnpRequire = (request, from) => {
return __non_webpack_require__(pnpResolve(request, from)) return __non_webpack_require__(pnpResolve(request, from))
} }
const res = cb({ isPnP: true, resolve: pnpResolve, require: pnpRequire }) const res = cb({ isPnp: true, resolve: pnpResolve, require: pnpRequire })
// check if it return a thenable // check if it return a thenable
if (res != null && res.then) { if (res != null && res.then) {
return res.then( return res.then(
(x) => { (x) => {
restore()
return x return x
}, },
(err) => { (err) => {
restore()
throw err throw err
} }
) )
} }
restore()
return res return res
} }
function withNonPnpEnvironment(base, cb) { function withNonPnpEnvironment(base, cb) {
return cb({ return cb({
isPnP: false, isPnp: false,
require(request, from = base) { require(request, from = base) {
return importFrom(from, request) return importFrom(from, request)
}, },

View File

@ -259,7 +259,7 @@ function withPackages(configDir, root, cb) {
return withUserEnvironment( return withUserEnvironment(
configDir, configDir,
root, root,
async ({ isPnP, require, resolve }) => { async ({ isPnp, require, resolve }) => {
const tailwindBase = path.dirname(resolve('tailwindcss/package.json')) const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
const postcss = require('postcss', tailwindBase) const postcss = require('postcss', tailwindBase)
const tailwindcss = require('tailwindcss') const tailwindcss = require('tailwindcss')
@ -274,7 +274,7 @@ function withPackages(configDir, root, cb) {
), ),
tailwindBase tailwindBase
) )
if (isPnP) { if (isPnp) {
browserslistCommand = 'yarn' browserslistCommand = 'yarn'
browserslistArgs = ['node', browserslistBin] browserslistArgs = ['node', browserslistBin]
} else { } else {