From f9084dd77948cb221d771babefe4840960ed8782 Mon Sep 17 00:00:00 2001 From: Andrei Fiodorov Date: Sun, 1 Oct 2023 12:22:15 +0200 Subject: [PATCH 1/4] feat: add possibility to get static modules --- src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.ts b/src/index.ts index 2a0f8c6..e5650b7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,6 +105,10 @@ class VirtualModulesPlugin { this._staticModules = modules || null; } + public getModules() { + return this._staticModules; + } + public writeModule(filePath: string, contents: string): void { if (!this._compiler) { throw new Error(`Plugin has not been initialized`); From 8f70584a538e425dcf7569212b8b2ee18394922f Mon Sep 17 00:00:00 2001 From: Andrei Fiodorov Date: Sun, 1 Oct 2023 12:23:06 +0200 Subject: [PATCH 2/4] feat: add test to check new added feature and fix ts errors --- src/__tests__/index.test.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index c7d9ff7..16019c2 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -21,12 +21,20 @@ describe('webpack-virtual-modules', () => { expect(() => plugin.writeModule('example.js', '')).not.toThrow(); }); + it('should return static modules', async () => { + const options = { 'static_module1.js': 'const foo;' }; + options[path.resolve('static_module2.js')] = 'const bar;'; + const plugin = new Plugin(options); + + expect(plugin.getModules()).toMatchObject(options); + }); + it('should write static modules to fs', async () => { const options = { 'static_module1.js': 'const foo;' }; options[path.resolve('static_module2.js')] = 'const bar;'; const plugin = new Plugin(options); - return new Promise((resolve) => { + return new Promise((resolve) => { webpack({ plugins: [plugin], entry: './entry.js', @@ -46,7 +54,7 @@ describe('webpack-virtual-modules', () => { 'static_module.js': 'const foo;', }); - return new Promise((resolve) => { + return new Promise((resolve) => { webpack({ plugins: [plugin], entry: './entry.js', @@ -64,7 +72,7 @@ describe('webpack-virtual-modules', () => { it('purge should work when no virtual files exist', async () => { const plugin = new Plugin(); - return new Promise((resolve) => { + return new Promise((resolve) => { webpack({ plugins: [plugin], entry: './entry.js', @@ -90,7 +98,7 @@ describe('webpack-virtual-modules', () => { entry: './entry.js', }); - return new Promise((resolve) => { + return new Promise((resolve) => { compiler.run((err, stats) => { if (!stats) throw err; @@ -198,7 +206,7 @@ describe('webpack-virtual-modules', () => { const fileSystem = new MemoryFileSystem(); compiler.outputFileSystem = fileSystem; - return new Promise((resolve) => { + return new Promise((resolve) => { compiler.run((err, stats) => { expect(stats).toBeDefined(); expect(err).toBeNull(); @@ -247,6 +255,7 @@ describe('webpack-virtual-modules', () => { // support mode: development // left for future testing and possibility of enabling test for it if (webpack.version && typeof webpack.version === 'string') { + // @ts-ignore TODO: It's enough? config.mode = 'development'; } const compiler = webpack(config); From 9f1c62abe3a3b40df16c996daf06c37b3a72ea90 Mon Sep 17 00:00:00 2001 From: Andrei Fiodorov Date: Sun, 1 Oct 2023 19:18:29 +0200 Subject: [PATCH 3/4] feat: refactor get modules method to return dynamic and static modules --- src/index.ts | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e5650b7..e3f71af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,11 @@ import { VirtualStats } from './virtual-stats'; import type { Compiler } from 'webpack'; let inode = 45000000; +const ALL = 'all'; +const STATIC = 'static'; +const DYNAMIC = 'dynamic'; + +type AvailableModules = typeof ALL | typeof STATIC | typeof DYNAMIC; function checkActivation(instance) { if (!instance._compiler) { @@ -105,8 +110,36 @@ class VirtualModulesPlugin { this._staticModules = modules || null; } - public getModules() { - return this._staticModules; + public getModuleList(filter: AvailableModules = ALL) { + let modules = {}; + const shouldGetStaticModules = filter === ALL || filter === STATIC; + const shouldGetDynamicModules = filter === ALL || filter === DYNAMIC; + + if (shouldGetStaticModules) { + // Get static modules + modules = { + ...modules, + ...this._staticModules, + }; + } + + if (shouldGetDynamicModules) { + // Get dynamic modules + const finalInputFileSystem: any = this._compiler?.inputFileSystem; + const virtualFiles = finalInputFileSystem?._virtualFiles ?? {}; + + const dynamicModules: Record = {}; + Object.keys(virtualFiles).forEach((key: string) => { + dynamicModules[key] = virtualFiles[key].contents; + }); + + modules = { + ...modules, + ...dynamicModules, + }; + } + + return modules; } public writeModule(filePath: string, contents: string): void { From 3253b6796bc93b5af14f84b30cdad458f53e77b6 Mon Sep 17 00:00:00 2001 From: Andrei Fiodorov Date: Sun, 1 Oct 2023 19:19:24 +0200 Subject: [PATCH 4/4] feat: add tests to check if getModuleList return all modules --- src/__tests__/index.test.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 16019c2..550ab32 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -26,7 +26,37 @@ describe('webpack-virtual-modules', () => { options[path.resolve('static_module2.js')] = 'const bar;'; const plugin = new Plugin(options); - expect(plugin.getModules()).toMatchObject(options); + expect(plugin.getModuleList()).toMatchObject(options); + }); + + it('should return dynamic and static modules', async () => { + const options = { 'static_module.js': 'const foo;' }; + const plugin = new Plugin(options); + + const compiler = webpack({ + plugins: [plugin], + entry: './entry.js', + }); + + return new Promise((resolve) => { + const watcher = compiler.watch({}, (err, stats) => { + if (!stats) throw err; + + plugin.writeModule('dynamic_module.js', 'const baz;'); + + const fs = stats.compilation.inputFileSystem as MemoryFileSystem; + fs.purge(); + + const finalOptions = { + [path.resolve('static_module.js')]: 'const foo;', + [path.resolve('dynamic_module.js')]: 'const baz;', + }; + + expect(plugin.getModuleList()).toMatchObject(finalOptions); + + watcher.close(resolve); + }); + }); }); it('should write static modules to fs', async () => {