Skip to content

Commit

Permalink
Refactor to allow exporting logic of inliningCss
Browse files Browse the repository at this point in the history
  • Loading branch information
justin808 committed Jun 21, 2021
1 parent 3a44839 commit 9e282f6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 17 deletions.
12 changes: 8 additions & 4 deletions package/__tests__/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('Env', () => {
railsEnv: 'development',
nodeEnv: 'development',
isProduction: false,
isDevelopment: true
isDevelopment: true,
runningWebpackDevServer: false
})
})

Expand All @@ -26,7 +27,8 @@ describe('Env', () => {
railsEnv: 'development',
nodeEnv: 'production',
isProduction: true,
isDevelopment: false
isDevelopment: false,
runningWebpackDevServer: false
})
})

Expand All @@ -37,7 +39,8 @@ describe('Env', () => {
railsEnv: 'production',
nodeEnv: 'production',
isProduction: true,
isDevelopment: false
isDevelopment: false,
runningWebpackDevServer: false
})
})

Expand All @@ -48,7 +51,8 @@ describe('Env', () => {
railsEnv: 'staging',
nodeEnv: 'production',
isProduction: true,
isDevelopment: false
isDevelopment: false,
runningWebpackDevServer: false
})
})
})
8 changes: 7 additions & 1 deletion package/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ const config = safeLoad(readFileSync(configPath), 'utf8')
const availableEnvironments = Object.keys(config).join('|')
const regex = new RegExp(`^(${availableEnvironments})$`, 'g')

// v4 of webpack-dev-server will switch to WEBPACK_DEV_SERVE
// https://github.com/rails/webpacker/issues/3057
const runningWebpackDevServer = process.env.WEBPACK_DEV_SERVER === 'true' ||
process.env.WEBPACK_DEV_SERVE === 'true'

module.exports = {
railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
nodeEnv,
isProduction,
isDevelopment
isDevelopment,
runningWebpackDevServer
}
2 changes: 1 addition & 1 deletion package/environments/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { merge } = require('webpack-merge')

const baseConfig = require('./base')
const devServer = require('../dev_server')
const { runningWebpackDevServer } = require('../utils/helpers')
const { runningWebpackDevServer } = require('../env')

const { outputPath: contentBase, publicPath } = require('../config')

Expand Down
2 changes: 2 additions & 0 deletions package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const config = require('./config')
const devServer = require('./dev_server')
const { nodeEnv } = require('./env')
const { moduleExists, canProcess } = require('./utils/helpers')
const inliningCss = require('./inliningCss')

const webpackConfig = () => {
const path = resolve(__dirname, 'environments', `${nodeEnv}.js`)
Expand All @@ -25,5 +26,6 @@ module.exports = {
rules,
moduleExists,
canProcess,
inliningCss,
...webpackMerge
}
7 changes: 7 additions & 0 deletions package/inliningCss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { runningWebpackDevServer } = require('./env')
const devServer = require('./dev_server')

// This logic is tied to lib/webpacker/instance.rb
const inliningCss = runningWebpackDevServer && devServer.hmr

module.exports = inliningCss
6 changes: 2 additions & 4 deletions package/utils/get_style_rule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint global-require: 0 */

const devServer = require('../dev_server')
const { canProcess, moduleExists, runningWebpackDevServer } = require('./helpers')
const { canProcess, moduleExists } = require('./helpers')
const inliningCss = require('../inliningCss')

const getStyleRule = (test, preprocessors = []) => {
if (moduleExists('css-loader')) {
Expand All @@ -12,7 +11,6 @@ const getStyleRule = (test, preprocessors = []) => {
}))

// style-loader is required when using css modules with HMR on the webpack-dev-server
const inliningCss = runningWebpackDevServer && devServer.hmr

const use = [
inliningCss ? 'style-loader' : require('mini-css-extract-plugin').loader,
Expand Down
8 changes: 1 addition & 7 deletions package/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ const canProcess = (rule, fn) => {
return null
}

// v4 of webpack-dev-server will switch to WEBPACK_DEV_SERVE
// https://github.com/rails/webpacker/issues/3057
const runningWebpackDevServer = process.env.WEBPACK_DEV_SERVER === 'true' ||
process.env.WEBPACK_DEV_SERVE === 'true'

module.exports = {
chdirTestApp,
chdirCwd,
Expand All @@ -52,6 +47,5 @@ module.exports = {
ensureTrailingSlash,
canProcess,
moduleExists,
resetEnv,
runningWebpackDevServer
resetEnv
}

0 comments on commit 9e282f6

Please sign in to comment.