Skip to content

Commit

Permalink
Merge pull request #419 from CodinGame/fix-missing-dependencies
Browse files Browse the repository at this point in the history
Fix missing dependencies
  • Loading branch information
CGNonofr authored Apr 30, 2024
2 parents d104adf + 0b0e675 commit e31c9a0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
23 changes: 11 additions & 12 deletions rollup/rollup-metadata-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { OutputBundle, OutputChunk, OutputOptions, Plugin, PluginContext } from 'rollup'
import type { OutputBundle, OutputOptions, Plugin, PluginContext } from 'rollup'
import { builtinModules } from 'module'
import path from 'path'

interface Group {
name: string
Expand All @@ -22,7 +21,7 @@ interface GroupResult {
interface Options {
stage?: 'generateBundle' | 'writeBundle'
getGroup?: (entryPoint: string, options: OutputOptions) => { name: string, publicName?: string, priority?: number }
handle (this: PluginContext, group: GroupResult, moduleGroupName: Map<string, string | undefined>, options: OutputOptions, bundle: OutputBundle): void | Promise<void>
handle (this: PluginContext, group: GroupResult, moduleGroupName: Map<string, string | undefined>, otherDependencies: Set<string>, options: OutputOptions, bundle: OutputBundle): void | Promise<void>
}

export default ({ handle, getGroup = () => ({ name: 'main' }), stage = 'generateBundle' }: Options): Plugin => ({
Expand All @@ -31,13 +30,6 @@ export default ({ handle, getGroup = () => ({ name: 'main' }), stage = 'generate
const dependencyCache = new Map<string, Set<string>>()
const externalDependencyCache = new Map<string, Set<string>>()

const inputToOutput = Object.fromEntries(Object.values(bundle)
.filter((chunk): chunk is OutputChunk => 'code' in chunk)
.map(chunk => [
chunk.facadeModuleId,
path.resolve(options.dir!, chunk.preliminaryFileName)
]))

const moduleExternalDependencies = new Map<string, Set<string>>()
const getModuleDependencies = (id: string, paths: string[]): { internal: Set<string>, external: Set<string> } => {
if (paths.includes(id)) {
Expand All @@ -61,7 +53,7 @@ export default ({ handle, getGroup = () => ({ name: 'main' }), stage = 'generate
const dependencies = [...moduleInfo.importedIds, ...moduleInfo.dynamicallyImportedIds].map(depId => {
return getModuleDependencies(depId, [...paths, id])
})
dependencyCache.set(id, new Set([inputToOutput[id] ?? id, ...dependencies.flatMap(d => Array.from(d.internal))]))
dependencyCache.set(id, new Set([id, ...dependencies.flatMap(d => Array.from(d.internal))]))
externalDependencyCache.set(id, new Set(dependencies.flatMap(d => Array.from(d.external))))
}

Expand Down Expand Up @@ -137,8 +129,15 @@ export default ({ handle, getGroup = () => ({ name: 'main' }), stage = 'generate
}
})

const otherDependencies = new Set(Array.from(moduleExternalDependencies.values()).map(set => Array.from(set)).flat())
for (const group of groupResults) {
for (const directDependency of group.directDependencies) {
otherDependencies.delete(directDependency)
}
}

await Promise.all(groupResults.map(async (group) => {
await handle.call(this, group, moduleGroupName, options, bundle)
await handle.call(this, group, moduleGroupName, otherDependencies, options, bundle)
}))
}
})
7 changes: 4 additions & 3 deletions rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,10 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
priority: 1
}
},
async handle (group, moduleGroupName, options, bundle) {
async handle (group, moduleGroupName, otherDependencies, options, bundle) {
if (group.name === 'main') {
// Generate package.json
const dependencies = new Set([...group.directDependencies, ...otherDependencies])
const packageJson: PackageJson = {
...Object.fromEntries(Object.entries(pkg).filter(([key]) => ['name', 'description', 'version', 'keywords', 'author', 'license', 'repository', 'type'].includes(key))),
private: false,
Expand Down Expand Up @@ -802,8 +803,8 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
}
},
dependencies: {
...Object.fromEntries(Object.entries(pkg.dependencies).filter(([key]) => group.directDependencies.has(key))),
...Object.fromEntries(Array.from(group.directDependencies).filter(dep => dep.startsWith('@codingame/monaco-vscode-')).map(dep => [dep, pkg.version]))
...Object.fromEntries(Object.entries(pkg.dependencies).filter(([key]) => dependencies.has(key))),
...Object.fromEntries(Array.from(dependencies).filter(dep => dep.startsWith('@codingame/monaco-vscode-')).map(dep => [dep, pkg.version]))
}
}
this.emitFile({
Expand Down
4 changes: 2 additions & 2 deletions rollup/rollup.default-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default rollup.defineConfig([
}
}),
metadataPlugin({
handle ({ directDependencies }, moduleGroupName, options, bundle) {
handle ({ directDependencies }, moduleGroupName, otherDependencies, options, bundle) {
const entrypoint = Object.values(bundle).filter(v => (v as rollup.OutputChunk).isEntry)[0]!.fileName
const packageJson: PackageJson = {
name: `@codingame/monaco-vscode-${name}-default-extension`,
Expand Down Expand Up @@ -158,7 +158,7 @@ ${extensions.map(name => ` whenReady${pascalCase(name)}()`).join(',\n')}
}
},
metadataPlugin({
handle ({ directDependencies }, moduleGroupName, options, bundle) {
handle ({ directDependencies }, moduleGroupName, otherDependencies, options, bundle) {
const entrypoint = Object.values(bundle).filter(v => (v as rollup.OutputChunk).isEntry)[0]!.fileName
const packageJson: PackageJson = {
name,
Expand Down
4 changes: 2 additions & 2 deletions rollup/rollup.rollup-plugins.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const config: rollup.RollupOptions[] = [{
preferConst: false
}),
metadataPlugin({
handle (_, dependencies) {
handle ({ directDependencies }) {
const packageJson: PackageJson = {
name: `@codingame/monaco-vscode-${path.basename(output)}`,
...Object.fromEntries(Object.entries(pkg).filter(([key]) => ['version', 'keywords', 'author', 'license', 'repository', 'type'].includes(key))),
Expand All @@ -67,7 +67,7 @@ const config: rollup.RollupOptions[] = [{
types: `${path.basename(output)}.d.ts`,
dependencies: {
vscode: `npm:${pkg.name}@^${pkg.version}`,
...Object.fromEntries(Object.entries(pkg.dependencies).filter(([key]) => dependencies.has(key)))
...Object.fromEntries(Object.entries(pkg.dependencies).filter(([key]) => directDependencies.has(key)))
}
}
this.emitFile({
Expand Down
2 changes: 1 addition & 1 deletion rollup/rollup.types.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default rollup.defineConfig((<{input: Record<string, string>, output: str
priority: 1
}
},
async handle ({ name: groupName, exclusiveModules, entrypoints }, moduleGroupName, options, bundle) {
async handle ({ name: groupName, exclusiveModules, entrypoints }, moduleGroupName, otherDependencies, options, bundle) {
if (groupName === 'main') {
return
}
Expand Down

1 comment on commit e31c9a0

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.