-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bundling): load rollup config using the rollup version installed …
…in the workspace
- Loading branch information
Showing
2 changed files
with
30 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,20 @@ import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory'; | |
import { basename, dirname, join } from 'path'; | ||
import { existsSync, readdirSync } from 'fs'; | ||
import { | ||
type TargetConfiguration, | ||
type CreateDependencies, | ||
type CreateNodes, | ||
readJsonFile, | ||
writeJsonFile, | ||
detectPackageManager, | ||
CreateNodesContext, | ||
detectPackageManager, | ||
joinPathFragments, | ||
readJsonFile, | ||
type TargetConfiguration, | ||
writeJsonFile, | ||
} from '@nx/devkit'; | ||
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes'; | ||
import { getLockFileName } from '@nx/js'; | ||
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs'; | ||
import { type RollupOptions } from 'rollup'; | ||
|
||
// This import causes an error due to the module resolution used. If we switch to bundler or nodenext in the future we remove this ignore. | ||
// @ts-ignore | ||
import { loadConfigFile } from 'rollup/loadConfigFile'; | ||
|
||
const cachePath = join(projectGraphCacheDirectory, 'rollup.hash'); | ||
const targetsCache = readTargetsCache(); | ||
|
||
|
@@ -91,6 +87,28 @@ async function buildRollupTarget( | |
options: RollupPluginOptions, | ||
context: CreateNodesContext | ||
): Promise<Record<string, TargetConfiguration>> { | ||
let loadConfigFile: ( | ||
path: string, | ||
commandOptions: unknown, | ||
watchMode: boolean | ||
) => Promise<{ options: RollupOptions[] }>; | ||
|
||
try { | ||
// Try to load the workspace version of rollup first (it should already exist). | ||
// Using the workspace rollup ensures that the config file is compatible with the `loadConfigFile` function. | ||
// e.g. rollup@2 supports having `require` calls in rollup config, but rollup@4 does not. | ||
const m = require(require.resolve('rollup/loadConfigFile', { | ||
paths: [dirname(configFilePath)], | ||
})); | ||
// Rollup 2 has this has default export, but it is named in 3 and 4. | ||
// See: https://www.unpkg.com/browse/[email protected]/dist/loadConfigFile.js | ||
loadConfigFile = typeof m === 'function' ? m : m.loadConfigFile; | ||
} catch { | ||
console.log('loadConfigFile 2'); | ||
// Fallback to our own if needed. | ||
loadConfigFile = require('rollup/loadConfigFile').loadConfigFile; | ||
} | ||
|
||
const namedInputs = getNamedInputs(projectRoot, context); | ||
const rollupConfig = ( | ||
(await loadConfigFile( | ||
|