Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mizok committed Apr 16, 2022
1 parent 415a13c commit 1fa0b13
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plug
import * as webpack from 'webpack';
import 'webpack-dev-server'; // dont remove this import, it's for webpack-dev-server type
import HtmlWebpackPlugin from 'html-webpack-plugin';
const COMPRESS = false;
const COMPRESS = true;

const getEntriesByParsingTemplateNames = (templatesFolderName,atRoot = true)=>{
const getEntriesByParsingTemplateNames = (templatesFolderName, atRoot = true) => {
const folderPath = resolve(__dirname, `./src/${templatesFolderName}`);
const entryObj: webpack.EntryObject = {};
const templateRegx = /(.*)(\.)(ejs|html)/g;
Expand All @@ -21,25 +21,25 @@ const getEntriesByParsingTemplateNames = (templatesFolderName,atRoot = true)=>{
entryName = entryName.replace(entryRegex, `$3`);
}

const entryDependency= atRoot?entryName:`${templatesFolderName}/${entryName}`
const entryDependency = atRoot ? entryName : `${templatesFolderName}/${entryName}`

let entryPath = resolve(__dirname, `src/ts/${entryDependency}.ts`);
// entry stylesheet
let entryStyleSheetPath = resolve(__dirname, `./src/scss/${entryDependency}.scss`);

entryPath = fs.existsSync(entryPath)?entryPath:undefined;
entryStyleSheetPath = fs.existsSync(entryStyleSheetPath)?entryStyleSheetPath:undefined;
entryPath = fs.existsSync(entryPath) ? entryPath : undefined;
entryStyleSheetPath = fs.existsSync(entryStyleSheetPath) ? entryStyleSheetPath : undefined;

// import es6-promise and scss util automatically
entryObj[entryName] = ['es6-promise/auto',entryPath,'./src/scss/reset.scss',entryStyleSheetPath].filter(function (x: string | undefined) {
entryObj[entryName] = ['es6-promise/auto', entryPath, './src/scss/reset.scss', entryStyleSheetPath].filter(function (x: string | undefined) {
return x !== undefined;
});

})
return entryObj;
}

const getTemaplteInstancesByParsingTemplateNames = (templatesFolderName,atRoot=true)=>{
const getTemaplteInstancesByParsingTemplateNames = (templatesFolderName, atRoot = true) => {
const forderPath = resolve(__dirname, `./src/${templatesFolderName}`);
return fs.readdirSync(forderPath).map((fullFileName: string) => {
const templateRegx = /(.*)(\.)(ejs|html)/g;
Expand All @@ -59,11 +59,11 @@ const getTemaplteInstancesByParsingTemplateNames = (templatesFolderName,atRoot=t
fs.writeFile(ejsFilePath, ' ', () => { });
console.warn(`WARNING : ${fullFileName} is an empty file`);
}

return new HtmlWebpackPlugin({
cache: false,
chunks: [entryName],
filename: `${atRoot?'':templatesFolderName+'/'}${outputFileName}.html`,
filename: `${atRoot ? '' : templatesFolderName + '/'}${outputFileName}.html`,
template: isEjs ? ejsFilePath : ejsFilePath.replace(ejsRegex, `$1.html`),
favicon: 'src/assets/images/logo.svg',
minify: COMPRESS ? {
Expand All @@ -74,7 +74,7 @@ const getTemaplteInstancesByParsingTemplateNames = (templatesFolderName,atRoot=t
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}:false
} : false
})
}).filter(function (x: HtmlWebpackPlugin | undefined) {
return x !== undefined;
Expand All @@ -86,8 +86,8 @@ const pageEntries: webpack.EntryObject = getEntriesByParsingTemplateNames('pages
const pageEntryTemplates: HtmlWebpackPlugin[] = getTemaplteInstancesByParsingTemplateNames('pages');


const config = (env:any,argv:any):webpack.Configuration=>{
const configObj:webpack.Configuration = {
const config = (env: any, argv: any): webpack.Configuration => {
const configObj: webpack.Configuration = {
entry: pageEntries,
output: {
filename: 'js/[name].[chunkhash].js',
Expand Down Expand Up @@ -141,8 +141,8 @@ const config = (env:any,argv:any):webpack.Configuration=>{
{
loader: 'template-ejs-loader',
options: {
data:{
mode:argv.mode
data: {
mode: argv.mode
}
}
}
Expand Down Expand Up @@ -177,19 +177,22 @@ const config = (env:any,argv:any):webpack.Configuration=>{
}
},
(() => {
return COMPRESS ? 'sass-loader': {
return COMPRESS ? 'sass-loader' : {
loader: 'sass-loader',
options: { sourceMap: true, sassOptions: { minimize: false, outputStyle: 'expanded' } }
}
}
})()

]
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg)$/,
type: 'asset/inline',
generator: {
filename: 'assets/fonts/[name][ext]'
}
}

]
},
resolve: {
Expand Down Expand Up @@ -217,8 +220,11 @@ const config = (env:any,argv:any):webpack.Configuration=>{
maxAssetSize: 512000
},
plugins: [
new webpack.DefinePlugin({
'PROCESS.MODE': JSON.stringify(argv.mode)
}),
(() => {
return COMPRESS ? new OptimizeCssAssetsWebpackPlugin():undefined
return COMPRESS ? new OptimizeCssAssetsWebpackPlugin() : undefined
})(),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
Expand All @@ -239,7 +245,7 @@ const config = (env:any,argv:any):webpack.Configuration=>{
}
),
...pageEntryTemplates

].filter(function (x) {
return x !== undefined;
})
Expand Down

0 comments on commit 1fa0b13

Please sign in to comment.