Skip to content

Commit becad7b

Browse files
committed
Cut out all of the unused ace editor modes; significantly reduces bundle size
1 parent e9f553d commit becad7b

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

resources/scripts/components/elements/AceEditor.tsx

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import ace, { Editor } from 'brace';
33
import styled from 'styled-components/macro';
44
import tw from 'twin.macro';
55
import Select from '@/components/elements/Select';
6+
// @ts-ignore
7+
import modes from '@/modes';
68

79
// @ts-ignore
810
require('brace/ext/modelist');
@@ -18,32 +20,6 @@ const EditorContainer = styled.div`
1820
}
1921
`;
2022

21-
const modes: { [k: string]: string } = {
22-
assembly_x86: 'Assembly (x86)',
23-
c_cpp: 'C++',
24-
coffee: 'Coffeescript',
25-
css: 'CSS',
26-
dockerfile: 'Dockerfile',
27-
golang: 'Go',
28-
html: 'HTML',
29-
ini: 'Ini',
30-
java: 'Java',
31-
javascript: 'Javascript',
32-
json: 'JSON',
33-
kotlin: 'Kotlin',
34-
lua: 'Luascript',
35-
perl: 'Perl',
36-
php: 'PHP',
37-
properties: 'Properties',
38-
python: 'Python',
39-
ruby: 'Ruby',
40-
plain_text: 'Plaintext',
41-
toml: 'TOML',
42-
typescript: 'Typescript',
43-
xml: 'XML',
44-
yaml: 'YAML',
45-
};
46-
4723
Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
4824

4925
export interface Props {
@@ -117,7 +93,7 @@ export default ({ style, initialContent, initialModePath, fetchContent, onConten
11793
>
11894
{
11995
Object.keys(modes).map(key => (
120-
<option key={key} value={key}>{modes[key]}</option>
96+
<option key={key} value={key}>{(modes as { [k: string]: string })[key]}</option>
12197
))
12298
}
12399
</Select>

resources/scripts/modes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
assembly_x86: 'Assembly (x86)',
3+
c_cpp: 'C++',
4+
coffee: 'Coffeescript',
5+
css: 'CSS',
6+
dockerfile: 'Dockerfile',
7+
golang: 'Go',
8+
html: 'HTML',
9+
ini: 'Ini',
10+
java: 'Java',
11+
javascript: 'Javascript',
12+
json: 'JSON',
13+
kotlin: 'Kotlin',
14+
lua: 'Luascript',
15+
perl: 'Perl',
16+
php: 'PHP',
17+
properties: 'Properties',
18+
python: 'Python',
19+
ruby: 'Ruby',
20+
plain_text: 'Plaintext',
21+
toml: 'TOML',
22+
typescript: 'Typescript',
23+
xml: 'XML',
24+
yaml: 'YAML',
25+
};

webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const path = require('path');
2+
const webpack = require('webpack');
23
const AssetsManifestPlugin = require('webpack-assets-manifest');
34
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
45
const TerserPlugin = require('terser-webpack-plugin');
56
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
67

78
const isProduction = process.env.NODE_ENV === 'production';
9+
const modes = Object.keys(require('./resources/scripts/modes'));
810

911
module.exports = {
1012
cache: true,
@@ -72,6 +74,7 @@ module.exports = {
7274
moment: 'moment',
7375
},
7476
plugins: [
77+
new webpack.ContextReplacementPlugin(/brace[/\\](mode|worker)/, new RegExp(`^\.\/(${modes.join('|')})$`)),
7578
new AssetsManifestPlugin({ writeToDisk: true, publicPath: true, integrity: true, integrityHashes: ['sha384'] }),
7679
!isProduction ? new ForkTsCheckerWebpackPlugin({
7780
eslint: {

0 commit comments

Comments
 (0)