mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
ae7bf74b3e
Node's ESM resolver does not consult NODE_PATH (unlike CJS require), so an ESM postcss.config.js shipped by a Hugo theme fails when loaded from the module cache: bare imports like `import x from "postcss-import"` have no node_modules to walk up to. Install a synchronous resolver hook (module.registerHooks) via --import=data:... on every Node invocation. On ERR_MODULE_NOT_FOUND for a bare specifier it resolves the package from each NODE_PATH entry via createRequire().resolve(). No-op for relative, absolute, URL-scheme and non-MODULE_NOT_FOUND failures. Synchronous hooks run on the main thread, so no --allow-worker is needed under the Node permission model. Fixes #13987 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
31 lines
1.1 KiB
Go
31 lines
1.1 KiB
Go
// Copyright 2026 The Hugo Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package hexec
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/base64"
|
|
"sync"
|
|
)
|
|
|
|
//go:embed esmloader.mjs
|
|
var esmLoaderSource string
|
|
|
|
// nodeESMLoaderImportArg returns a "--import=data:..." argument that installs
|
|
// a Node.js ESM resolver hook making NODE_PATH a fallback for failed bare
|
|
// imports. See esmloader.mjs for the rationale.
|
|
var nodeESMLoaderImportArg = sync.OnceValue(func() string {
|
|
return "--import=data:text/javascript;base64," + base64.StdEncoding.EncodeToString([]byte(esmLoaderSource))
|
|
})
|