import { RuleSetRule } from 'webpack' ; import { isProjectSourceIn , normalizeRuleMatch } from '@reskript/core' ; import { BuildEntry } from '@reskript/settings' ; import * as loaders from '../loaders' ; import { introduceLoaders } from '../utils/loader' ; type LoaderType = keyof typeof loaders ; const createUseWith = ( entry ) => { return ( ... names ) => introduceLoaders ( names , entry ) ; } ; const assetModuleConfig = ( entry ) => { return { type : 'asset' , parser : { dataUrlCondition : { maxSize : entry . projectSettings . build . largeAssetSize , } , } , } ; } ; export const script = ( entry ) : RuleSetRule => { const { cwd , projectSettings : { build : { script : { babel } } } } = entry ; const use = createUseWith ( entry ) ; const isProjectSource = isProjectSourceIn ( cwd ) ; const isWorker = ( resource ) => isProjectSource ( resource ) && /\.worker\.[jt]sx?$/ . test ( resource ) ; const rulesWithBabelRequirement = ( requireBabel ) => { return { oneOf : [ { resource : isWorker , use : use ( 'worker' , requireBabel && 'babel' ) , } , { resource : isProjectSource , use : use ( requireBabel && 'babel' ) , } , { use : use ( requireBabel && 'babel' ) , } , ] , } ; } ; return { test : /\.[jt]sx?$/ , oneOf : [ { resource : normalizeRuleMatch ( cwd , babel ) , ... rulesWithBabelRequirement ( true ) , } , rulesWithBabelRequirement ( false ) , ] , } ; } ; export const less = ( entry ) : RuleSetRule => { const { cwd , usage , projectSettings : { build : { style : { modules , extract } } } } = entry ; const use = createUseWith ( entry ) ; const final = ( usage === 'build' && extract ) ? 'cssExtract' : 'style' ; return { test : /\.less$/ , oneOf : [ { test : /\.global\.less$/ , use : use ( final , 'css' , 'postCSS' , 'less' , 'lessSafe' , 'styleResources' ) , } , { resource : normalizeRuleMatch ( cwd , modules ) , use : use ( 'classNames' , final , 'cssModules' , 'postCSSModules' , 'less' , 'lessSafe' , 'styleResources' ) , } , { use : use ( final , 'css' , 'postCSS' , 'less' , 'lessSafe' , 'styleResources' ) , } , ] , } ; } ; export const css = ( entry ) : RuleSetRule => { const { cwd , usage , projectSettings : { build : { style : { modules , extract } } } } = entry ; const use = createUseWith ( entry ) ; const final = ( usage === 'build' && extract ) ? 'cssExtract' : 'style' ; return { test : /\.css$/ , oneOf : [ { test : /\.global\.css$/ , use : use ( final , 'css' , 'postCSS' ) , } , { resource : normalizeRuleMatch ( cwd , modules ) , use : use ( 'classNames' , final , 'cssModules' , 'postCSSModules' ) , } , { use : use ( final , 'css' , 'postCSS' ) , } , ] , } ; } ; export const image = ( entry ) : RuleSetRule => { const use = createUseWith ( entry ) ; return { test : /\.(jpe?g|png|gif)$/i , use : use ( 'img' ) , ... assetModuleConfig ( entry ) , } ; } ; export const svg = ( entry ) : RuleSetRule => { const { mode } = entry ; const use = createUseWith ( entry ) ; return { test : /\.svg$/ , oneOf : [ { resourceQuery : /^\?react$/ , use : use ( 'svgToComponent' , mode === 'production' && 'svgo' ) , } , { resourceQuery : { not : /^\?react$/ , } , use : use ( mode === 'production' && 'svgo' ) , ... assetModuleConfig ( entry ) , } , ] , } ; } ; export const file = ( entry ) : RuleSetRule => { return { test : /\.(eot|ttf|woff|woff2)(\?.+)?$/ , ... assetModuleConfig ( entry ) , } ; } ;