1
- const fs = require ( 'fs' ) ;
2
1
const path = require ( 'path' ) ;
3
- const jscodeshift = require ( 'jscodeshift' ) ;
4
2
import installPackage from './installPackage' ;
5
3
import logger from './logger' ;
6
- import chalk from 'chalk' ;
7
4
8
5
export async function addMacroSupport ( ) {
9
6
const packageJson = require ( path . join ( process . cwd ( ) , 'package.json' ) ) ;
@@ -15,96 +12,6 @@ export async function addMacroSupport() {
15
12
16
13
let isMacroPluginInstalled = await installPackage ( 'unplugin-parcel-macros' , { dev : true } ) ;
17
14
18
- return { isMacroPluginInstalled, isMacroSupportEnabled : false } ;
19
-
20
15
// TODO: Try to automatically update bundle config
21
-
22
- const configFiles = [ 'webpack' , 'next' , 'vite' , 'rollup' ] . flatMap ( bundler => [
23
- `${ bundler } .config.js` ,
24
- `${ bundler } .config.ts` ,
25
- `${ bundler } .config.mjs`
26
- ] ) ;
27
-
28
- // TODO: Detecting esbuild config is harder
29
-
30
- const configFile = configFiles . find ( file => fs . existsSync ( path . join ( process . cwd ( ) , file ) ) ) ;
31
-
32
- if ( ! configFile ) {
33
- logger . warn ( `Bundler config was not detected, so we couldn't update it automatically. Be sure to configure your bundler to support macros: ${ chalk . bold ( 'https://react-spectrum.corp.adobe.com/s2/#configuring-your-bundler' ) } ` ) ;
34
- return ;
35
- }
36
-
37
- const filePath = path . join ( process . cwd ( ) , configFile ) ;
38
- const source = fs . readFileSync ( filePath , 'utf8' ) ;
39
- const j = jscodeshift . withParser ( configFile . endsWith ( '.ts' ) ? 'ts' : 'babel' ) ;
40
- const root = j ( source ) ;
41
-
42
- const bundler = configFile . split ( '.' ) [ 0 ] ;
43
- const pluginName = `macros.${ bundler } ()` ;
44
-
45
- let modified = false ;
46
-
47
- if ( bundler === 'webpack' || bundler === 'vite' ) {
48
- root
49
- . find ( j . ArrayExpression )
50
- . filter ( path => path . parent . node . key && path . parent . node . key . name === 'plugins' )
51
- . forEach ( path => {
52
- path . node . elements . push ( j . callExpression ( j . identifier ( pluginName ) , [ ] ) ) ;
53
- modified = true ;
54
- } ) ;
55
- } else if ( bundler === 'rollup' ) {
56
- root
57
- . find ( j . ArrayExpression )
58
- . filter ( path => path . parent . node . key && path . parent . node . key . name === 'plugins' )
59
- . forEach ( path => {
60
- // Ensure macro plugin is placed before babel plugin
61
- const babelPluginIndex = path . node . elements . findIndex ( node => node . type === 'CallExpression' && node . callee . type === 'Identifier' && node . callee . name === 'babel' ) ;
62
- const newPlugin = j . callExpression ( j . identifier ( pluginName ) , [ ] ) ;
63
- if ( babelPluginIndex !== - 1 ) {
64
- path . node . elements . splice ( babelPluginIndex , 0 , newPlugin ) ;
65
- } else {
66
- path . node . elements . unshift ( newPlugin ) ;
67
- }
68
- modified = true ;
69
- } ) ;
70
- } else if ( bundler === 'next' ) {
71
- root
72
- . find ( j . ObjectExpression )
73
- . filter ( path => path . parent . node . type === 'AssignmentExpression' && path . parent . node . left . type === 'MemberExpression' && path . parent . node . left . property . name === 'webpack' )
74
- . forEach ( path => {
75
- const configParam = j . identifier ( 'config' ) ;
76
- const pushCall = j . callExpression (
77
- j . memberExpression ( j . memberExpression ( configParam , j . identifier ( 'plugins' ) ) , j . identifier ( 'push' ) ) ,
78
- [ j . identifier ( 'plugin' ) ]
79
- ) ;
80
- const returnStatement = j . returnStatement ( configParam ) ;
81
- path . node . body = j . blockStatement ( [ pushCall , returnStatement ] ) ;
82
- modified = true ;
83
- } ) ;
84
-
85
- // Add plugin instantiation
86
- root . get ( ) . node . program . body . unshift (
87
- j . variableDeclaration ( 'const' , [
88
- j . variableDeclarator (
89
- j . identifier ( 'plugin' ) ,
90
- j . callExpression ( j . identifier ( pluginName ) , [ ] )
91
- )
92
- ] )
93
- ) ;
94
- }
95
-
96
- if ( modified ) {
97
- // Add import statement
98
- root . get ( ) . node . program . body . unshift (
99
- j . importDeclaration (
100
- [ j . importDefaultSpecifier ( j . identifier ( 'macros' ) ) ] ,
101
- j . literal ( 'unplugin-parcel-macros' )
102
- )
103
- ) ;
104
-
105
- fs . writeFileSync ( filePath , root . toSource ( ) ) ;
106
- logger . success ( `Successfully updated ${ chalk . bold ( configFile ) } with the macros plugin.` ) ;
107
- } else {
108
- console . log ( `Could not automatically add the macro plugin to ${ chalk . bold ( configFile ) } . See the docs to try configuring it manually: ${ chalk . bold ( 'https://react-spectrum.corp.adobe.com/s2/#configuring-your-bundler' ) } ` ) ;
109
- }
16
+ return { isMacroPluginInstalled, isMacroSupportEnabled : false } ;
110
17
}
0 commit comments