1
- module . exports = function ( targetsBrowsersString ) {
1
+ const path = require ( 'path' ) ;
2
+
3
+ const webpack = require ( 'webpack' ) ;
4
+ const MemoryFileSystem = require ( 'memory-fs' )
5
+
6
+ /**
7
+ * here we extract plugin module names from @babel/preset-env/lib/available-plugins.js
8
+ * example:
9
+ * getPluginModuleName('bugfix/transform-v8-static-class-fields-redefine-readonly') -> '@babel/plugin-bugfix-v8-static-class-fields-redefine-readonly'
10
+ *
11
+ * if you find a better way to convert plugin name? to plugin module name
12
+ * -> https://github.com/FranckFreiburger/vue3-sfc-loader/issues/new?assignees=&labels=enhancement&projects=&template=feature_request.md&title=better+than+getPluginModuleName
13
+ */
14
+ const initGetPluginModuleName = new Promise ( ( resolve , reject ) => {
15
+
16
+ const compiler = webpack ( {
17
+ entry : path . join ( __dirname , '../node_modules/@babel/preset-env/lib/available-plugins.js' ) ,
18
+ output : {
19
+ path : '/' ,
20
+ filename : 'bundle.js' ,
21
+ library : {
22
+ type : 'commonjs' , // doc: https://webpack.js.org/configuration/output/#type-commonjs
23
+ }
24
+ } ,
25
+ plugins : [
26
+ new webpack . DefinePlugin ( {
27
+ 'require' : '__require' ,
28
+ } ) ,
29
+ ]
30
+ } ) ;
31
+
32
+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
33
+ compiler . run ( err => {
34
+
35
+ if ( err )
36
+ return reject ( err )
37
+
38
+ const script = compiler . outputFileSystem . readFileSync ( `${ compiler . options . output . path } /${ compiler . options . output . filename } ` ) . toString ( ) ;
39
+ const data = Function ( '__require' , 'exports = {};' + script + '; return exports.default' ) ( id => ( { default : id } ) ) ;
40
+ function getPluginModuleName ( id ) {
41
+
42
+ const tmp = data [ id ] ( ) ;
43
+ return tmp . default ?? data [ id ] ( ) ;
44
+ }
45
+ resolve ( getPluginModuleName ) ;
46
+ } ) ;
47
+
48
+ } )
49
+
50
+
51
+ module . exports = async function ( targetsBrowsersString ) {
2
52
3
53
const browserslist = require ( 'browserslist' )
4
54
const pluginsCompatData = require ( '@babel/preset-env/lib/plugins-compat-data' ) ;
@@ -7,6 +57,8 @@ module.exports = function(targetsBrowsersString) {
7
57
8
58
const browsers = browserslist ( targetsBrowsersString ) ;
9
59
60
+ const getPluginModuleName = await initGetPluginModuleName ;
61
+
10
62
// see @babel /preset-env/lib/index.js:307
11
- return [ ...filterItems ( pluginsCompatData . plugins , new Set ( ) , new Set ( ) , helperCompilationTargets ( { browsers } ) , [ ] ) ] ;
63
+ return [ ...filterItems ( pluginsCompatData . plugins , new Set ( ) , new Set ( ) , helperCompilationTargets ( { browsers } ) , [ ] ) ] . map ( getPluginModuleName ) ;
12
64
}
0 commit comments