处理txt的webpack的loader插件 编写一个处理txt的loader插件,适用于wbepack
编写一个处理txt的loader插件,适用于wbepack
实现一个处理txt的插件,给文本每行前后添加****
module. exports = function txtLoader ( content ) { const callback = this . async ( ) ; console. log ( '66667777' , content) try { const contentString = content. toString ( 'utf-8' ) ; const processedContent = contentString. split ( '\n' ) . map ( line => ` **** ${ line. trim ( ) } **** ` ) . join ( '\n' ) ; console. log ( 'processedContent' , processedContent) callback ( null , ` module.exports = ${ JSON . stringify ( processedContent) } ; ` ) ; } catch ( error) { callback ( error) ; }
} ;
module. exports. raw = true ;
适用,这是webpack5.x的配置过程
chainWebpack : ( config ) => { config. module. rule ( 'txt' ) . test ( / \.txt$ / ) . use ( 'txt-loader' ) . loader ( path. resolve ( __dirname, './webpack-txt-loader.js' ) ) . end ( )
}