/
transform.ts
176 lines (158 loc) · 5.51 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
175
176
import * as ESTree from 'estree';
import { decode } from 'sourcemap-codec';
import Program from '../ast/nodes/Program';
import Graph from '../Graph';
import Module from '../Module';
import {
Asset,
Plugin,
PluginCache,
PluginContext,
RawSourceMap,
RollupError,
RollupWarning,
TransformSourceDescription
} from '../rollup/types';
import { createTransformEmitAsset, EmitAsset } from './assetHooks';
import { augmentCodeLocation, error } from './error';
import { dirname, resolve } from './path';
import { trackPluginCache } from './pluginDriver';
export default function transform(
graph: Graph,
source: TransformSourceDescription,
module: Module
) {
const id = module.id;
const sourcemapChain: RawSourceMap[] = [];
const originalSourcemap = typeof source.map === 'string' ? JSON.parse(source.map) : source.map;
if (originalSourcemap && typeof originalSourcemap.mappings === 'string')
originalSourcemap.mappings = decode(originalSourcemap.mappings);
const baseEmitAsset = graph.pluginDriver.emitAsset;
const originalCode = source.code;
let ast = <Program>source.ast;
let transformDependencies: string[];
let assets: Asset[];
let customTransformCache = false;
let trackedPluginCache: { used: boolean; cache: PluginCache };
let curPlugin: Plugin;
const curSource: string = source.code;
function transformReducer(this: PluginContext, code: string, result: any, plugin: Plugin) {
// track which plugins use the custom this.cache to opt-out of transform caching
if (!customTransformCache && trackedPluginCache.used) customTransformCache = true;
if (customTransformCache) {
if (result && Array.isArray(result.dependencies)) {
for (const dep of result.dependencies) {
const depId = resolve(dirname(id), dep);
if (!graph.watchFiles[depId]) graph.watchFiles[depId] = true;
}
}
} else {
// assets emitted by transform are transformDependencies
if (assets.length) module.transformAssets = assets;
if (result && Array.isArray(result.dependencies)) {
// not great, but a useful way to track this without assuming WeakMap
if (!(<any>curPlugin).warnedTransformDependencies)
this.warn({
code: 'TRANSFORM_DEPENDENCIES_DEPRECATED',
message: `Returning "dependencies" from plugin transform hook is deprecated for using this.addWatchFile() instead.`
});
(<any>curPlugin).warnedTransformDependencies = true;
if (!transformDependencies) transformDependencies = [];
for (const dep of result.dependencies)
transformDependencies.push(resolve(dirname(id), dep));
}
}
if (result == null) return code;
if (typeof result === 'string') {
result = {
code: result,
ast: undefined,
map: undefined
};
} else if (typeof result.map === 'string') {
// `result.map` can only be a string if `result` isn't
result.map = JSON.parse(result.map);
}
if (result.map && typeof result.map.mappings === 'string') {
result.map.mappings = decode(result.map.mappings);
}
// strict null check allows 'null' maps to not be pushed to the chain, while 'undefined' gets the missing map warning
if (result.map !== null) {
sourcemapChain.push(result.map || { missing: true, plugin: plugin.name });
}
ast = result.ast;
return result.code;
}
let setAssetSourceErr: any;
return graph.pluginDriver
.hookReduceArg0<any, string>(
'transform',
[curSource, id],
transformReducer,
(pluginContext, plugin) => {
curPlugin = plugin;
if (plugin.cacheKey) customTransformCache = true;
else trackedPluginCache = trackPluginCache(pluginContext.cache);
let emitAsset: EmitAsset;
({ assets, emitAsset } = createTransformEmitAsset(graph.assetsById, baseEmitAsset));
return {
...pluginContext,
cache: trackedPluginCache ? trackedPluginCache.cache : pluginContext.cache,
warn(warning: RollupWarning | string, pos?: { line: number; column: number }) {
if (typeof warning === 'string') warning = { message: warning } as RollupWarning;
if (pos) augmentCodeLocation(warning, pos, curSource, id);
warning.id = id;
warning.hook = 'transform';
pluginContext.warn(warning);
},
error(err: RollupError | string, pos?: { line: number; column: number }) {
if (typeof err === 'string') err = { message: err };
if (pos) augmentCodeLocation(err, pos, curSource, id);
err.id = id;
err.hook = 'transform';
pluginContext.error(err);
},
emitAsset,
addWatchFile(id: string) {
if (!transformDependencies) transformDependencies = [];
transformDependencies.push(id);
pluginContext.addWatchFile(id);
},
setAssetSource(assetId, source) {
pluginContext.setAssetSource(assetId, source);
if (!customTransformCache && !setAssetSourceErr) {
try {
this.error({
code: 'INVALID_SETASSETSOURCE',
message: `setAssetSource cannot be called in transform for caching reasons. Use emitAsset with a source, or call setAssetSource in another hook.`
});
} catch (err) {
setAssetSourceErr = err;
}
}
}
};
}
)
.catch(err => {
if (typeof err === 'string') err = { message: err };
if (err.code !== 'PLUGIN_ERROR') {
if (err.code) err.pluginCode = err.code;
err.code = 'PLUGIN_ERROR';
}
err.id = id;
error(err);
})
.then(code => {
if (!customTransformCache && setAssetSourceErr) throw setAssetSourceErr;
return {
code,
transformDependencies,
originalCode,
originalSourcemap,
ast: <ESTree.Program>ast,
sourcemapChain,
customTransformCache
};
});
}