forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle-output.ts
More file actions
211 lines (191 loc) · 7.51 KB
/
Copy pathbundle-output.ts
File metadata and controls
211 lines (191 loc) · 7.51 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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 { type ObjectHook, PluginContext, rollup, RollupOptions, TreeshakingOptions } from 'rollup';
import type * as d from '../../declarations';
import { lazyComponentPlugin } from '../output-targets/dist-lazy/lazy-component-plugin';
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 nodeResolvePlugin = rollupNodeResolvePlugin({
mainFields: ['collection:main', 'jsnext:main', 'es2017', 'es2015', 'module', 'main'],
browser: bundleOpts.platform !== 'hydrate',
rootDir: config.rootDir,
exportConditions: ['default', 'module', 'import', 'require'],
extensions: ['.tsx', '.ts', '.mts', '.cts', '.js', '.mjs', '.cjs', '.json', '.d.ts', '.d.mts', '.d.cts'],
...config.nodeResolve,
});
// @ts-expect-error - this is required now.
nodeResolvePlugin.resolve = async function () {
// Investigate if we can use this to leverage Stencil's in-memory fs
};
// @ts-expect-error - this is required now.
nodeResolvePlugin.warn = (log) => {
const onWarn = createOnWarnFn(buildCtx.diagnostics);
if (typeof log === 'string') {
onWarn({ message: log });
} else if (typeof log === 'function') {
const result = log();
if (typeof result === 'string') {
onWarn({ message: result });
} else {
onWarn(result);
}
} else {
onWarn(log);
}
};
assertIsObjectHook(nodeResolvePlugin.resolveId);
// remove default 'post' order
nodeResolvePlugin.resolveId.order = null;
const orgNodeResolveId = nodeResolvePlugin.resolveId.handler;
const orgNodeResolveId2 = (nodeResolvePlugin.resolveId.handler = async function (importee: string, importer: string) {
const [realImportee, query] = importee.split('?');
const resolved = await orgNodeResolveId.call(
nodeResolvePlugin as unknown as PluginContext,
realImportee,
importer,
{
attributes: {},
isEntry: true,
},
);
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,
bundleOpts.conditionals?.lazyLoad ?? false,
),
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,
maxParallelFileOps: config.rollupConfig.inputOptions.maxParallelFileOps,
};
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;
};
function assertIsObjectHook<T>(hook: ObjectHook<T>): asserts hook is { handler: T; order?: 'pre' | 'post' | null } {
if (typeof hook !== 'object') throw new Error(`expected the rollup plugin hook ${hook} to be an object`);
}