UNPKG

2.83 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2018, salesforce.com, inc.
3 * All rights reserved.
4 * SPDX-License-Identifier: MIT
5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6 */
7const fs = require('fs');
8const path = require('path');
9
10const DEBUG_SUFFIX = '_debug';
11const PROD_SUFFIX = '.min';
12
13function 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
20const MODULES = new Set(['engine', 'synthetic-shadow', 'wire-service']);
21const FORMATS = new Set(['iife', 'umd', 'esm']);
22const TARGETS = new Set(['es5', 'es2017']);
23const MODES = new Set(['dev', 'prod', 'prod_debug']);
24
25const DEFAULT_FORMAT = 'esm';
26const DEFAULT_TARGET = 'es2017';
27const DEFAULT_MODE = 'dev';
28
29function 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
54function 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
59function 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 // The default is on the package itself (naming convention is set)
68 if (format === DEFAULT_FORMAT && target === DEFAULT_TARGET && mode === DEFAULT_MODE) {
69 distPath = require.resolve(`@lwc/${name}/dist/${name}.js`);
70
71 // Otherwise is on dist of this package
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
87module.exports = {
88 getVersion,
89 getModule,
90 getModulePath,
91};