Skip to content

Commit

Permalink
feature: webpack v5
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 5, 2023
1 parent ddf636d commit 4776d6f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .madrun.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export default {
'coverage:report': () => 'c8 report --reporter=lcov',
'report': () => 'c8 report --reporter=lcov',
'6to5': () => [buildEnv, 'webpack --progress'],
'6to5:client': () => run('6to5', '--mode production'),
'6to5:client': () => run('6to5', '--mode production', {
NODE_ENV: 'production',
}),
'6to5:client:dev': async () => await run('6to5', '--mode development', {
NODE_ENV: 'development',
}),
Expand Down
59 changes: 30 additions & 29 deletions .webpack/css.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'se strict';

const fs = require('fs');
const {
Expand All @@ -7,15 +7,12 @@ const {
join,
} = require('path');

const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const {env} = require('process');
const {env} = process;
const isDev = env.NODE_ENV === 'development';

const extractCSS = (a) => new ExtractTextPlugin(`${a}.css`);
const extractMain = extractCSS('[name]');

const cssNames = [
'nojs',
'view',
Expand All @@ -25,52 +22,56 @@ const cssNames = [
...getCSSList('columns'),
];

const cssPlugins = cssNames.map(extractCSS);
const clean = (a) => a.filter(Boolean);

const plugins = clean([
...cssPlugins,
extractMain,
!isDev && new OptimizeCssAssetsPlugin(),
]);
const plugins = [
new MiniCssExtractPlugin({
chunkFilename: '[name].css',
}),
new CssMinimizerPlugin(),
];

const rules = [{
test: /\.css$/,
exclude: /css\/(nojs|view|config|terminal|user-menu|columns.*)\.css/,
use: extractMain.extract(['css-loader']),
}, ...cssPlugins.map(extract), {
use: [MiniCssExtractPlugin.loader, "css-loader"],
}, /*{
test: /\.(png|gif|svg|woff|woff2|eot|ttf)$/,
use: {
loader: 'url-loader',
options: {
limit: 100_000,
},
},
}];
}*/];

module.exports = {
plugins,
module: {
rules,
},
optimization: {
splitChunks: {
cacheGroups: {
cloudcmdCommon: {
type: "css/mini-extract",
name: "cloudcmd.common",
chunks: (chunk) => {
//return !/css\/(nojs|view|config|terminal|user-menu|columns.*)\.css/.test(name)
console.log(chunk);
return !chunk.name.includes('modules');
},
enforce: true,
},
},
},
},
};

function getCSSList(dir) {
const base = (a) => basename(a, extname(a));
const addDir = (name) => `${dir}/${name}`;
const rootDir = join(__dirname, '..');

return fs
.readdirSync(`${rootDir}/css/${dir}`)
return fs.readdirSync(`${rootDir}/css/${dir}`)
.map(base)
.map(addDir);
}

function extract(extractPlugin) {
const {filename} = extractPlugin;

return {
test: RegExp(`css/${filename}`),
use: extractPlugin.extract(['css-loader']),
};
}
10 changes: 8 additions & 2 deletions .webpack/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
const {EnvironmentPlugin} = require('webpack');
const WebpackBar = require('webpackbar');

const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin');
//const { ServiceWorkerPlugin } = require('service-worker-webpack');

const {env} = require('process');
const modules = './modules';
Expand Down Expand Up @@ -51,10 +51,12 @@ const plugins = [
new EnvironmentPlugin({
NODE_ENV,
}),
/*
new ServiceWorkerWebpackPlugin({
entry: join(__dirname, '..', 'client', 'sw', 'sw.js'),
excludes: ['*'],
}),
*/
new WebpackBar(),
];

Expand All @@ -66,6 +68,10 @@ const splitChunks = {
module.exports = {
resolve: {
symlinks: false,
fallback: {
"path": require.resolve("path-browserify"),
"process": require.resolve("process/browser"),
},
},
devtool,
optimization: {
Expand Down Expand Up @@ -101,7 +107,7 @@ module.exports = {
devtoolModuleFilenameTemplate,
publicPath: '/dist/',
},
externals: [convertToWebpack5Externals(externals)],
externals: [externals],
module: {
rules,
noParse,
Expand Down
8 changes: 6 additions & 2 deletions client/sw/register.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const tryToCatch = require('try-to-catch');
const {Workbox} = require('workbox-window');

module.exports.registerSW = registerSW;
module.exports.unregisterSW = unregisterSW;
Expand All @@ -20,8 +21,11 @@ async function registerSW(prefix) {
return;

const {serviceWorker} = navigator;
const register = serviceWorker.register.bind(serviceWorker);
const [e, sw] = await tryToCatch(register, `${prefix}/sw.js`);
//const register = serviceWorker.register.bind(serviceWorker);
//const [e, sw] = await tryToCatch(register, `${prefix}/sw.js`);
const wb = new Workbox(`${prefix}/sw.js`);
const register = wb.register.bind(wb);
const [e, sw] = await tryToCatch(register);

if (e)
return null;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@
"morgan": "^1.6.1",
"multi-rename": "^2.0.0",
"nodemon": "^3.0.1",
"path-browserify": "^1.0.1",
"philip": "^2.0.0",
"place": "^1.1.4",
"process": "^0.11.10",
"readjson": "^2.0.1",
"request": "^2.76.0",
"rimraf": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion server/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const isDev = process.env.NODE_ENV === 'development';
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';

const dist = getDist(isDev);
const columnsDir = path.join(__dirname, '..', dist, 'columns');
const columnsDir = path.join(__dirname, '..', 'css', 'columns');

const names = fs
.readdirSync(columnsDir)
Expand Down

0 comments on commit 4776d6f

Please sign in to comment.