Skip to content

Commit f0d3e5a

Browse files
authored
Merge pull request #223 from PepsRyuu/LoadHookSourceMap
Load Hook return AST
2 parents 96db07f + 67f5532 commit f0d3e5a

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

docs/sdk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ container.hooks.buildStart(options);
4343
container.hooks.resolveDynamicImport(id, parentId);
4444
container.hooks.resolveId(id, parentId, options);
4545
container.hooks.load(filepath, parentFilepath);
46-
container.hooks.transform(code, id);
46+
container.hooks.transform(code, id, map);
4747
container.hooks.watchChange(id);
4848
container.hooks.buildEnd(error);
4949
container.hooks.renderStart(outputOptions, inputOptions);

lib/impl/NollupCodeGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class NollupCodeGenerator {
363363
if (map) {
364364
map.sourceRoot = 'nollup:///';
365365
map.sources[map.sources.length - 1] = sourcePath;
366-
code += `${ConvertSourceMap.fromObject(map).toComment()}\\n`;
366+
code += `\\n${ConvertSourceMap.fromObject(map).toComment()}\\n`;
367367
}
368368

369369
code += `\\n//# sourceURL=nollup-int:///${sourcePath}`;

lib/impl/NollupCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ async function compileModule (context, filePath, parentFilePath, depth, emitted,
147147
context.currentModuleEmittedChunksCache = emittedChunksCache;
148148

149149
let loaded = await context.plugins.hooks.load(filePath, parentFilePath);
150-
let transformed = await context.plugins.hooks.transform( loaded.code, filePath);
150+
let transformed = await context.plugins.hooks.transform( loaded.code, filePath, loaded.map);
151151
let resolved = await ImportExportResolver(context.plugins, transformed.code, filePath, generator, context.liveBindings);
152152

153153
file.transformed = resolved.code;

lib/impl/PluginLifecycle.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,16 @@ const PluginLifecycle = {
316316
}
317317
},
318318

319-
async transform (code, id) {
320-
let map, originalCode = code, mapChain = [];
319+
async transform (code, id, map) {
320+
let originalCode = code, mapChain = [];
321+
322+
if (map) {
323+
// The load step can contain a map, as well as transform code from original code.
324+
// TypeScript plugin uses the load step to transform from TS to ES.
325+
map = typeof map === 'string'? JSON.parse(map) : map;
326+
originalCode = map.sourcesContent? map.sourcesContent[0] : code;
327+
mapChain.push({ code: originalCode, map });
328+
}
321329

322330
container.__currentMapChain = mapChain;
323331
container.__currentOriginalCode = originalCode;
@@ -355,15 +363,13 @@ const PluginLifecycle = {
355363

356364
}, { code, id, map: null, syntheticNamedExports: false });
357365

358-
map = await combineSourceMapChainFast(mapChain, originalCode, id);
359-
360366
container.__currentMapChain = null;
361367
container.__currentOriginalCode = null;
362368
container.__currentModuleId = null;
363369

364370
return {
365371
code: hr.code,
366-
map: map,
372+
map: await combineSourceMapChainFast(mapChain, originalCode, id),
367373
syntheticNamedExports
368374
};
369375
},

lib/impl/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
* @property {(id: string, parentFilePath: string) => Promise<RollupResolveIdResult>} resolveDynamicImport
168168
* @property {(id: string, parentFilePath: string, options: object) => Promise<RollupResolveIdResult>} resolveId
169169
* @property {(filePath: string, parentFilePath: string) => Promise<RollupSourceDescription>} load
170-
* @property {(code: string, id: string) => Promise<RollupSourceDescription>} transform
170+
* @property {(code: string, id: string, map: object) => Promise<RollupSourceDescription>} transform
171171
* @property {(filePath: string) => void} watchChange
172172
* @property {(error: Error) => Promise<void>} buildEnd
173173
* @property {(outputOptions: RollupOutputOptions, inputOptions: RollupConfigContainer)} renderStart

test/cases/api/hooks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ describe ('API: Plugin Hooks', () => {
617617
});
618618

619619
it ('should be allowed to return an AST');
620+
it ('should be allowed to return an AST as a string');
620621

621622
it ('should allow meta option to be returned', async () => {
622623
fs.stub('./src/main.js', () => `

0 commit comments

Comments
 (0)