import autoprefixer from 'autoprefixer'; export default class Environment { constructor(options) { this.development = true; this.production = false; this.server = false; this.autoprefixerTargets = [ 'chrome >= 58', ]; this.locales = ['en']; if (options !== undefined && options !== null) { this.input(options); } } input(options) { if (options) { switch (true) { case typeof (options) === 'string': // string this.inputString(options); break; case Array.isArray(options): // array options.forEach((arg) => { this.input(arg); }); break; default: // object Object.keys(options).forEach((k) => { this[k] = options[k] || this[k]; }); break; } } else if (process.env.NODE_ENV) { this.inputString(process.env.NODE_ENV); } } inputString(env) { switch (env.toLowerCase()) { case 'development': this.development = true; this.production = false; break; case 'production': this.development = false; this.production = true; break; case 'server': this.server = true; break; default: console.warn('Unknown environment:', env); break; } } styleLoaders(...preprocessingLoaders) { const { production, autoprefixerTargets, server, ExtractTextPlugin, // @HACK } = this; if (!ExtractTextPlugin) { throw new Error('Need a valid ExtractTextPlugin fed into the environment object.'); } let cssLoaders = [ { loader: 'style-loader', }, { loader: 'css-loader', options: { importLoaders: 1, sourceMap: true, modules: true, localIdentName: production ? '[name]__[local]--[hash:base64:5]' : '[name]__[local]--[hash:base64:5]', }, }, { loader: 'postcss-loader', options: { ident: 'postcss', plugins: [ autoprefixer({ browsers: autoprefixerTargets, grid: false, }), ], sourceMap: true, }, }, ].filter(loader => loader !== false); if (preprocessingLoaders && preprocessingLoaders.length > 0) { cssLoaders.push( { loader: 'resolve-url-loader', options: { fail: true, silent: false, }, }, ...preprocessingLoaders.map(loader => Object.assign({}, loader, { options: Object.assign({}, loader.options || {}, { sourceMap: true, }), })), ); } if (!server) { const fallback = cssLoaders.shift(); cssLoaders = ExtractTextPlugin.extract({ fallback, use: cssLoaders, }); } return cssLoaders; } }