Last active
July 2, 2021 00:43
-
-
Save leastbad/d4c7e1d7872a1e20c0e9a82340f2082f to your computer and use it in GitHub Desktop.
Webpacker 5 config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (api) { | |
var validEnv = ['development', 'test', 'production'] | |
var currentEnv = api.env() | |
var isDevelopmentEnv = api.env('development') | |
var isProductionEnv = api.env('production') | |
var isTestEnv = api.env('test') | |
if (!validEnv.includes(currentEnv)) { | |
throw new Error( | |
'Please specify a valid `NODE_ENV` or ' + | |
'`BABEL_ENV` environment variables. Valid values are "development", ' + | |
'"test", and "production". Instead, received: ' + | |
JSON.stringify(currentEnv) + | |
'.' | |
) | |
} | |
return { | |
presets: [ | |
isTestEnv && [ | |
'@babel/preset-env', | |
{ | |
targets: { | |
node: 'current' | |
} | |
} | |
], | |
(isProductionEnv || isDevelopmentEnv) && [ | |
'@babel/preset-env', | |
{ | |
forceAllTransforms: true, | |
useBuiltIns: 'entry', | |
corejs: 3, | |
modules: false, | |
exclude: ['transform-typeof-symbol'] | |
} | |
] | |
].filter(Boolean), | |
plugins: [ | |
'babel-plugin-macros', | |
'@babel/plugin-syntax-dynamic-import', | |
isTestEnv && 'babel-plugin-dynamic-import-node', | |
'@babel/plugin-transform-destructuring', | |
[ | |
'@babel/plugin-proposal-class-properties', | |
{ | |
loose: true | |
} | |
], | |
[ | |
'@babel/plugin-proposal-object-rest-spread', | |
{ | |
useBuiltIns: true | |
} | |
], | |
[ | |
'@babel/plugin-transform-runtime', | |
{ | |
helpers: false, | |
regenerator: true, | |
corejs: false | |
} | |
], | |
[ | |
'@babel/plugin-transform-regenerator', | |
{ | |
async: false | |
} | |
], | |
['@babel/plugin-proposal-private-methods', { loose: true }] | |
].filter(Boolean) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { environment } = require('@rails/webpacker') | |
const sassLoader = environment.loaders.get('sass') | |
const sassLoaderConfig = sassLoader.use.find(function (element) { | |
return element.loader == 'sass-loader' | |
}) | |
const options = sassLoaderConfig.options | |
options.implementation = require('sass') | |
function hotfixPostcssLoaderConfig (subloader) { | |
const subloaderName = subloader.loader | |
if (subloaderName === 'postcss-loader') { | |
subloader.options.postcssOptions = subloader.options.config | |
delete subloader.options.config | |
} | |
} | |
environment.loaders.keys().forEach(loaderName => { | |
const loader = environment.loaders.get(loaderName) | |
loader.use.forEach(hotfixPostcssLoaderConfig) | |
}) | |
module.exports = environment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "webpacker", "~> 5.4.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"@fullhuman/postcss-purgecss": "^4.0.3", | |
"@rails/webpacker": "5.4.0", | |
"postcss": "^8.2.10", | |
"postcss-loader": "^4.0.3", | |
"sass": "^1.32.7" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you probably want a different safelist | |
const purgecss = require('@fullhuman/postcss-purgecss')({ | |
content: [ | |
'./app/**/*.html.erb', | |
'./app/helpers/**/*.rb', | |
'./app/javascript/**/*.js' | |
], | |
safelist: { | |
standard: [/^bg/, /^text/], | |
greedy: [/^ss/] | |
}, | |
defaultExtractor: content => { | |
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || [] | |
const innerMatches = content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || [] | |
return broadMatches.concat(innerMatches) | |
} | |
}) | |
module.exports = { | |
plugins: [ | |
require('postcss-import'), | |
require('postcss-flexbugs-fixes'), | |
require('postcss-preset-env')({ | |
autoprefixer: { | |
flexbox: 'no-2009' | |
}, | |
stage: 3 | |
}), | |
...(process.env.NODE_ENV === 'production' ? [purgecss] : []) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment