Skip to content

Commit

Permalink
Bundle code with vite/rollup for static serving
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Mar 20, 2024
1 parent 4c835b8 commit 4b93f8c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/generator-langium/templates/web/.package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"scripts": {
"build:web": "npm run build",
"bundle": "vite build",
"bundle:serve": "http-server ./dist --port 5175",
"dev": "vite",
"dev:debug": "vite --debug --force",
"serve": "npm run dev"
Expand All @@ -15,6 +17,7 @@
},
"devDependencies": {
"@codingame/esbuild-import-meta-url-plugin": "~1.0.2",
"vite": "~5.1.6"
"vite": "~5.1.6",
"http-server": "~14.1.1"
}
}
6 changes: 4 additions & 2 deletions packages/generator-langium/templates/web/src/setupCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export const configureMonacoWorkers = () => {
};

export const configureWorker = (): LanguageClientConfig => {
const workerURL = new URL('./language/main-browser.ts', import.meta.url);
const workerURL = new URL('./language/main-browser', import.meta.url);
console.log(`Using the following worker URL: ${workerURL.href}`);
const lsWorker = new Worker(workerURL.href, {

// vite does not extract the worker properly if it is in the workerURL variable
const lsWorker = new Worker(new URL('./language/main-browser', import.meta.url), {
type: 'module',
name: '<%= LanguageName %> Language Server'
});
Expand Down
6 changes: 2 additions & 4 deletions packages/generator-langium/templates/web/src/setupExtended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { configureWorker, defineUserServices } from './setupCommon.js';

export const setupConfigExtended = (): UserConfig => {
const extensionFilesOrContents = new Map();
const languageConfigUrl = new URL('../language-configuration.json', window.location.href);
const textmateConfigUrl = new URL('../syntaxes/<%= language-id %>.tmLanguage.json', window.location.href);
extensionFilesOrContents.set('/language-configuration.json', languageConfigUrl);
extensionFilesOrContents.set('/<%= language-id %>-grammar.json', textmateConfigUrl);
extensionFilesOrContents.set('/language-configuration.json', new URL('../language-configuration.json', import.meta.url));
extensionFilesOrContents.set('/<%= language-id %>-grammar.json', new URL('../syntaxes/<%= language-id %>.tmLanguage.json', import.meta.url));

return {
wrapperConfig: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ <h1><%= RawLanguageName %> in Langium</h1>
<!-- Monaco Configuration -->
<script type="module">
// vite is used to resolve the included TypeScript files
import { configureMonacoWorkers } from '../src/setupCommon.ts';
import { executeClassic } from '../src/setupClassic.ts';
import { configureMonacoWorkers } from '../src/setupCommon';
import { executeClassic } from '../src/setupClassic';

configureMonacoWorkers();
// keep a reference to a promise for when the editor is finished starting, we'll use this to setup the canvas on load
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ <h1><%= RawLanguageName %> in Langium</h1>
<!-- Monaco Configuration -->
<script type="module">
// vite is used to resolve the included TypeScript files
import { configureMonacoWorkers } from '../src/setupCommon.ts';
import { executeExtended } from '../src/setupExtended.ts';
import { configureMonacoWorkers } from '../src/setupCommon';
import { executeExtended } from '../src/setupExtended';

configureMonacoWorkers();
// keep a reference to a promise for when the editor is finished starting, we'll use this to setup the canvas on load
Expand Down
8 changes: 6 additions & 2 deletions packages/generator-langium/templates/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default defineConfig(() => {
target: 'esnext',
rollupOptions: {
input: {
monacoClassic: path.resolve(__dirname, '/static/monacoClassic.html'),
monacoExtended: path.resolve(__dirname, '/static/monacoExtended.html'),
index: path.resolve(__dirname, 'index.html'),
monacoClassic: path.resolve(__dirname, 'static/monacoClassic.html'),
monacoExtended: path.resolve(__dirname, 'static/monacoExtended.html'),
}
}
},
Expand All @@ -23,6 +24,9 @@ export default defineConfig(() => {
importMetaUrlPlugin
]
}
},
server: {
port: 5173
}
};
return config;
Expand Down

0 comments on commit 4b93f8c

Please sign in to comment.