-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from vordgi/ncm-36
ncm-36 update the configuration and its validation
- Loading branch information
Showing
6 changed files
with
90 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { CUSTOM, VALID_MINIFIERS_KEYS } from "./constants/minifiers"; | ||
|
||
type Config = { | ||
type?: (typeof VALID_MINIFIERS_KEYS)[number]; | ||
templateString?: string; | ||
} | ||
|
||
const validKeys = ['type', 'templateString']; | ||
|
||
const validateIsObject = (config: unknown): config is Config => { | ||
if (!config) return false; | ||
|
||
if (typeof config !== 'object' || Array.isArray(config)) { | ||
console.error(`next-classnames-minifier: Invalid configuration. Expected object, received ${typeof config}. See https://github.com/vordgi/next-classnames-minifier#configuration`); | ||
process.exit(); | ||
} | ||
|
||
const isValidKeys = Object.keys(config).every(key => validKeys.includes(key)); | ||
|
||
if (!isValidKeys) { | ||
console.error(`next-classnames-minifier: Invalid configuration. Valid keys are: ${validKeys.join(', ')}. See https://github.com/vordgi/next-classnames-minifier#configuration`); | ||
process.exit(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
const validateConfig = (config: unknown = {}): Config => { | ||
if (!validateIsObject(config)) return {}; | ||
|
||
if (config.type && !VALID_MINIFIERS_KEYS.includes(config.type)) { | ||
console.error(`next-classnames-minifier: Invalid configuration. Valid types are: ${VALID_MINIFIERS_KEYS.join(', ')}. See https://github.com/vordgi/next-classnames-minifier#configuration`) | ||
process.exit(); | ||
} | ||
|
||
if (config.type === CUSTOM && !config.templateString) { | ||
console.error('next-classnames-minifier: Invalid configuration. The templateString option is required for the "custom" type. See https://github.com/vordgi/next-classnames-minifier#configuration') | ||
process.exit(); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export default validateConfig; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,45 @@ | ||
import type { Configuration } from 'webpack/types'; | ||
import type { Options } from './lib/types/plugin'; | ||
import type { Config } from './lib/types/plugin'; | ||
import type ConverterBase from './lib/converters/ConverterBase'; | ||
import { CUSTOM, MINIFIED, VALID_MINIFIERS_KEYS } from './lib/constants/minifiers'; | ||
import { CUSTOM, MINIFIED } from './lib/constants/minifiers'; | ||
import ConverterMinified from './lib/converters/ConverterMinified'; | ||
import ConverterCustom from './lib/converters/ConverterCustom'; | ||
import injectConfig from './lib/injectConfig'; | ||
import validateConfig from './lib/validateConfig'; | ||
import path from 'path'; | ||
|
||
let classnamesMinifier: ConverterBase; | ||
let infoMessageShown = false; | ||
|
||
const withClassnameMinifier = (pluginOptions: Options = {}) => (nextConfig: any = {}) => ({ | ||
...nextConfig, | ||
webpack: (config: Configuration, options: any) => { | ||
const { dev = 'none', prod = 'minified' } = pluginOptions; | ||
const isProd = process.env.NODE_ENV === 'production'; | ||
const minifierConfig = isProd ? prod : dev; | ||
const minifierType = typeof minifierConfig === 'string' ? minifierConfig : minifierConfig.type; | ||
|
||
if (!infoMessageShown) { | ||
if (!VALID_MINIFIERS_KEYS.includes(minifierType)) { | ||
console.log(`next-classnames-minifier. Invalid key for target env: ${minifierType}, valid keys are: ${VALID_MINIFIERS_KEYS.join(', ')}`); | ||
process.kill(0); | ||
process.exit(); | ||
} else if (!isProd && minifierType === MINIFIED) { | ||
console.log(`next-classnames-minifier. It is not recommended to use "minified" mode in development mode, it may slow down the update`); | ||
} else if (minifierType === 'custom' && (typeof minifierConfig !== 'object' || !minifierConfig.templateString)) { | ||
console.log(`next-classnames-minifier. Add templateString for custom minifier`); | ||
process.kill(0); | ||
process.exit(); | ||
} | ||
infoMessageShown = true; | ||
} | ||
|
||
if (minifierType === MINIFIED) { | ||
if (!classnamesMinifier) { | ||
const cacheDir = path.join(process.cwd(), '.next/cache/ncm'); | ||
classnamesMinifier = new ConverterMinified(cacheDir); | ||
} | ||
|
||
injectConfig({ classnamesMinifier }, config.module?.rules); | ||
} else if (minifierType === CUSTOM && typeof minifierConfig === 'object' && minifierConfig.templateString) { | ||
if (!classnamesMinifier) { | ||
classnamesMinifier = new ConverterCustom(); | ||
const withClassnameMinifier = (pluginOptions: Config = {}) => { | ||
validateConfig(pluginOptions); | ||
|
||
return (nextConfig: any = {}) => ({ | ||
...nextConfig, | ||
webpack: (config: Configuration, options: any) => { | ||
const { type = MINIFIED, templateString } = pluginOptions; | ||
|
||
if (type === MINIFIED) { | ||
if (!classnamesMinifier) { | ||
const cacheDir = path.join(process.cwd(), '.next/cache/ncm'); | ||
classnamesMinifier = new ConverterMinified(cacheDir); | ||
} | ||
|
||
injectConfig({ classnamesMinifier }, config.module?.rules); | ||
} else if (type === CUSTOM && templateString) { | ||
if (!classnamesMinifier) { | ||
classnamesMinifier = new ConverterCustom(); | ||
} | ||
|
||
injectConfig({ localIdentName: templateString, classnamesMinifier }, config.module?.rules); | ||
} | ||
|
||
injectConfig({ localIdentName: minifierConfig.templateString, classnamesMinifier }, config.module?.rules); | ||
} | ||
if (typeof nextConfig.webpack === 'function') { | ||
return nextConfig.webpack(config, options); | ||
} | ||
|
||
if (typeof nextConfig.webpack === 'function') { | ||
return nextConfig.webpack(config, options); | ||
return config; | ||
} | ||
|
||
return config; | ||
} | ||
}); | ||
}) | ||
}; | ||
|
||
export default withClassnameMinifier; |