1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const fs = require('fs');
|
8 | const path = require('path');
|
9 |
|
10 | const DEBUG_SUFFIX = '_debug';
|
11 | const PROD_SUFFIX = '.min';
|
12 |
|
13 | function getVersion() {
|
14 | const pkgPath = path.join(__dirname, './package.json');
|
15 | const pkgSrc = fs.readFileSync(pkgPath, 'utf8');
|
16 | const pkg = JSON.parse(pkgSrc);
|
17 | return pkg.version;
|
18 | }
|
19 |
|
20 | const MODULES = new Set(['engine', 'synthetic-shadow', 'wire-service']);
|
21 | const FORMATS = new Set(['iife', 'umd', 'esm']);
|
22 | const TARGETS = new Set(['es5', 'es2017']);
|
23 | const MODES = new Set(['dev', 'prod', 'prod_debug']);
|
24 |
|
25 | const DEFAULT_FORMAT = 'esm';
|
26 | const DEFAULT_TARGET = 'es2017';
|
27 | const DEFAULT_MODE = 'dev';
|
28 |
|
29 | function validateArgs(name, format, target, mode) {
|
30 | if (!MODULES.has(name)) {
|
31 | throw new Error(
|
32 | `Invalid module name "${name}"` +
|
33 | `Available module names: ${Array.from(MODULES.keys())}`
|
34 | );
|
35 | }
|
36 |
|
37 | if (!FORMATS.has(format)) {
|
38 | throw new Error(
|
39 | `Invalid format "${name}"` + `Available formats: ${Array.from(FORMATS.keys())}`
|
40 | );
|
41 | }
|
42 |
|
43 | if (!TARGETS.has(target)) {
|
44 | throw new Error(
|
45 | `Invalid target "${target}"` + `Available targets: ${Array.from(TARGETS.keys())}`
|
46 | );
|
47 | }
|
48 |
|
49 | if (!MODES.has(mode)) {
|
50 | throw new Error(`Invalid mode "${mode}"` + `Available modes: ${Array.from(MODES.keys())}`);
|
51 | }
|
52 | }
|
53 |
|
54 | function getModule(name, format = DEFAULT_FORMAT, target = DEFAULT_TARGET, mode = DEFAULT_MODE) {
|
55 | const distPath = getModulePath(name, format, target, mode);
|
56 | return fs.readFileSync(distPath, 'utf8');
|
57 | }
|
58 |
|
59 | function getModulePath(
|
60 | name,
|
61 | format = DEFAULT_FORMAT,
|
62 | target = DEFAULT_TARGET,
|
63 | mode = DEFAULT_MODE
|
64 | ) {
|
65 | validateArgs(name, format, target, mode);
|
66 | let distPath;
|
67 |
|
68 | if (format === DEFAULT_FORMAT && target === DEFAULT_TARGET && mode === DEFAULT_MODE) {
|
69 | distPath = require.resolve(`@lwc/${name}/dist/${name}.js`);
|
70 |
|
71 |
|
72 | } else if (mode === 'prod') {
|
73 | distPath = path.join(__dirname, 'dist', name, format, target, `${name}${PROD_SUFFIX}.js`);
|
74 | } else if (mode === 'prod_debug') {
|
75 | distPath = path.join(__dirname, 'dist', name, format, target, `${name}${DEBUG_SUFFIX}.js`);
|
76 | } else {
|
77 | distPath = path.join(__dirname, 'dist', name, format, target, `${name}.js`);
|
78 | }
|
79 |
|
80 | if (!fs.existsSync(distPath)) {
|
81 | throw new Error(`Module path "${distPath}" for module ${name} not found`);
|
82 | }
|
83 |
|
84 | return distPath;
|
85 | }
|
86 |
|
87 | module.exports = {
|
88 | getVersion,
|
89 | getModule,
|
90 | getModulePath,
|
91 | };
|