Skip to content

Commit 5dbb5ff

Browse files
authored
Eliminate all usages of require() in the client (compiler-explorer#7796)
1 parent 93e31a5 commit 5dbb5ff

6 files changed

Lines changed: 54 additions & 17 deletions

File tree

static/client.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, Compiler Explorer Authors
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright
10+
// notice, this list of conditions and the following disclaimer in the
11+
// documentation and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+
// POSSIBILITY OF SUCH DAMAGE.
24+
25+
declare module '*.pug' {
26+
const content: any;
27+
export default content;
28+
}
29+
30+
declare module 'lodash.clonedeep' {
31+
const cloneDeep: <T>(value: T) => T;
32+
export = cloneDeep;
33+
}

static/main.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,22 @@ import {setupRealDark, takeUsersOutOfRealDark} from './real-dark.js';
7070
import {formatISODate, updateAndCalcTopBarHeight} from './utils.js';
7171

7272
const logos = require.context('../views/resources/logos', false, /\.(png|svg)$/);
73-
7473
const siteTemplateScreenshots = require.context('../views/resources/template_screenshots', false, /\.png$/);
74+
import changelogDocument from './generated/changelog.pug';
75+
import cookiesDocument from './generated/cookies.pug';
76+
import privacyDocument from './generated/privacy.pug';
7577

7678
if (!window.PRODUCTION && !options.embedded) {
79+
// TODO: Replace with top-level await import() when we move to Vite
7780
require('./tests/_all');
7881
}
7982

8083
//css
81-
require('bootstrap/dist/css/bootstrap.min.css');
82-
require('golden-layout/src/css/goldenlayout-base.css');
83-
require('tom-select/dist/css/tom-select.bootstrap5.css');
84-
require('./styles/colours.scss');
85-
require('./styles/explorer.scss');
84+
import 'bootstrap/dist/css/bootstrap.min.css';
85+
import 'golden-layout/src/css/goldenlayout-base.css';
86+
import 'tom-select/dist/css/tom-select.bootstrap5.css';
87+
import './styles/colours.scss';
88+
import './styles/explorer.scss';
8689

8790
// Check to see if the current unload is a UI reset.
8891
// Forgive me the global usage here
@@ -91,8 +94,8 @@ const simpleCooks = new SimpleCook();
9194
const historyWidget = new HistoryWidget();
9295

9396
const policyDocuments = {
94-
cookies: require('./generated/cookies.pug').default,
95-
privacy: require('./generated/privacy.pug').default,
97+
cookies: cookiesDocument,
98+
privacy: privacyDocument,
9699
};
97100

98101
function setupSettings(hub: Hub): [Themer, SiteSettings] {
@@ -193,7 +196,7 @@ function setupButtons(options: CompilerExplorerOptions, hub: Hub) {
193196

194197
$('#changes').on('click', () => {
195198
// TODO(jeremy-rifkin): Fix types
196-
alertSystem.alert('Changelog', $(require('./generated/changelog.pug').default.text) as any);
199+
alertSystem.alert('Changelog', changelogDocument.text);
197200
});
198201

199202
$.get(window.location.origin + window.httpRoot + 'bits/icons.html')

static/multifile-service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ import {FiledataPair} from '../types/compilation/compilation.interfaces.js';
3030
import {unwrap} from './assert.js';
3131
import {Hub} from './hub.js';
3232
import {LanguageKey} from './languages.interfaces.js';
33+
import * as options from './options.js';
3334
import {Alert} from './widgets/alert.js';
34-
const languages = require('./options').options.languages;
35+
36+
const languages = options.options.languages;
3537

3638
export interface MultifileFile {
3739
fileId: number;

static/sharing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import * as url from './url.js';
3636
import {SentryCapture} from './sentry.js';
3737
import {Settings, SiteSettings} from './settings.js';
3838
import ClickEvent = JQuery.ClickEvent;
39-
40-
const cloneDeep = require('lodash.clonedeep');
39+
import cloneDeep from 'lodash.clonedeep';
4140

4241
enum LinkType {
4342
Short = 0,

static/url.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

2525
import GoldenLayout from 'golden-layout';
26+
import lzstring from 'lz-string';
2627
import _ from 'underscore';
27-
28-
const lzstring = require('lz-string');
29-
const Components = require('./components');
28+
import * as Components from './components.js';
3029

3130
import * as rison from './rison.js';
3231

@@ -47,6 +46,8 @@ export function convertOldState(state: any): any {
4746
};
4847
const filters = _.clone(state.filterAsm);
4948
delete filters.colouriseAsm;
49+
// TODO(junlarsen): find the missing language field here
50+
// @ts-expect-error: this is missing the language field, which was never noticed because the import was untyped
5051
content.push(Components.getEditorWith(1, source, options));
5152
content.push(Components.getCompilerWith(1, filters, sc.options, sc.compiler));
5253
return {version: 4, content: [{type: 'row', content: content}]};

static/widgets/load-save.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ import {SourceApiEntry} from '../../types/source.interfaces.js';
3131
import {unwrap, unwrapString} from '../assert.js';
3232
import * as BootstrapUtils from '../bootstrap-utils.js';
3333
import {HistorySource} from '../history.js';
34+
import * as history from '../history.js';
3435
import {localStorage} from '../local.js';
3536
import {Alert} from './alert.js';
3637

37-
const history = require('../history');
38-
3938
type PopulateItem = {name: string; load: () => void; delete?: () => void; overwrite?: () => void};
4039

4140
export class LoadSave {

0 commit comments

Comments
 (0)