UNPKG

2.82 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-dom', 'engine-server', '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}". Available module names: ${Array.from(MODULES.keys())}`
33 );
34 }
35
36 if (!FORMATS.has(format)) {
37 throw new Error(
38 `Invalid format "${name}". Available formats: ${Array.from(FORMATS.keys())}`
39 );
40 }
41
42 if (!TARGETS.has(target)) {
43 throw new Error(
44 `Invalid target "${target}". Available targets: ${Array.from(TARGETS.keys())}`
45 );
46 }
47
48 if (!MODES.has(mode)) {
49 throw new Error(`Invalid mode "${mode}". Available modes: ${Array.from(MODES.keys())}`);
50 }
51}
52
53function getModule(name, format = DEFAULT_FORMAT, target = DEFAULT_TARGET, mode = DEFAULT_MODE) {
54 const distPath = getModulePath(name, format, target, mode);
55 return fs.readFileSync(distPath, 'utf8');
56}
57
58function getModulePath(
59 name,
60 format = DEFAULT_FORMAT,
61 target = DEFAULT_TARGET,
62 mode = DEFAULT_MODE
63) {
64 validateArgs(name, format, target, mode);
65 let distPath;
66 // The default is on the package itself (naming convention is set)
67 if (format === DEFAULT_FORMAT && target === DEFAULT_TARGET && mode === DEFAULT_MODE) {
68 distPath = require.resolve(`@lwc/${name}/dist/${name}.js`);
69
70 // Otherwise is on dist of this package
71 } else if (mode === 'prod') {
72 distPath = path.join(__dirname, 'dist', name, format, target, `${name}${PROD_SUFFIX}.js`);
73 } else if (mode === 'prod_debug') {
74 distPath = path.join(__dirname, 'dist', name, format, target, `${name}${DEBUG_SUFFIX}.js`);
75 } else {
76 distPath = path.join(__dirname, 'dist', name, format, target, `${name}.js`);
77 }
78
79 if (!fs.existsSync(distPath)) {
80 throw new Error(`Module path "${distPath}" for module ${name} not found`);
81 }
82
83 return distPath;
84}
85
86module.exports = {
87 getVersion,
88 getModule,
89 getModulePath,
90};