adopt web extensions to webpack 5

This commit is contained in:
Martin Aeschlimann
2021-07-08 21:41:03 +02:00
parent a3e07245ee
commit 7ebe6a6054
51 changed files with 552 additions and 277 deletions

View File

@@ -16,12 +16,6 @@ const { NLSBundlePlugin } = require('vscode-nls-dev/lib/webpack-bundler');
const { DefinePlugin } = require('webpack');
function withNodeDefaults(/**@type WebpackConfig*/extConfig) {
// Need to find the top-most `package.json` file
const folderName = path.relative(__dirname, extConfig.context).split(/[\\\/]/)[0];
const pkgPath = path.join(__dirname, folderName, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const id = `${pkg.publisher}.${pkg.name}`;
/** @type WebpackConfig */
let defaultConfig = {
mode: 'none', // this leaves the source code as close as possible to the original (when packaging we set this to 'production')
@@ -70,19 +64,28 @@ function withNodeDefaults(/**@type WebpackConfig*/extConfig) {
},
// yes, really source maps
devtool: 'source-map',
plugins: [
new CopyWebpackPlugin({
patterns: [
{ from: 'src', to: '.', globOptions: { ignore: ['**/test/**', '**/*.ts'] }, noErrorOnMissing: true }
]
}),
new NLSBundlePlugin(id)
],
plugins: getDefaultNodePlugins(extConfig.context),
};
return merge(defaultConfig, extConfig);
}
function getDefaultNodePlugins(context) {
// Need to find the top-most `package.json` file
const folderName = path.relative(__dirname, context).split(/[\\\/]/)[0];
const pkgPath = path.join(__dirname, folderName, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const id = `${pkg.publisher}.${pkg.name}`;
return [
new CopyWebpackPlugin({
patterns: [
{ from: 'src', to: '.', globOptions: { ignore: ['**/test/**', '**/*.ts'] }, noErrorOnMissing: true }
]
}),
new NLSBundlePlugin(id)
];
}
function withBrowserDefaults(/**@type WebpackConfig*/extConfig) {
/** @type WebpackConfig */
@@ -93,8 +96,11 @@ function withBrowserDefaults(/**@type WebpackConfig*/extConfig) {
mainFields: ['module', 'main'],
extensions: ['.ts', '.js'], // support ts-files and js-files
alias: {
'vscode-nls': path.resolve(__dirname, '../build/polyfills/vscode-nls.js'),
'vscode-extension-telemetry': path.resolve(__dirname, '../build/polyfills/vscode-extension-telemetry.js')
},
fallback: {
'path': require.resolve('path-browserify'),
'util': require.resolve('util')
}
},
module: {
@@ -130,21 +136,31 @@ function withBrowserDefaults(/**@type WebpackConfig*/extConfig) {
},
// yes, really source maps
devtool: 'source-map',
plugins: [
new CopyWebpackPlugin({
patterns: [
{ from: 'src', to: '.', globOptions: { ignore: ['**/test/**', '**/*.ts'] }, noErrorOnMissing: true }
]
}),
new DefinePlugin({ WEBWORKER: JSON.stringify(true) })
]
plugins: getDefaultBrowserPlugins()
};
return merge(defaultConfig, extConfig);
}
function getDefaultBrowserPlugins() {
return [
new CopyWebpackPlugin({
patterns: [
{ from: 'src', to: '.', globOptions: { ignore: ['**/test/**', '**/*.ts'] }, noErrorOnMissing: true }
]
}),
new DefinePlugin({
'process.env': JSON.stringify({}),
'process.env.BROWSER_ENV': JSON.stringify('true')
})
];
}
module.exports = withNodeDefaults;
module.exports.node = withNodeDefaults;
module.exports.browser = withBrowserDefaults;
module.exports.getNodePlugins = getDefaultNodePlugins;
module.exports.getBrowserPlugins = getDefaultBrowserPlugins;