-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathjavascript.ts
More file actions
executable file
·60 lines (50 loc) · 1.99 KB
/
javascript.ts
File metadata and controls
executable file
·60 lines (50 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import * as babel from '@babel/core';
import babelClassPropertiesPlugin from '@babel/plugin-proposal-class-properties';
import babelObjectRestSpreadPlugin from '@babel/plugin-proposal-object-rest-spread';
import lwcClassTransformPlugin from '@lwc/babel-plugin-component';
import { normalizeToCompilerError, TransformerErrors } from '@lwc/errors';
import { NormalizedTransformOptions } from '../options';
import { TransformResult } from './transformer';
export default function scriptTransform(
code: string,
filename: string,
options: NormalizedTransformOptions
): TransformResult {
const {
isExplicitImport,
experimentalDynamicComponent: dynamicImports,
outputConfig: { sourcemap },
} = options;
let result;
try {
result = babel.transformSync(code, {
filename,
sourceMaps: sourcemap,
// Prevent Babel from loading local configuration.
babelrc: false,
configFile: false,
// Force Babel to generate new line and white spaces. This prevent Babel from generating
// an error when the generated code is over 500KB.
compact: false,
plugins: [
[lwcClassTransformPlugin, { isExplicitImport, dynamicImports }],
[babelClassPropertiesPlugin, { loose: true }],
// This plugin should be removed in a future version. The object-rest-spread is
// already a stage 4 feature. The LWC compile should leave this syntax untouched.
babelObjectRestSpreadPlugin,
],
})!;
} catch (e) {
throw normalizeToCompilerError(TransformerErrors.JS_TRANSFORMER_ERROR, e, { filename });
}
return {
code: result.code!,
map: result.map,
};
}