1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const path_1 = tslib_1.__importDefault(require("path"));
|
5 | const rollup_plugin_babel_1 = tslib_1.__importDefault(require("rollup-plugin-babel"));
|
6 | const plugin_commonjs_1 = tslib_1.__importDefault(require("@rollup/plugin-commonjs"));
|
7 | const plugin_node_resolve_1 = tslib_1.__importDefault(require("@rollup/plugin-node-resolve"));
|
8 | const typescript_1 = tslib_1.__importDefault(require("typescript"));
|
9 | const componentTargets_1 = tslib_1.__importStar(require("./componentTargets"));
|
10 | const diagnostics_1 = require("./diagnostics");
|
11 | const rollupToVinyl_1 = tslib_1.__importDefault(require("./rollupToVinyl"));
|
12 | const forbidAbsoluteImport_1 = tslib_1.__importDefault(require("./plugins/forbidAbsoluteImport"));
|
13 | const i18nPolyfill_1 = tslib_1.__importDefault(require("./plugins/i18nPolyfill"));
|
14 | const platformExternals = tslib_1.__importStar(require("./plugins/platformExternals"));
|
15 | const polyfill_1 = tslib_1.__importDefault(require("./plugins/polyfill"));
|
16 | const polyfillDevice_1 = tslib_1.__importDefault(require("./plugins/polyfillDevice"));
|
17 | const resourceImports_1 = tslib_1.__importDefault(require("./plugins/resourceImports"));
|
18 | const workaroundRequireScope_1 = tslib_1.__importDefault(require("./plugins/workaroundRequireScope"));
|
19 | const terser_1 = tslib_1.__importDefault(require("./plugins/terser"));
|
20 | const typescript_2 = tslib_1.__importDefault(require("./plugins/typescript"));
|
21 | const rollupWarningHandler_1 = tslib_1.__importDefault(require("./rollupWarningHandler"));
|
22 | const tsconfigOverrides = {
|
23 | noEmitHelpers: false,
|
24 | importHelpers: true,
|
25 | noResolve: false,
|
26 | noEmit: false,
|
27 | inlineSourceMap: false,
|
28 | moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs,
|
29 | module: typescript_1.default.ModuleKind.ESNext,
|
30 | suppressOutputPathCheck: true,
|
31 | };
|
32 | function pluginIf(condition, plugin) {
|
33 | return condition ? [plugin()] : [];
|
34 | }
|
35 | const deviceModulesWithoutDefaultExports = new Set([
|
36 | 'crypto',
|
37 | 'document',
|
38 | 'fs',
|
39 | 'jpeg',
|
40 | 'power',
|
41 | 'scientific',
|
42 | 'scientific/signal',
|
43 | 'system',
|
44 | 'user-activity',
|
45 | 'user-settings',
|
46 | ]);
|
47 | function compile({ component, entryPoint, outputDir, defaultLanguage, allowUnknownExternals = false, onDiagnostic = diagnostics_1.logDiagnosticToConsole, }) {
|
48 | const ecma = component !== componentTargets_1.ComponentType.DEVICE ? 6 : 5;
|
49 | const { translationsGlob } = componentTargets_1.default[component];
|
50 | return rollupToVinyl_1.default({
|
51 | input: entryPoint,
|
52 | plugins: [
|
53 | ...pluginIf(component === componentTargets_1.ComponentType.DEVICE, polyfillDevice_1.default),
|
54 | ...pluginIf(component !== componentTargets_1.ComponentType.DEVICE, () => polyfill_1.default(i18nPolyfill_1.default(translationsGlob, defaultLanguage))),
|
55 | platformExternals.plugin(component),
|
56 | typescript_2.default({
|
57 | onDiagnostic,
|
58 | tsconfigOverride: Object.assign(Object.assign({}, tsconfigOverrides), { target: ecma === 6 ? typescript_1.default.ScriptTarget.ES2015 : typescript_1.default.ScriptTarget.ES5 }),
|
59 | tsconfigSearchPath: path_1.default.dirname(entryPoint),
|
60 | }),
|
61 | ...pluginIf(component === componentTargets_1.ComponentType.SETTINGS, resourceImports_1.default),
|
62 | forbidAbsoluteImport_1.default(),
|
63 | plugin_node_resolve_1.default({ preferBuiltins: false }),
|
64 | plugin_commonjs_1.default({ include: ['node_modules/**'] }),
|
65 | ...pluginIf(ecma === 5, () => rollup_plugin_babel_1.default({
|
66 | plugins: [
|
67 | require('@babel/plugin-transform-block-scoped-functions'),
|
68 | require('@babel/plugin-transform-block-scoping'),
|
69 | require('@babel/plugin-syntax-dynamic-import'),
|
70 | ],
|
71 | compact: false,
|
72 | babelrc: false,
|
73 | extensions: ['.js', '.json'],
|
74 | inputSourceMap: false,
|
75 | })),
|
76 | workaroundRequireScope_1.default(),
|
77 | terser_1.default({
|
78 | ecma,
|
79 | safari10: component !== componentTargets_1.ComponentType.DEVICE,
|
80 | mangle: {
|
81 | toplevel: true,
|
82 | },
|
83 | output: {
|
84 | semicolons: false,
|
85 | },
|
86 | compress: {
|
87 | toplevel: true,
|
88 | arrows: component !== componentTargets_1.ComponentType.DEVICE,
|
89 | keep_infinity: component !== componentTargets_1.ComponentType.DEVICE,
|
90 | passes: 2,
|
91 | },
|
92 | }),
|
93 | ],
|
94 | onwarn: rollupWarningHandler_1.default({
|
95 | onDiagnostic,
|
96 | codeCategories: allowUnknownExternals
|
97 | ? { UNRESOLVED_IMPORT: diagnostics_1.DiagnosticCategory.Warning }
|
98 | : undefined,
|
99 | }),
|
100 | inlineDynamicImports: component !== componentTargets_1.ComponentType.DEVICE,
|
101 | }, {
|
102 | dir: outputDir,
|
103 | file: outputDir === undefined ? `${component}.js` : undefined,
|
104 | format: 'cjs',
|
105 | sourcemap: true,
|
106 | sourcemapPathTransform: (mapPath) => path_1.default.normalize(outputDir === undefined ? mapPath : path_1.default.join(outputDir, mapPath)),
|
107 | interop: (id) => {
|
108 | if (id === null) {
|
109 | return 'auto';
|
110 | }
|
111 | if (platformExternals.externals[component].indexOf(id) === -1) {
|
112 | return 'auto';
|
113 | }
|
114 | if (component === componentTargets_1.ComponentType.DEVICE &&
|
115 | deviceModulesWithoutDefaultExports.has(id)) {
|
116 | return 'auto';
|
117 | }
|
118 | return 'esModule';
|
119 | },
|
120 | });
|
121 | }
|
122 | exports.default = compile;
|