Skip to content

Commit

Permalink
fix: ensure CSS modules attached to dynamic import modules are includ…
Browse files Browse the repository at this point in the history
…ed in build output

- add implementation for BabelTarget.getTargetFromGroup()
- add method to determine the original asset name from a targeted asset name

fixes #40
  • Loading branch information
DanielSchaffer committed Nov 1, 2019
1 parent 9d8d080 commit 0f85535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/babel-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export class BabelTarget implements BabelTargetInfo {
return this.tagAssetsWithKey ? `${name}.${this.key}` : name
}

public getUntargetedAssetName(targetedAssetName: string): string {
return this.tagAssetsWithKey ? targetedAssetName : targetedAssetName.replace(`.${this.key}`, '')
}

public getTargetedRequest(request: string): string {
const tag = `babel-target=${this.key}`
if (request.includes(tag)) {
Expand Down Expand Up @@ -165,8 +169,13 @@ export class BabelTarget implements BabelTargetInfo {
return BabelTarget.getTargetFromModule(entrypoint.runtimeChunk.entryModule)
}

// eslint-disable-next-line
public static getTargetFromGroup(group: ChunkGroup): BabelTarget {
for (const origin of group.origins) {
const target = BabelTarget.getTargetFromModule(origin.module)
if (target) {
return target
}
}
return undefined
}

Expand All @@ -175,6 +184,13 @@ export class BabelTarget implements BabelTargetInfo {
return BabelTarget.getTargetFromModule(chunk.entryModule)
}

for (const group of chunk.groupsIterable) {
const target = BabelTarget.getTargetFromGroup(group)
if (target) {
return target
}
}

return undefined
}

Expand Down
19 changes: 14 additions & 5 deletions src/normalize.css.chunks.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class NormalizeCssChunksPlugin implements Plugin {

// get the original (untagged) name of the entry module so we can correctly
// attribute any contained CSS modules to the entry
const name = this.findEntryName(chunk)
const name = this.findEntryName(chunk, target)

// track the original entry names to use later
if (!cssModules[name]) {
Expand Down Expand Up @@ -104,13 +104,22 @@ export class NormalizeCssChunksPlugin implements Plugin {
})
}

private findEntryName(chunk: Chunk): string {
private findEntryName(chunk: Chunk, target: BabelTarget): string {
const entry = this.findEntryModule(chunk)
if (entry) {
return entry.reasons[0].dependency.originalName
if (!entry) {
throw new Error(`Could not determine entry module for chunk ${chunk.name}`)
}
const originalName = entry.reasons[0].dependency.originalName
if (originalName) {
return originalName
}

throw new Error(`Could not determine entry module for chunk ${chunk.name}`)
if (target && chunk.name) {
// modules from dynamic imports don't get originalName, so just guess based on the targeted asset name
// TODO: can we override the default ContextElementDependency factory, similar to the entry dependencies?
// this could allow setting originalName, but could also cause blind targeting to be more complicated
return target.getUntargetedAssetName(chunk.name)
}
}

private findEntryModule(chunk: Chunk): Module {
Expand Down

0 comments on commit 0f85535

Please sign in to comment.