|
| 1 | +const path = require('path') |
| 2 | +const VirtualModulesPlugin = require('webpack-virtual-modules') |
| 3 | + |
| 4 | +const isCodeFile = file => file.endsWith('.ts') || file.endsWith('.js') |
| 5 | +const IGNORED_FOLDERS = ['assets', '.dependencies'] |
| 6 | + |
| 7 | +const SCENE_INIT_CONTENT = ` |
| 8 | +import scene from './.expanse.json' |
| 9 | +
|
| 10 | +delete scene.history |
| 11 | +delete scene.historyVersion |
| 12 | +
|
| 13 | +window.ecs.application.init(scene) |
| 14 | +
|
| 15 | +if (module.hot) { |
| 16 | + const isInline = window.location.href.includes('liveSyncMode=inline') |
| 17 | +
|
| 18 | + const handler = isInline |
| 19 | + ? () => { } |
| 20 | + : async () => { |
| 21 | + const updatedScene = (await import('./.expanse.json')).default |
| 22 | +
|
| 23 | + delete updatedScene.history |
| 24 | + delete updatedScene.historyVersion |
| 25 | +
|
| 26 | + window.ecs.application.getScene().updateBaseObjects(updatedScene.objects) |
| 27 | + window.ecs.application.getScene().updateDebug(updatedScene) |
| 28 | + } |
| 29 | +
|
| 30 | + module.hot.accept('./.expanse.json', handler) |
| 31 | +}` |
| 32 | + |
| 33 | +const createVirtualEntryPlugin = (options = {}) => { |
| 34 | + const {srcDir} = options |
| 35 | + const virtualModules = new VirtualModulesPlugin() |
| 36 | + const importedFiles = new Set() |
| 37 | + const ignoredFoldersPaths = IGNORED_FOLDERS.map(folder => path.join(srcDir, folder)) |
| 38 | + |
| 39 | + let hasInit = false |
| 40 | + let watcher = null |
| 41 | + |
| 42 | + const removeImports = (files) => { |
| 43 | + files.forEach(file => importedFiles.delete(file)) |
| 44 | + } |
| 45 | + |
| 46 | + const updateVirtualModules = () => { |
| 47 | + const appFiles = Array.from(importedFiles).filter( |
| 48 | + file => file === path.join(srcDir, 'app.js') || file === path.join(srcDir, 'app.ts') |
| 49 | + ) |
| 50 | + const otherFiles = Array.from(importedFiles).filter( |
| 51 | + file => !appFiles.includes(file) |
| 52 | + ) |
| 53 | + |
| 54 | + const imports = [...appFiles, ...otherFiles] |
| 55 | + .map((file) => { |
| 56 | + const relativePath = path.relative(srcDir, file) |
| 57 | + return `import ${JSON.stringify(`./${relativePath.replace(/\\/g, '/')}`)}` |
| 58 | + }) |
| 59 | + .join('\n') |
| 60 | + |
| 61 | + const content = `${imports}\n${SCENE_INIT_CONTENT}` |
| 62 | + |
| 63 | + virtualModules.writeModule(path.join(srcDir, 'entry.js'), content) |
| 64 | + } |
| 65 | + |
| 66 | + const initFileWatcher = async () => { |
| 67 | + if (watcher) { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + const chokidar = await import('chokidar') |
| 73 | + watcher = chokidar.default.watch(srcDir, { |
| 74 | + ignored: filePath => ignoredFoldersPaths.some(folder => filePath.startsWith(folder)), |
| 75 | + persistent: true, |
| 76 | + }) |
| 77 | + |
| 78 | + watcher.on('add', (filePath) => { |
| 79 | + if (isCodeFile(filePath)) { |
| 80 | + importedFiles.add(filePath) |
| 81 | + updateVirtualModules() |
| 82 | + } |
| 83 | + }) |
| 84 | + } catch (err) { |
| 85 | + // chokidar not available, file watching disabled |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const closeFileWatcher = () => { |
| 90 | + if (watcher) { |
| 91 | + watcher.close() |
| 92 | + watcher = null |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const getCodeFiles = (fs, dir) => { |
| 97 | + const files = fs.readdirSync(dir) |
| 98 | + files.forEach((file) => { |
| 99 | + const fullPath = path.join(dir, file) |
| 100 | + try { |
| 101 | + if (fs.lstatSync(fullPath).isDirectory()) { |
| 102 | + if (!IGNORED_FOLDERS.includes(file)) { |
| 103 | + getCodeFiles(fs, fullPath) |
| 104 | + } |
| 105 | + } else if (isCodeFile(fullPath)) { |
| 106 | + importedFiles.add(fullPath) |
| 107 | + } |
| 108 | + } catch (err) { |
| 109 | + // ignore virtual files |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + apply: (compiler) => { |
| 116 | + compiler.hooks.watchRun.tapAsync('VirtualEntryPlugin', async (compilation, callback) => { |
| 117 | + const {removedFiles} = compilation |
| 118 | + |
| 119 | + if (!hasInit) { |
| 120 | + hasInit = true |
| 121 | + updateVirtualModules() |
| 122 | + await initFileWatcher() |
| 123 | + } |
| 124 | + |
| 125 | + if (removedFiles) { |
| 126 | + const removedCodeFiles = Array.from(removedFiles).filter(isCodeFile) |
| 127 | + if (removedCodeFiles.length > 0) { |
| 128 | + removeImports(removedCodeFiles) |
| 129 | + updateVirtualModules() |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + callback() |
| 134 | + }) |
| 135 | + |
| 136 | + compiler.hooks.shutdown.tap('VirtualEntryPlugin', () => { |
| 137 | + closeFileWatcher() |
| 138 | + }) |
| 139 | + |
| 140 | + compiler.hooks.beforeCompile.tapAsync('VirtualEntryPlugin', (_, callback) => { |
| 141 | + if (!compiler.watching && !hasInit) { |
| 142 | + getCodeFiles(compiler.inputFileSystem, srcDir) |
| 143 | + updateVirtualModules() |
| 144 | + hasInit = true |
| 145 | + } |
| 146 | + callback() |
| 147 | + }) |
| 148 | + |
| 149 | + virtualModules.apply(compiler) |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +module.exports = createVirtualEntryPlugin |
0 commit comments