I have a script that adds methods to an object in order to keep everything in one place cleanly. This works well with the unminified js but once it gets minified it breaks and I don't why. Does anyone have experience with this, that can guide me to a solution? Why does my script throw 'Cannot redefine property: i' Error only after minifying?
Strategy.js
const Strategy = {};
Object.defineProperty(Strategy, 'map', { value: new Map() });
Object.defineProperty(Strategy, 'registerStrategy', {
value: function registerStrategy(fn) {
if (fn.name) {
this.map.set(fn.name, fn);
return Object.defineProperty(Strategy, fn.name, { value: fn });
}
return false;
},
});
export default Strategy;
strategy-1.js
import Strategy from '../strategy';
export function strategy1(param) {
return param + ' this is strategy 1.';
}
Strategy.registerStrategy(strategy1);
strategy-2.js
import Strategy from '../strategy';
export function strategy2(param) {
return param + ' this is strategy 2.';
}
Strategy.registerStrategy(strategy2);
webpack.config.js
import path from 'path';
const webpackConfig = {
module: {
rules: [
{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: {
presets: ['env'],
},
}],
},
],
},
mode: 'development',
watch,
devtool: 'source-map',
entry: './src/js/main.js',
output: {
filename: 'app.js',
path: path.resolve(__dirname, './dist/'),
},
};
export default webpackConfig;
Update based on answer from loganfsmyth
Strategy.js
const Strategy = {};
Object.defineProperty(Strategy, 'map', { value: new Map() });
Object.defineProperty(Strategy, 'registerStrategy', {
value: function registerStrategy(fnName, fn) {
if (fnName) {
this.map.set(fnName, fn);
return Object.defineProperty(Strategy, fnName, {
value: fn,
writable: true,
configurable: false,
});
}
return false;
},
});
Strategy.registerStrategies = (strats) => {
Object.keys(strats).forEach((name) => {
Strategy.registerStrategy(name, strats[name]);
});
};
export default Strategy;