@@ -2,14 +2,16 @@ use anyhow::Result;
2
2
use turbo_rcstr:: rcstr;
3
3
use turbo_tasks:: { ResolvedVc , Vc } ;
4
4
use turbo_tasks_fs:: { self , FileSystemEntryType , FileSystemPath } ;
5
- use turbopack:: module_options:: { LoaderRuleItem , OptionWebpackRules , WebpackRules } ;
5
+ use turbopack:: module_options:: { LoaderRuleItem , WebpackRules } ;
6
6
use turbopack_core:: {
7
7
issue:: { Issue , IssueExt , IssueSeverity , IssueStage , OptionStyledString , StyledString } ,
8
8
reference_type:: { CommonJsReferenceSubType , ReferenceType } ,
9
9
resolve:: { node:: node_cjs_resolve_options, parse:: Request , pattern:: Pattern , resolve} ,
10
10
} ;
11
11
use turbopack_node:: transforms:: webpack:: WebpackLoaderItem ;
12
12
13
+ // https://babeljs.io/docs/config-files
14
+ // TODO: Also support a `babel` key in a package.json file
13
15
const BABEL_CONFIG_FILES : & [ & str ] = & [
14
16
".babelrc" ,
15
17
".babelrc.json" ,
@@ -22,94 +24,59 @@ const BABEL_CONFIG_FILES: &[&str] = &[
22
24
"babel.config.cjs" ,
23
25
] ;
24
26
25
- /// If the user has a babel configuration file (see list above) alongside their
26
- /// `next.config.js` configuration, automatically add `babel-loader` as a
27
- /// webpack loader for each eligible file type if it doesn't already exist.
27
+ /// If the user has a babel configuration file (see list above) alongside their `next.config.js`
28
+ /// configuration, automatically add `babel-loader` as a webpack loader for each eligible file type
29
+ /// if it doesn't already exist.
28
30
#[ turbo_tasks:: function]
29
31
pub async fn maybe_add_babel_loader (
30
32
project_root : FileSystemPath ,
31
- webpack_rules : Option < ResolvedVc < WebpackRules > > ,
32
- ) -> Result < Vc < OptionWebpackRules > > {
33
- let has_babel_config = {
34
- let mut has_babel_config = false ;
35
- for & filename in BABEL_CONFIG_FILES {
36
- let filetype = * project_root. join ( filename) ?. get_type ( ) . await ?;
37
- if matches ! ( filetype, FileSystemEntryType :: File ) {
38
- has_babel_config = true ;
39
- break ;
40
- }
33
+ webpack_rules : Vc < WebpackRules > ,
34
+ ) -> Result < Vc < WebpackRules > > {
35
+ let mut has_babel_config = false ;
36
+ for & filename in BABEL_CONFIG_FILES {
37
+ let filetype = * project_root. join ( filename) ?. get_type ( ) . await ?;
38
+ if matches ! ( filetype, FileSystemEntryType :: File ) {
39
+ has_babel_config = true ;
40
+ break ;
41
41
}
42
- has_babel_config
43
- } ;
44
-
45
- if has_babel_config {
46
- let mut rules = if let Some ( webpack_rules) = webpack_rules {
47
- webpack_rules. owned ( ) . await ?
48
- } else {
49
- Default :: default ( )
50
- } ;
51
- let mut has_emitted_babel_resolve_issue = false ;
52
- let mut has_changed = false ;
53
- for pattern in [ "*.js" , "*.jsx" , "*.ts" , "*.tsx" , "*.cjs" , "*.mjs" ] {
54
- let rule = rules. get_mut ( pattern) ;
55
- let has_babel_loader = if let Some ( rule) = rule. as_ref ( ) {
56
- rule. loaders
57
- . await ?
58
- . iter ( )
59
- . any ( |c| c. loader == "babel-loader" )
60
- } else {
61
- false
62
- } ;
63
-
64
- if !has_babel_loader {
65
- if !has_emitted_babel_resolve_issue
66
- && !* is_babel_loader_available ( project_root. clone ( ) ) . await ?
67
- {
68
- BabelIssue {
69
- path : project_root. clone ( ) ,
70
- title : StyledString :: Text ( rcstr ! (
71
- "Unable to resolve babel-loader, but a babel config is present"
72
- ) )
73
- . resolved_cell ( ) ,
74
- description : StyledString :: Text ( rcstr ! (
75
- "Make sure babel-loader is installed via your package manager."
76
- ) )
77
- . resolved_cell ( ) ,
78
- severity : IssueSeverity :: Fatal ,
79
- }
80
- . resolved_cell ( )
81
- . emit ( ) ;
82
-
83
- has_emitted_babel_resolve_issue = true ;
84
- }
42
+ }
43
+ if !has_babel_config {
44
+ return Ok ( webpack_rules) ;
45
+ }
85
46
86
- let loader = WebpackLoaderItem {
87
- loader : rcstr ! ( "babel-loader" ) ,
88
- options : Default :: default ( ) ,
89
- } ;
90
- if let Some ( rule) = rule {
91
- let mut loaders = rule. loaders . owned ( ) . await ?;
92
- loaders. push ( loader) ;
93
- rule. loaders = ResolvedVc :: cell ( loaders) ;
94
- } else {
95
- rules. insert (
96
- pattern. into ( ) ,
97
- LoaderRuleItem {
98
- loaders : ResolvedVc :: cell ( vec ! [ loader] ) ,
99
- rename_as : Some ( rcstr ! ( "*" ) ) ,
100
- condition : None ,
101
- } ,
102
- ) ;
103
- }
104
- has_changed = true ;
105
- }
106
- }
47
+ let mut rules = webpack_rules. owned ( ) . await ?;
48
+ let pattern = rcstr ! ( "*.{js,jsx,ts,tsx,cjs,mjs,mts,cts}" ) ;
107
49
108
- if has_changed {
109
- return Ok ( Vc :: cell ( Some ( ResolvedVc :: cell ( rules) ) ) ) ;
50
+ if !* is_babel_loader_available ( project_root. clone ( ) ) . await ? {
51
+ BabelIssue {
52
+ path : project_root. clone ( ) ,
53
+ title : StyledString :: Text ( rcstr ! (
54
+ "Unable to resolve babel-loader, but a babel config is present"
55
+ ) )
56
+ . resolved_cell ( ) ,
57
+ description : StyledString :: Text ( rcstr ! (
58
+ "Make sure babel-loader is installed via your package manager."
59
+ ) )
60
+ . resolved_cell ( ) ,
61
+ severity : IssueSeverity :: Fatal ,
110
62
}
63
+ . resolved_cell ( )
64
+ . emit ( ) ;
111
65
}
112
- Ok ( Vc :: cell ( webpack_rules) )
66
+
67
+ rules. push ( (
68
+ pattern,
69
+ LoaderRuleItem {
70
+ loaders : ResolvedVc :: cell ( vec ! [ WebpackLoaderItem {
71
+ loader: rcstr!( "babel-loader" ) ,
72
+ options: Default :: default ( ) ,
73
+ } ] ) ,
74
+ rename_as : Some ( rcstr ! ( "*" ) ) ,
75
+ condition : None ,
76
+ } ,
77
+ ) ) ;
78
+
79
+ Ok ( Vc :: cell ( rules) )
113
80
}
114
81
115
82
#[ turbo_tasks:: function]
0 commit comments