/
transform.ts
174 lines (165 loc) · 5.18 KB
/
transform.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
import MagicString, { SourceMap } from 'magic-string';
import Module from '../Module';
import {
DecodedSourceMapOrMissing,
EmittedFile,
ExistingRawSourceMap,
Plugin,
PluginContext,
RollupError,
RollupWarning,
SourceDescription,
TransformModuleJSON,
TransformPluginContext,
TransformResult,
WarningHandler
} from '../rollup/types';
import { getTrackedPluginCache } from './PluginCache';
import { PluginDriver } from './PluginDriver';
import { collapseSourcemap } from './collapseSourcemaps';
import { decodedSourcemap } from './decodedSourcemap';
import { augmentCodeLocation, errNoTransformMapOrAstWithoutCode } from './error';
import { throwPluginError } from './pluginUtils';
export default async function transform(
source: SourceDescription,
module: Module,
pluginDriver: PluginDriver,
warn: WarningHandler
): Promise<TransformModuleJSON> {
const id = module.id;
const sourcemapChain: DecodedSourceMapOrMissing[] = [];
let originalSourcemap = source.map === null ? null : decodedSourcemap(source.map);
const originalCode = source.code;
let ast = source.ast;
const transformDependencies: string[] = [];
const emittedFiles: EmittedFile[] = [];
let customTransformCache = false;
const useCustomTransformCache = () => (customTransformCache = true);
let curPlugin: Plugin | undefined;
const curSource: string = source.code;
function transformReducer(
this: PluginContext,
previousCode: string,
result: TransformResult,
plugin: Plugin
): string {
let code: string;
let map: string | ExistingRawSourceMap | { mappings: '' } | null | undefined;
if (typeof result === 'string') {
code = result;
} else if (result && typeof result === 'object') {
module.updateOptions(result);
if (result.code == null) {
if (result.map || result.ast) {
warn(errNoTransformMapOrAstWithoutCode(plugin.name));
}
return previousCode;
}
({ code, map, ast } = result);
} else {
return previousCode;
}
// strict null check allows 'null' maps to not be pushed to the chain,
// while 'undefined' gets the missing map warning
if (map !== null) {
sourcemapChain.push(
decodedSourcemap(typeof map === 'string' ? JSON.parse(map) : map) || {
missing: true,
plugin: plugin.name
}
);
}
return code;
}
let code: string;
try {
code = await pluginDriver.hookReduceArg0(
'transform',
[curSource, id],
transformReducer,
(pluginContext, plugin): TransformPluginContext => {
curPlugin = plugin;
return {
...pluginContext,
addWatchFile(id: string) {
transformDependencies.push(id);
pluginContext.addWatchFile(id);
},
cache: customTransformCache
? pluginContext.cache
: getTrackedPluginCache(pluginContext.cache, useCustomTransformCache),
emitAsset(name: string, source?: string | Uint8Array) {
emittedFiles.push({ name, source, type: 'asset' as const });
return pluginContext.emitAsset(name, source);
},
emitChunk(id, options) {
emittedFiles.push({ id, name: options && options.name, type: 'chunk' as const });
return pluginContext.emitChunk(id, options);
},
emitFile(emittedFile: EmittedFile) {
emittedFiles.push(emittedFile);
return pluginDriver.emitFile(emittedFile);
},
error(err: RollupError | string, pos?: number | { column: number; line: number }): never {
if (typeof err === 'string') err = { message: err };
if (pos) augmentCodeLocation(err, pos, curSource, id);
err.id = id;
err.hook = 'transform';
return pluginContext.error(err);
},
getCombinedSourcemap() {
const combinedMap = collapseSourcemap(
id,
originalCode,
originalSourcemap,
sourcemapChain,
warn
);
if (!combinedMap) {
const magicString = new MagicString(originalCode);
return magicString.generateMap({ hires: true, includeContent: true, source: id });
}
if (originalSourcemap !== combinedMap) {
originalSourcemap = combinedMap;
sourcemapChain.length = 0;
}
return new SourceMap({
...combinedMap,
file: null as never,
sourcesContent: combinedMap.sourcesContent!
});
},
setAssetSource() {
return this.error({
code: 'INVALID_SETASSETSOURCE',
message: `setAssetSource cannot be called in transform for caching reasons. Use emitFile with a source, or call setAssetSource in another hook.`
});
},
warn(warning: RollupWarning | string, pos?: number | { column: number; line: number }) {
if (typeof warning === 'string') warning = { message: warning };
if (pos) augmentCodeLocation(warning, pos, curSource, id);
warning.id = id;
warning.hook = 'transform';
pluginContext.warn(warning);
}
};
}
);
} catch (err: any) {
throwPluginError(err, curPlugin?.name ?? 'Unknown plugin', { hook: 'transform', id });
}
if (!customTransformCache) {
// files emitted by a transform hook need to be emitted again if the hook is skipped
if (emittedFiles.length) module.transformFiles = emittedFiles;
}
return {
ast,
code,
customTransformCache,
meta: module.info.meta,
originalCode,
originalSourcemap,
sourcemapChain,
transformDependencies
};
}