Skip to content

Commit 7761245

Browse files
silverwindtechknowlogick
andauthoredJun 28, 2020
Move fomantic and jQuery to main webpack bundle (#11997)
This saves around 3 MB binary size by not including useless fomantic files in the build. Also, this allows us to move jQuery into the main bundle as well which eliminates a few HTTP requests. Also included are webpack config changes: - split less and css loaders to speed up compliation - enable css sourcemaps - switch css minfier plugin to cssnano-webpack-plugin which works better for sourcemaps than the previous plugin Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 2281b04 commit 7761245

File tree

9 files changed

+88
-69
lines changed

9 files changed

+88
-69
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ coverage.all
7979
/public/serviceworker.js
8080
/public/css
8181
/public/fonts
82-
/public/fomantic
8382
/public/img/svg
83+
/web_src/fomantic/build
8484
/VERSION
8585

8686
# Snapcraft

‎Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(G
8888

8989
GO_PACKAGES ?= $(filter-out code.gitea.io/gitea/integrations/migration-test,$(filter-out code.gitea.io/gitea/integrations,$(shell $(GO) list -mod=vendor ./... | grep -v /vendor/)))
9090

91-
WEBPACK_SOURCES := $(shell find web_src/js web_src/less -type f)
91+
WEBPACK_SOURCES := $(shell find web_src -type f)
9292
WEBPACK_CONFIGS := webpack.config.js
9393
WEBPACK_DEST := public/js/index.js public/css/index.css
9494
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/serviceworker.js public/img/svg
@@ -111,8 +111,8 @@ endif
111111
GO_SOURCES_OWN := $(filter-out vendor/% %/bindata.go, $(GO_SOURCES))
112112

113113
FOMANTIC_CONFIGS := semantic.json web_src/fomantic/theme.config.less web_src/fomantic/_site/globals/site.variables
114-
FOMANTIC_DEST := public/fomantic/semantic.min.js public/fomantic/semantic.min.css
115-
FOMANTIC_DEST_DIR := public/fomantic
114+
FOMANTIC_DEST := web_src/fomantic/build/semantic.js web_src/fomantic/build/semantic.css
115+
FOMANTIC_DEST_DIR := web_src/fomantic/build
116116

117117
#To update swagger use: GO111MODULE=on go get -u github.com/go-swagger/go-swagger/cmd/swagger@v0.20.1
118118
SWAGGER := $(GO) run -mod=vendor github.com/go-swagger/go-swagger/cmd/swagger
@@ -297,7 +297,7 @@ lint-frontend: node_modules
297297
npx stylelint web_src/less
298298

299299
.PHONY: watch-frontend
300-
watch-frontend: node_modules
300+
watch-frontend: node-check $(FOMANTIC_DEST) node_modules
301301
rm -rf $(WEBPACK_DEST_ENTRIES)
302302
NODE_ENV=development npx webpack --hide-modules --display-entrypoints=false --watch --progress
303303

@@ -590,7 +590,7 @@ npm-update: node-check | node_modules
590590
.PHONY: fomantic
591591
fomantic: $(FOMANTIC_DEST)
592592

593-
$(FOMANTIC_DEST): $(FOMANTIC_CONFIGS) package-lock.json | node_modules
593+
$(FOMANTIC_DEST): $(FOMANTIC_CONFIGS) | node_modules
594594
rm -rf $(FOMANTIC_DEST_DIR)
595595
cp web_src/fomantic/theme.config.less node_modules/fomantic-ui/src/theme.config
596596
cp -r web_src/fomantic/_site/* node_modules/fomantic-ui/src/_site/

‎modules/public/public.go

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type Options struct {
3333
// KnownPublicEntries list all direct children in the `public` directory
3434
var KnownPublicEntries = []string{
3535
"css",
36-
"fomantic",
3736
"img",
3837
"js",
3938
"serviceworker.js",

‎package-lock.json

+10-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"clipboard": "2.0.6",
1616
"core-js": "3.6.5",
1717
"css-loader": "3.5.3",
18-
"cssnano": "4.1.10",
18+
"cssnano-webpack-plugin": "1.0.3",
1919
"domino": "2.1.5",
2020
"dropzone": "5.7.1",
2121
"fast-glob": "3.2.2",
@@ -29,10 +29,8 @@
2929
"mini-css-extract-plugin": "0.9.0",
3030
"monaco-editor": "0.20.0",
3131
"monaco-editor-webpack-plugin": "1.9.0",
32-
"optimize-css-assets-webpack-plugin": "5.0.3",
3332
"postcss-loader": "3.0.0",
3433
"postcss-preset-env": "6.7.0",
35-
"postcss-safe-parser": "4.0.2",
3634
"svg-sprite-loader": "5.0.0",
3735
"svgo": "1.3.2",
3836
"svgo-loader": "2.2.1",

‎semantic.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
"themes": "src/themes/"
1010
},
1111
"output": {
12-
"packaged": "../../public/fomantic/",
13-
"uncompressed": "../../public/fomantic/components/",
14-
"compressed": "../../public/fomantic/components/",
15-
"themes": "../../public/fomantic/themes/"
12+
"packaged": "../../web_src/fomantic/build/",
13+
"uncompressed": "../../web_src/fomantic/build/components/",
14+
"compressed": "../../web_src/fomantic/build/components/",
15+
"themes": "../../web_src/fomantic/build/themes/"
1616
},
17-
"clean": "../../public/fomantic/"
17+
"clean": "../../web_src/fomantic/build/"
1818
},
1919
"permission": false,
2020
"autoInstall": false,

‎templates/base/footer.tmpl

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
{{template "custom/body_outer_post" .}}
1212

1313
{{template "base/footer_content" .}}
14-
15-
<script src="{{StaticUrlPrefix}}/js/jquery.js?v={{MD5 AppVer}}"></script>
1614
{{if .RequireSimpleMDE}}
1715
<script src="{{StaticUrlPrefix}}/vendor/plugins/simplemde/simplemde.min.js"></script>
1816
<script src="{{StaticUrlPrefix}}/vendor/plugins/codemirror/addon/mode/loadmode.js"></script>
@@ -23,9 +21,6 @@
2321
{{end}}
2422

2523
<!-- Third-party libraries -->
26-
{{if .RequireMinicolors}}
27-
<script src="{{StaticUrlPrefix}}/vendor/plugins/jquery.minicolors/jquery.minicolors.min.js"></script>
28-
{{end}}
2924
{{if .RequireU2F}}
3025
<script src="{{StaticUrlPrefix}}/vendor/plugins/u2f/index.js"></script>
3126
{{end}}
@@ -34,8 +29,10 @@
3429
<script src='{{ URLJoin .RecaptchaURL "api.js"}}' async></script>
3530
{{end}}
3631
{{end}}
37-
<script src="{{StaticUrlPrefix}}/fomantic/semantic.min.js?v={{MD5 AppVer}}"></script>
3832
<script src="{{StaticUrlPrefix}}/js/index.js?v={{MD5 AppVer}}"></script>
33+
{{if .RequireMinicolors}}
34+
<script src="{{StaticUrlPrefix}}/vendor/plugins/jquery.minicolors/jquery.minicolors.min.js"></script>
35+
{{end}}
3936
{{template "custom/footer" .}}
4037
</body>
4138
</html>

‎templates/base/head.tmpl

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
{{if .RequireSimpleMDE}}
9696
<link rel="stylesheet" href="{{StaticUrlPrefix}}/vendor/plugins/simplemde/simplemde.min.css">
9797
{{end}}
98-
<link rel="stylesheet" href="{{StaticUrlPrefix}}/fomantic/semantic.min.css?v={{MD5 AppVer}}">
9998
<link rel="stylesheet" href="{{StaticUrlPrefix}}/css/index.css?v={{MD5 AppVer}}">
10099
<noscript>
101100
<style>

‎webpack.config.js

+63-29
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const cssnano = require('cssnano');
21
const fastGlob = require('fast-glob');
32
const wrapAnsi = require('wrap-ansi');
3+
const CssNanoPlugin = require('cssnano-webpack-plugin');
44
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
55
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
66
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
7-
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
87
const PostCSSPresetEnv = require('postcss-preset-env');
9-
const PostCSSSafeParser = require('postcss-safe-parser');
108
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
119
const TerserPlugin = require('terser-webpack-plugin');
1210
const VueLoaderPlugin = require('vue-loader/lib/plugin');
@@ -24,19 +22,31 @@ for (const path of glob('web_src/less/themes/*.less')) {
2422

2523
const isProduction = process.env.NODE_ENV !== 'development';
2624

25+
const filterCssImport = (parsedImport, cssFile) => {
26+
const url = parsedImport && parsedImport.url ? parsedImport.url : parsedImport;
27+
const importedFile = url.replace(/[?#].+/, '').toLowerCase();
28+
if (cssFile.includes('monaco')) return true;
29+
if (cssFile.includes('fomantic')) {
30+
if (/brand-icons/.test(importedFile)) return false;
31+
if (/(eot|ttf|woff)$/.test(importedFile)) return false;
32+
return true;
33+
}
34+
return cssFile.includes('node_modules');
35+
};
36+
2737
module.exports = {
2838
mode: isProduction ? 'production' : 'development',
2939
entry: {
3040
index: [
41+
resolve(__dirname, 'web_src/js/jquery.js'),
42+
resolve(__dirname, 'web_src/fomantic/build/semantic.js'),
3143
resolve(__dirname, 'web_src/js/index.js'),
44+
resolve(__dirname, 'web_src/fomantic/build/semantic.css'),
3245
resolve(__dirname, 'web_src/less/index.less'),
3346
],
3447
swagger: [
3548
resolve(__dirname, 'web_src/js/standalone/swagger.js'),
3649
],
37-
jquery: [
38-
resolve(__dirname, 'web_src/js/jquery.js'),
39-
],
4050
serviceworker: [
4151
resolve(__dirname, 'web_src/js/serviceworker.js'),
4252
],
@@ -66,12 +76,9 @@ module.exports = {
6676
},
6777
},
6878
}),
69-
new OptimizeCSSAssetsPlugin({
70-
cssProcessor: cssnano,
71-
cssProcessorOptions: {
72-
parser: PostCSSSafeParser,
73-
},
74-
cssProcessorPluginOptions: {
79+
new CssNanoPlugin({
80+
sourceMap: true,
81+
cssnanoOptions: {
7582
preset: [
7683
'default',
7784
{
@@ -91,10 +98,10 @@ module.exports = {
9198
monaco: {
9299
test: /monaco-editor/,
93100
name: 'monaco',
94-
chunks: 'async'
95-
}
96-
}
97-
}
101+
chunks: 'async',
102+
},
103+
},
104+
},
98105
},
99106
module: {
100107
rules: [
@@ -150,12 +157,41 @@ module.exports = {
150157
],
151158
'@babel/plugin-proposal-object-rest-spread',
152159
],
160+
generatorOpts: {
161+
compact: false,
162+
},
153163
},
154164
},
155165
],
156166
},
157167
{
158-
test: /\.(less|css)$/i,
168+
test: /.css$/i,
169+
use: [
170+
{
171+
loader: MiniCssExtractPlugin.loader,
172+
},
173+
{
174+
loader: 'css-loader',
175+
options: {
176+
importLoaders: 1,
177+
url: filterCssImport,
178+
import: filterCssImport,
179+
sourceMap: true,
180+
},
181+
},
182+
{
183+
loader: 'postcss-loader',
184+
options: {
185+
plugins: () => [
186+
PostCSSPresetEnv(),
187+
],
188+
sourceMap: true,
189+
},
190+
},
191+
],
192+
},
193+
{
194+
test: /.less$/i,
159195
use: [
160196
{
161197
loader: MiniCssExtractPlugin.loader,
@@ -164,22 +200,25 @@ module.exports = {
164200
loader: 'css-loader',
165201
options: {
166202
importLoaders: 2,
167-
url: (_url, resourcePath) => {
168-
// only resolve URLs for dependencies
169-
return resourcePath.includes('node_modules');
170-
},
171-
}
203+
url: filterCssImport,
204+
import: filterCssImport,
205+
sourceMap: true,
206+
},
172207
},
173208
{
174209
loader: 'postcss-loader',
175210
options: {
176211
plugins: () => [
177212
PostCSSPresetEnv(),
178213
],
214+
sourceMap: true,
179215
},
180216
},
181217
{
182218
loader: 'less-loader',
219+
options: {
220+
sourceMap: true,
221+
},
183222
},
184223
],
185224
},
@@ -232,9 +271,10 @@ module.exports = {
232271
chunkFilename: 'css/[name].css',
233272
}),
234273
new SourceMapDevToolPlugin({
235-
filename: 'js/[name].js.map',
274+
filename: '[file].map',
236275
include: [
237276
'js/index.js',
277+
'css/index.css',
238278
],
239279
}),
240280
new SpriteLoaderPlugin({
@@ -251,12 +291,6 @@ module.exports = {
251291
modulesDirectories: [
252292
resolve(__dirname, 'node_modules'),
253293
],
254-
additionalModules: [
255-
{
256-
name: 'fomantic-ui',
257-
directory: resolve(__dirname, 'node_modules/fomantic-ui'),
258-
},
259-
],
260294
renderLicenses: (modules) => {
261295
const line = '-'.repeat(80);
262296
return modules.map((module) => {

0 commit comments

Comments
 (0)
Please sign in to comment.