|
const ExtractTextPlugin = require('extract-text-webpack-plugin'); |
|
const autoprefixer = require('autoprefixer'); |
|
const CleanWebpackPlugin = require('clean-webpack-plugin'); |
|
|
|
const imageNamePattern = '[name].[hash:8].[ext]'; |
|
|
|
module.exports = { |
|
assetPrefix: process.env.CDN_HOST || '', |
|
poweredByHeader: false, |
|
exportPathMap: () => ({}), |
|
webpack: (config, { buildId, dev }) => { |
|
const id = dev ? 'local' : buildId; |
|
const cdnHost = process.env.CDN_HOST || ''; |
|
|
|
function getRelativeBuildOutputPath(partPath) { |
|
const absoluteOutputPath = path.resolve(__dirname, `dist/static/${id}/${partPath}`); |
|
return path.relative(config.output.path, absoluteOutputPath); |
|
} |
|
|
|
function prefixWithCDN(partPath) { |
|
return `${cdnHost}${partPath}`; |
|
} |
|
|
|
const modifiedConfig = { |
|
...config, |
|
module: { |
|
rules: [ |
|
...config.module.rules.map((loader) => { |
|
// We only want to modify the babel-loaders so if leave |
|
// all other loaders untouched. |
|
if (loader.loader !== 'babel-loader') { |
|
return loader; |
|
} |
|
|
|
return { |
|
...loader, |
|
options: { |
|
...loader.options, |
|
cacheDirectory: false, |
|
plugins: [ |
|
...loader.options.plugins || [], |
|
[ |
|
'file-loader', |
|
{ |
|
name: imageNamePattern, |
|
extensions: ['png', 'jpg', 'gif', 'gif', 'svg'], |
|
publicPath: prefixWithCDN(`/static/${id}/assets`), |
|
// Why is this conditional needed? |
|
outputPath: dev ? `./app/dist/static/${id}/assets` : getRelativeBuildOutputPath('assets/'), |
|
}, |
|
], |
|
], |
|
}, |
|
}; |
|
}), |
|
|
|
{ |
|
test: /\.scss$/, |
|
loader: 'emit-file-loader', |
|
options: { |
|
name: 'dist/[path][name].[ext]', |
|
}, |
|
}, |
|
|
|
{ |
|
test: /\.scss$/, |
|
use: ExtractTextPlugin.extract({ |
|
use: [ |
|
{ |
|
loader: 'css-loader', |
|
options: { |
|
localIdentName: dev |
|
? '[folder]__[local]-[hash:8]' |
|
: '[hash:16]', |
|
modules: true, |
|
alias: { |
|
'./static': path.resolve(__dirname, '../node_modules/@deliveroo/consumer-component-library/static'), |
|
}, |
|
}, |
|
}, |
|
{ |
|
loader: 'postcss-loader', |
|
options: { |
|
plugins: [autoprefixer({ browsers: ['last 2 versions'] })], |
|
}, |
|
}, |
|
{ |
|
loader: 'sass-loader', |
|
}, |
|
], |
|
}), |
|
}, |
|
|
|
{ |
|
test: /\.(png|jpg|jpeg|svg|gif|ttf)$/, |
|
use: [ |
|
{ |
|
loader: 'file-loader', |
|
options: { |
|
name: imageNamePattern, |
|
publicPath(url) { |
|
return prefixWithCDN( |
|
url.substring(url.indexOf('dist')).replace('dist', ''), |
|
); |
|
}, |
|
outputPath(filename) { |
|
// Why is this conditional needed? |
|
const result = dev ? path.join(__dirname, `./dist/static/${id}/assets/${filename}`) : getRelativeBuildOutputPath(`assets/${filename}`); |
|
return result; |
|
}, |
|
}, |
|
}, |
|
], |
|
}, |
|
], |
|
}, |
|
|
|
plugins: [ |
|
...config.plugins, |
|
|
|
new ExtractTextPlugin({ |
|
filename(getPath) { |
|
return getRelativeBuildOutputPath(getPath('stylesheets/[name].css')); |
|
}, |
|
publicPath: '', |
|
allChunks: true, |
|
}), |
|
|
|
new webpack.DefinePlugin({ |
|
'process.env.NODE_ENV': JSON.stringify( |
|
dev ? 'development' : 'production', |
|
), |
|
'process.env.BUILD_ID': JSON.stringify(id), |
|
'process.env.CDN_HOST': JSON.stringify(cdnHost), |
|
}), |
|
|
|
new CleanWebpackPlugin([path.join(__dirname, './dist/')], { verbose: false }), |
|
], |
|
}; |
|
|
|
return modifiedConfig; |
|
}, |
|
}; |