Skip to content

Commit 673bbe3

Browse files
author
Franck Freiburger
committed
fix(build): 'bugfix/transform-v8-static-class-fields-redefine-readonly' is not a babel plugin module name
use in-memory webpack transform of "@babel/preset-env/lib/available-plugins.js" in order to extract real plugin module names
1 parent 440083a commit 673bbe3

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed
Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
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) {
252

353
const browserslist = require('browserslist')
454
const pluginsCompatData = require('@babel/preset-env/lib/plugins-compat-data');
@@ -7,6 +57,8 @@ module.exports = function(targetsBrowsersString) {
757

858
const browsers = browserslist(targetsBrowsersString);
959

60+
const getPluginModuleName = await initGetPluginModuleName;
61+
1062
// 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);
1264
}

build/webpack.config.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const caniuse = require('./caniuse-isSupported.js')
5757

5858
const pkg = require('../package.json');
5959

60-
const configure = ({name, vueTarget, libraryTargetModule}) => (env = {}, { mode = 'production', configName }) => {
60+
const configure = ({name, vueTarget, libraryTargetModule}) => async (env = {}, { mode = 'production', configName }) => {
6161
if (configName && !configName.includes(name)) {
6262
return {name}
6363
}
@@ -104,16 +104,17 @@ const configure = ({name, vueTarget, libraryTargetModule}) => (env = {}, { mode
104104
if ( browserslist(actualTargetsBrowsers).length === 0 )
105105
throw new RangeError('browserslist(' + actualTargetsBrowsers + ') selects no browsers');
106106

107-
let pluginNameList = requiredBabelPluginsNamesByBrowserTarget(actualTargetsBrowsers);
107+
let pluginNameList = await requiredBabelPluginsNamesByBrowserTarget(actualTargetsBrowsers);
108108
// now, exclude some plugins
109109
const excludeBabelPlugins = [
110-
'transform-unicode-sets-regex', // exclude because it indirectly imports regenerate-unicode-properties (about 500KB)
110+
'@babel/plugin-transform-unicode-sets-regex', // exclude because it indirectly imports regenerate-unicode-properties (about 500KB)
111111
]
112112

113113
pluginNameList = pluginNameList.filter(e => !excludeBabelPlugins.includes(e) );
114114
console.log('requiredBabelPluginsNamesByBrowserTarget', pluginNameList);
115115

116-
const ___targetBrowserBabelPlugins = '{' + pluginNameList.map(e => `'${ e }': require('@babel/plugin-${ e }'),\n`).join('') + '}';
116+
117+
const ___targetBrowserBabelPlugins = '{' + pluginNameList.map(e => `'${ e }': require('${ e }'),\n`).join('') + '}';
117118

118119
return {
119120
name,

0 commit comments

Comments
 (0)