Skip to content

Commit

Permalink
Merge pull request #91 from CodinGame/improve-asset-loading
Browse files Browse the repository at this point in the history
Improve asset loading
  • Loading branch information
CGNonofr authored Apr 6, 2023
2 parents 24f923e + c3b4088 commit abca957
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 91 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"rollup": "^3.20.2",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-external-assets": "^3.0.1",
"rollup-plugin-glob-import": "^0.5.0",
"rollup-plugin-styles": "^4.0.0",
"semver": "^7.3.8",
"ts-node": "^10.9.1",
Expand Down
40 changes: 10 additions & 30 deletions rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as babylonParser from 'recast/parsers/babylon.js'
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars'
import inject from '@rollup/plugin-inject'
import externalAssets from 'rollup-plugin-external-assets'
import globImport from 'rollup-plugin-glob-import'
import * as fs from 'fs'
import * as path from 'path'
import * as vm from 'vm'
Expand All @@ -36,7 +37,7 @@ const PURE_FUNCTIONS = new Set([
'darken',
'lighten',
'Color.fromHex',
'asBroswerUri',
'asBrowserUri',
'values',
'keys',
'toString',
Expand Down Expand Up @@ -201,6 +202,7 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
},
external,
output: [{
assetFileNames: 'assets/[name][extname]',
format: 'esm',
dir: 'dist',
entryFileNames: '[name].js',
Expand Down Expand Up @@ -389,37 +391,14 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
}),
// Create a require instance with a toUrl method (like in vscode) to load static resources (mp3, wasm...)
inject({
require: path.resolve('src/custom-require.js')
require: path.resolve('src/assets')
}),
{
name: 'vscode-resource-loading-plugin',
resolveId (id) {
if (id.endsWith('custom-require.js')) {
return id
}
return undefined
},
load (id) {
if (!id.endsWith('custom-require.js')) {
return
}
const sounds = fs.readdirSync(path.resolve(VSCODE_DIR, 'vs/platform/audioCues/browser/media/'))
const code = `
${sounds.map(sound => `import _${path.parse(sound).name} from 'vscode/vs/platform/audioCues/browser/media/${sound}'`).join('\n')}
import _onigWasm from 'vscode-oniguruma/release/onig.wasm'
const fileUrls = {
'vscode-oniguruma/../onig.wasm': _onigWasm,
${sounds.map(sound => ` 'vs/platform/audioCues/browser/media/${sound}': _${path.parse(sound).name}`).join(',\n')}
}
export default {
toUrl: (id) => fileUrls[id]
}
`
return code
globImport({
format: 'default',
rename (name, id) {
return path.relative(VSCODE_DIR, id).replace(/[/.]/g, '_')
}
},
}),
externalAssets(['**/*.mp3', '**/*.wasm']),
{
name: 'dynamic-import-polyfill',
Expand Down Expand Up @@ -447,6 +426,7 @@ export default {
external,
input: Object.values(input).map(f => `./dist/${path.basename(f, '.ts')}`),
output: [{
assetFileNames: 'assets/[name][extname]',
format: 'esm',
dir: 'dist',
entryFileNames: '[name].js',
Expand Down
18 changes: 18 additions & 0 deletions src/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

let assetUrls: Record<string, string> = {}
export function registerAssets (assets: Record<string, string>): void {
assetUrls = {
...assetUrls,
...assets
}
}

function toUrl (name: string): string | undefined {
return assetUrls[name] ?? assetUrls[name.replace(/[/.]/g, '_')]
}

const customRequire = {
toUrl
}

export default customRequire
Loading

0 comments on commit abca957

Please sign in to comment.