11import { BabelLoaderTransformOptions , BabelPresetOptions } from 'babel-loader'
2- import * as webpack from 'webpack'
3- import Chunk = webpack . compilation . Chunk
4- import ChunkGroup = webpack . compilation . ChunkGroup
5- import Entrypoint = webpack . compilation . Entrypoint
6- import Module = webpack . compilation . Module
2+ import { ModuleGraph , ChunkGraph } from 'webpack'
3+
4+ import Entrypoint = require( 'webpack/lib/Entrypoint' )
5+ import Chunk = require( 'webpack/lib/Chunk' )
6+ import ChunkGroup = require( 'webpack/lib/ChunkGroup' )
7+ import Module = require( 'webpack/lib/Module' )
78
89import { BabelLoaderCacheDirectoryOption } from './babel.multi.target.options'
910import { BabelTargetOptions } from './babel.target.options'
11+ import { BabelTargetEntryDependency } from './babel.target.entry.dependency'
1012import { BrowserProfileName , StandardBrowserProfileName } from './browser.profile.name'
1113import { DEV_SERVER_CLIENT } from './constants'
1214import {
13- DEFAULT_BABEL_PLUGINS ,
1415 DEFAULT_BABEL_PRESET_OPTIONS ,
1516 DEFAULT_BROWSERS ,
1617 DEFAULT_TARGET_INFO ,
@@ -28,67 +29,6 @@ export type BabelTargetInfo = { [TOption in keyof BabelTargetOptions]: BabelTarg
2829 readonly options : BabelLoaderTransformOptions
2930}
3031
31- // webpack doesn't actually export things from the `compilation` namespace, so can only use them to
32- // type checking, not anything at runtime like instanceof
33- // so, need to do this instead
34- const SIG = {
35- module : [
36- 'disconnect' ,
37- 'unseal' ,
38- 'isEntryModule' ,
39- 'isInChunk' ,
40- ] ,
41- entrypoint : [
42- 'isInitial' ,
43- 'getFiles' ,
44- 'getRuntimeChunk' ,
45- 'setRuntimeChunk' ,
46- ] ,
47- chunk : [
48- 'hasRuntime' ,
49- 'canBeInitial' ,
50- 'isOnlyInitial' ,
51- 'hasEntryModule' ,
52- 'addModule' ,
53- 'removeModule' ,
54- 'setModules' ,
55- 'getNumberOfModules' ,
56- 'addGroup' ,
57- 'isInGroup' ,
58- 'canBeIntegrated' ,
59- ] ,
60- chunkGroup : [
61- 'unshiftChunk' ,
62- 'insertChunk' ,
63- 'pushChunk' ,
64- 'replaceChunk' ,
65- 'isInitial' ,
66- 'addChild' ,
67- 'getChildren' ,
68- 'getNumberOfChildren' ,
69- ] ,
70- }
71-
72- function hasSig ( obj : any , sig : string [ ] ) : boolean {
73- return sig . every ( name => typeof obj [ name ] === 'function' )
74- }
75-
76- function isModule ( obj : any ) : obj is Module {
77- return hasSig ( obj , SIG . module )
78- }
79-
80- function isEntrypoint ( obj : any ) : obj is Entrypoint {
81- return hasSig ( obj , SIG . entrypoint )
82- }
83-
84- function isChunkGroup ( obj : any ) : obj is ChunkGroup {
85- return hasSig ( obj , SIG . chunkGroup )
86- }
87-
88- function isChunk ( obj : any ) : obj is Chunk {
89- return hasSig ( obj , SIG . chunk )
90- }
91-
9232export class BabelTarget implements BabelTargetInfo {
9333
9434 public readonly profileName : BrowserProfileName
@@ -105,7 +45,11 @@ export class BabelTarget implements BabelTargetInfo {
10545 }
10646
10747 public getTargetedAssetName ( name : string ) : string {
108- return this . tagAssetsWithKey ? `${ name } .${ this . key } ` : name
48+ // sometimes a asset will be targeted twice
49+ if ( this . tagAssetsWithKey && ! name . endsWith ( `.${ this . key } ` ) ) {
50+ return `${ name } .${ this . key } `
51+ }
52+ return name
10953 }
11054
11155 public getTargetedRequest ( request : string ) : string {
@@ -136,21 +80,20 @@ export class BabelTarget implements BabelTargetInfo {
13680 return targets . find ( target => target . key === key )
13781 }
13882
139- public static getTargetFromModule ( module : Module ) : BabelTarget {
140- if ( module . options && module . options . babelTarget ) {
83+ public static getTargetFromModule ( module : Module , moduleGraph : ModuleGraph ) : BabelTarget {
84+ if ( module . options ? .babelTarget ) {
14185 return module . options . babelTarget
14286 }
14387
144- if ( ! module . reasons ) {
145- return undefined
146- }
88+ // https://github.com/webpack/webpack/pull/7826/commits/381e2db2009b0020de358c23c702d074806980a5
89+ const reasons = Array . from ( moduleGraph . getIncomingConnections ( module ) )
14790
148- for ( const reason of module . reasons ) {
149- if ( reason . dependency && reason . dependency . babelTarget ) {
91+ for ( const reason of reasons ) {
92+ if ( reason . dependency && reason . dependency instanceof BabelTargetEntryDependency ) {
15093 return reason . dependency . babelTarget
15194 }
152- if ( reason . module ) {
153- const target = BabelTarget . getTargetFromModule ( reason . module )
95+ if ( reason . module && reason . module !== module ) {
96+ const target = BabelTarget . getTargetFromModule ( reason . module , moduleGraph )
15497 if ( target ) {
15598 return target
15699 }
@@ -161,39 +104,41 @@ export class BabelTarget implements BabelTargetInfo {
161104
162105 }
163106
164- public static getTargetFromEntrypoint ( entrypoint : Entrypoint ) : BabelTarget {
165- if ( ! entrypoint . runtimeChunk . hasEntryModule ( ) ) {
107+ public static getTargetFromEntrypoint ( entrypoint : Entrypoint , moduleGraph : ModuleGraph , chunkGraph : ChunkGraph ) : BabelTarget {
108+ const runtime = entrypoint . getRuntimeChunk ( )
109+ const modules = Array . from ( chunkGraph . getChunkEntryModulesIterable ( runtime ) )
110+ if ( modules . length === 0 ) {
166111 return undefined
167112 }
168- return BabelTarget . getTargetFromModule ( entrypoint . runtimeChunk . entryModule )
113+ return BabelTarget . getTargetFromModule ( modules [ 0 ] , moduleGraph )
169114 }
170115
171116 // eslint-disable-next-line
172117 public static getTargetFromGroup ( group : ChunkGroup ) : BabelTarget {
173118 return undefined
174119 }
175120
176- public static getTargetFromChunk ( chunk : Chunk ) : BabelTarget {
177- if ( chunk . entryModule ) {
178- return BabelTarget . getTargetFromModule ( chunk . entryModule )
121+ public static getTargetFromChunk ( chunk : Chunk , moduleGraph : ModuleGraph , chunkGraph : ChunkGraph ) : BabelTarget {
122+ const modules = Array . from ( chunkGraph . getChunkEntryModulesIterable ( chunk ) )
123+ if ( modules . length === 0 ) {
124+ return undefined
179125 }
180-
181- return undefined
126+ return BabelTarget . getTargetFromModule ( modules [ 0 ] , moduleGraph )
182127 }
183128
184- public static findTarget ( source : BabelTargetSource ) : BabelTarget {
129+ public static findTarget ( source : BabelTargetSource , moduleGraph : ModuleGraph , chunkGraph : ChunkGraph ) : BabelTarget {
185130
186- if ( isModule ( source ) ) {
187- return BabelTarget . getTargetFromModule ( source )
131+ if ( source instanceof Module ) {
132+ return BabelTarget . getTargetFromModule ( source , moduleGraph )
188133 }
189- if ( isEntrypoint ( source ) ) {
190- return BabelTarget . getTargetFromEntrypoint ( source )
134+ if ( source instanceof Entrypoint ) {
135+ return BabelTarget . getTargetFromEntrypoint ( source , moduleGraph , chunkGraph )
191136 }
192- if ( isChunkGroup ( source ) ) {
137+ if ( source instanceof ChunkGroup ) {
193138 return BabelTarget . getTargetFromGroup ( source )
194139 }
195- if ( isChunk ( source ) ) {
196- return BabelTarget . getTargetFromChunk ( source )
140+ if ( source instanceof Chunk ) {
141+ return BabelTarget . getTargetFromChunk ( source , moduleGraph , chunkGraph )
197142 }
198143
199144 return undefined
@@ -228,18 +173,14 @@ export class BabelTargetFactory {
228173
229174 public createTransformOptions ( key : string , browsers : string [ ] , loaderOptions : { cacheDirectory ?: BabelLoaderCacheDirectoryOption } ) : BabelLoaderTransformOptions {
230175
231- const mergedPresetOptions : BabelPresetOptions = Object . assign (
232- { } ,
233- DEFAULT_BABEL_PRESET_OPTIONS ,
234- this . presetOptions ,
235- {
236- targets : {
237- browsers,
238- } ,
239- } , {
240- modules : false ,
176+ const mergedPresetOptions : BabelPresetOptions = {
177+ ...DEFAULT_BABEL_PRESET_OPTIONS ,
178+ ...this . presetOptions ,
179+ targets : {
180+ browsers,
241181 } ,
242- )
182+ modules : false ,
183+ }
243184
244185 const cacheDirectory = this . getCacheDirectory ( key , loaderOptions . cacheDirectory )
245186
@@ -249,7 +190,7 @@ export class BabelTargetFactory {
249190 ...this . presets ,
250191 ] ,
251192 plugins : [
252- ... DEFAULT_BABEL_PLUGINS ,
193+ // https://github.com/babel/babel/issues/10271#issuecomment-528379505
253194 ...this . plugins ,
254195 ] ,
255196 cacheDirectory,
0 commit comments