@vyr/pack-auto-provider-plugin 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +38 -0
  2. package/loader.js +53 -0
  3. package/package.json +1 -0
package/index.js ADDED
@@ -0,0 +1,38 @@
1
+ const { resolve, join } = require("path")
2
+
3
+ class AutoProviderPlugin {
4
+
5
+ constructor(entrys) {
6
+ this.entrys = entrys.map(entry => join(process.cwd(), entry))
7
+ }
8
+
9
+ validate(path) {
10
+ for (const entry of this.entrys) {
11
+ if (path.indexOf(entry) > -1) return true
12
+ }
13
+
14
+ return false
15
+ }
16
+
17
+ provider(module) {
18
+ const custom = {
19
+ loader: resolve(__dirname, './loader'),
20
+ options: JSON.stringify({})
21
+ }
22
+ module.loaders.push(custom)
23
+ }
24
+
25
+ compiler(module) {
26
+ if (!module.request) return
27
+ if (this.validate(module.request) === false) return
28
+ this.provider(module)
29
+ }
30
+
31
+ apply(compiler) {
32
+ compiler.hooks.compilation.tap('auto-provider-plugin', (compilation) => {
33
+ compilation.hooks.buildModule.tap('auto-provider-plugin', (module) => this.compiler(module))
34
+ })
35
+ }
36
+ }
37
+
38
+ module.exports = AutoProviderPlugin
package/loader.js ADDED
@@ -0,0 +1,53 @@
1
+ const editorConfig = require('../../editor.config.json')
2
+
3
+ const extensions = [
4
+ editorConfig.browser.runtime,
5
+ ...editorConfig.browser.extensions,
6
+ ...editorConfig.browser.remote,
7
+ ...editorConfig.browser.services.map(service => service.name),
8
+ ]
9
+
10
+ module.exports = function (source) {
11
+ const options = this.getOptions()
12
+
13
+ const imports = [
14
+ `import axios from 'axios'`,
15
+ `import { Locale } from '@vyr/locale'`,
16
+ `const libs = [`,
17
+ ]
18
+ for (let i = 0; i < extensions.length; i++) {
19
+ const extension = extensions[i]
20
+ imports.push(` { name:'__VYR_RUNTIME_${extension}',load:async () => import('${extension}')},`)
21
+ }
22
+ imports.push(`]`)
23
+
24
+ const codes = [
25
+ `const getConfig = async()=> {`,
26
+ ` const res = await axios.get('/cli/getConfig')`,
27
+ ` return res.data`,
28
+ `}`,
29
+ `const extensions = {}`,
30
+ ]
31
+
32
+ codes.push(
33
+ `const loadExtensions = async () => {`,
34
+ ` const config = await getConfig()`,
35
+ ` for(const provider of config.languageProviders) Locale.register(provider)`,
36
+ ` Locale.use(config.language)`,
37
+ ` const tasks = []`,
38
+ ` for (const lib of libs) {`,
39
+ ` const task = lib.load().then(res => window[lib.name] = res)`,
40
+ ` tasks.push(task)`,
41
+ ` } `,
42
+ ` await Promise.all(tasks)`,
43
+ ` return config`,
44
+ `}`,
45
+ )
46
+
47
+ const code = source
48
+ .replace('//__VYR_IMPORT', imports.join('\n'))
49
+ .replace('//__VYR_DEFINE', codes.join('\n'))
50
+ .replace('//__VYR_CONFIG', `const config = await loadExtensions()`)
51
+
52
+ return code
53
+ }
package/package.json ADDED
@@ -0,0 +1 @@
1
+ {"name":"@vyr/pack-auto-provider-plugin","version":"0.0.4","description":"","main":"./index.js","author":"","license":"ISC"}