-
Notifications
You must be signed in to change notification settings - Fork 789
/
bundle-output.ts
177 lines (163 loc) · 6.38 KB
/
bundle-output.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import rollupCommonjsPlugin from '@rollup/plugin-commonjs';
import rollupJsonPlugin from '@rollup/plugin-json';
import rollupNodeResolvePlugin from '@rollup/plugin-node-resolve';
import rollupReplacePlugin from '@rollup/plugin-replace';
import { createOnWarnFn, isString, loadRollupDiagnostics } from '@utils';
import { PluginContext, rollup, RollupOptions, TreeshakingOptions } from 'rollup';
import type * as d from '../../declarations';
import { lazyComponentPlugin } from '../output-targets/dist-lazy/lazy-component-plugin';
import { createCustomResolverAsync } from '../sys/resolve/resolve-module-async';
import { appDataPlugin } from './app-data-plugin';
import type { BundleOptions } from './bundle-interface';
import { coreResolvePlugin } from './core-resolve-plugin';
import { devNodeModuleResolveId } from './dev-node-module-resolve';
import { extFormatPlugin } from './ext-format-plugin';
import { extTransformsPlugin } from './ext-transforms-plugin';
import { fileLoadPlugin } from './file-load-plugin';
import { loaderPlugin } from './loader-plugin';
import { pluginHelper } from './plugin-helper';
import { serverPlugin } from './server-plugin';
import { resolveIdWithTypeScript, typescriptPlugin } from './typescript-plugin';
import { userIndexPlugin } from './user-index-plugin';
import { workerPlugin } from './worker-plugin';
export const bundleOutput = async (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
bundleOpts: BundleOptions,
) => {
try {
const rollupOptions = getRollupOptions(config, compilerCtx, buildCtx, bundleOpts);
const rollupBuild = await rollup(rollupOptions);
compilerCtx.rollupCache.set(bundleOpts.id, rollupBuild.cache);
return rollupBuild;
} catch (e: any) {
if (!buildCtx.hasError) {
// TODO(STENCIL-353): Implement a type guard that balances using our own copy of Rollup types (which are
// breakable) and type safety (so that the error variable may be something other than `any`)
loadRollupDiagnostics(config, compilerCtx, buildCtx, e);
}
}
return undefined;
};
/**
* Build the rollup options that will be used to transpile, minify, and otherwise transform a Stencil project
* @param config the Stencil configuration for the project
* @param compilerCtx the current compiler context
* @param buildCtx a context object containing information about the current build
* @param bundleOpts Rollup bundling options to apply to the base configuration setup by this function
* @returns the rollup options to be used
*/
export const getRollupOptions = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
bundleOpts: BundleOptions,
): RollupOptions => {
const customResolveOptions = createCustomResolverAsync(config.sys, compilerCtx.fs, [
'.tsx',
'.ts',
'.js',
'.mjs',
'.json',
'.d.ts',
]);
const nodeResolvePlugin = rollupNodeResolvePlugin({
mainFields: ['collection:main', 'jsnext:main', 'es2017', 'es2015', 'module', 'main'],
customResolveOptions,
browser: true,
rootDir: config.rootDir,
...(config.nodeResolve as any),
});
const orgNodeResolveId = nodeResolvePlugin.resolveId;
const orgNodeResolveId2 = (nodeResolvePlugin.resolveId = async function (importee: string, importer: string) {
const [realImportee, query] = importee.split('?');
const resolved = await orgNodeResolveId.call(
nodeResolvePlugin as unknown as PluginContext,
realImportee,
importer,
{},
);
if (resolved) {
if (isString(resolved)) {
return query ? resolved + '?' + query : resolved;
}
return {
...resolved,
id: query ? resolved.id + '?' + query : resolved.id,
};
}
return resolved;
});
if (config.devServer?.experimentalDevModules) {
nodeResolvePlugin.resolveId = async function (importee: string, importer: string) {
const resolvedId = await orgNodeResolveId2.call(
nodeResolvePlugin as unknown as PluginContext,
importee,
importer,
);
return devNodeModuleResolveId(config, compilerCtx.fs, resolvedId, importee);
};
}
const beforePlugins = config.rollupPlugins.before || [];
const afterPlugins = config.rollupPlugins.after || [];
const rollupOptions: RollupOptions = {
input: bundleOpts.inputs,
output: {
inlineDynamicImports: bundleOpts.inlineDynamicImports ?? false,
},
plugins: [
coreResolvePlugin(config, compilerCtx, bundleOpts.platform, !!bundleOpts.externalRuntime),
appDataPlugin(config, compilerCtx, buildCtx, bundleOpts.conditionals, bundleOpts.platform),
lazyComponentPlugin(buildCtx),
loaderPlugin(bundleOpts.loader),
userIndexPlugin(config, compilerCtx),
typescriptPlugin(compilerCtx, bundleOpts, config),
extFormatPlugin(config),
extTransformsPlugin(config, compilerCtx, buildCtx),
workerPlugin(config, compilerCtx, buildCtx, bundleOpts.platform, !!bundleOpts.inlineWorkers),
serverPlugin(config, bundleOpts.platform),
...beforePlugins,
nodeResolvePlugin,
resolveIdWithTypeScript(config, compilerCtx),
rollupCommonjsPlugin({
include: /node_modules/,
sourceMap: config.sourceMap,
transformMixedEsModules: false,
...config.commonjs,
}),
...afterPlugins,
pluginHelper(config, buildCtx, bundleOpts.platform),
rollupJsonPlugin({
preferConst: true,
}),
rollupReplacePlugin({
'process.env.NODE_ENV': config.devMode ? '"development"' : '"production"',
preventAssignment: true,
}),
fileLoadPlugin(compilerCtx.fs),
],
treeshake: getTreeshakeOption(config, bundleOpts),
preserveEntrySignatures: bundleOpts.preserveEntrySignatures ?? 'strict',
onwarn: createOnWarnFn(buildCtx.diagnostics),
cache: compilerCtx.rollupCache.get(bundleOpts.id),
external: config.rollupConfig.inputOptions.external,
};
return rollupOptions;
};
const getTreeshakeOption = (config: d.ValidatedConfig, bundleOpts: BundleOptions): TreeshakingOptions | boolean => {
if (bundleOpts.platform === 'hydrate') {
return {
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
};
}
const treeshake =
!config.devMode && config.rollupConfig.inputOptions.treeshake !== false
? {
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
}
: false;
return treeshake;
};