feat!: upgrade webpack to 5 and add TS support

Breaking flag is because it may have some differences in the way the library is exported -
`module.exports = ` vs `module.exports.default = `. That would depend on the Webpack config,
so it should continue working, but just to be safe.
This commit is contained in:
Georgi Angelov 2024-10-10 11:45:50 +03:00
parent f4e7e908f5
commit 3d0b429526
30 changed files with 4507 additions and 5190 deletions

View file

@ -1,80 +1,74 @@
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const base = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
devtool: 'cheap-module-source-map',
module: {
rules: [
{
include: [
path.resolve('src')
],
test: /\.js$/,
loader: 'babel-loader',
options: {
plugins: [
'@babel/plugin-transform-runtime'
],
presets: [
['@babel/preset-env', {targets: {browsers: ['last 3 versions', 'Safari >= 8', 'iOS >= 8']}}]
],
// Consider a file a "module" if import/export statements are present, or else consider it a
// "script". Fixes "Cannot assign to read only property 'exports'" when using
// @babel/plugin-transform-runtime with CommonJS files.
sourceType: 'unambiguous'
}
},
{
test: /\.(png|svg|wav)$/,
loader: 'arraybuffer-loader'
const ScratchWebpackConfigBuilder = require('scratch-webpack-configuration');
const baseConfig = new ScratchWebpackConfigBuilder(
{
rootPath: path.resolve(__dirname),
enableReact: false,
enableTs: true,
shouldSplitChunks: false
})
.setTarget('browserslist')
.merge({
resolve: {
fallback: {
Buffer: require.resolve('buffer/')
}
]
},
optimization: {
minimizer: [
new UglifyJsPlugin({
include: /\.min\.js$/,
sourceMap: true
})
]
},
plugins: []
};
}
});
module.exports = [
// Web-compatible
Object.assign({}, base, {
target: 'web',
entry: {
'scratch-storage': './src/index.js',
'scratch-storage.min': './src/index.js'
},
if (!process.env.CI) {
baseConfig.addPlugin(new webpack.ProgressPlugin());
}
// Web-compatible
const webConfig = baseConfig.clone()
.merge({
output: {
library: 'ScratchStorage',
libraryTarget: 'umd',
path: path.resolve('dist', 'web'),
filename: '[name].js'
path: path.resolve(__dirname, 'dist', 'web'),
filename: '[name].js',
clean: false
}
}),
});
// Node-compatible
Object.assign({}, base, {
target: 'node',
const webNonMinConfig = webConfig.clone()
.merge({
entry: {
'scratch-storage': './src/index.js'
'scratch-storage': path.join(__dirname, './src/index.ts')
},
optimization: {
minimize: false
}
});
const webMinConfig = webConfig.clone()
.merge({
entry: {
'scratch-storage.min': path.join(__dirname, './src/index.ts')
},
optimization: {
minimize: true
}
});
// Node-compatible
const nodeConfig = baseConfig.clone()
.merge({
entry: {
'scratch-storage': path.join(__dirname, './src/index.ts')
},
output: {
library: 'ScratchStorage',
libraryTarget: 'commonjs2',
path: path.resolve('dist', 'node'),
filename: '[name].js'
},
externals: {
'base64-js': true,
'js-md5': true,
'localforage': true,
'text-encoding': true
path: path.resolve(__dirname, 'dist', 'node'),
filename: '[name].js',
clean: false
}
})
];
.addExternals(['base64-js', 'js-md5', 'localforage', 'text-encoding']);
module.exports = [webNonMinConfig.get(), webMinConfig.get(), nodeConfig.get()];