Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic string typing updates #2057

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"immutable": "^3.8.2",
"istanbul": "^0.4.3",
"lint-staged": "^7.0.0",
"magic-string": "^0.22.4",
"magic-string": "^0.22.5",
"minimist": "^1.2.0",
"mocha": "^5.0.4",
"prettier": "^1.10.2",
Expand Down
14 changes: 7 additions & 7 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { timeEnd, timeStart } from './utils/timers';
import { decode } from 'sourcemap-codec';
import MagicString, { Bundle as MagicStringBundle } from 'magic-string';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import { blank, forOwn } from './utils/object';
import Module, { ModuleJSON } from './Module';
import finalisers from './finalisers/index';
Expand Down Expand Up @@ -762,7 +762,7 @@ export default class Chunk {

magicString = finalise(
this,
(<any>magicString).trim(), // TODO TypeScript: Awaiting MagicString PR
magicString.trim(),
{
exportMode,
getPath,
Expand All @@ -777,10 +777,10 @@ export default class Chunk {
timeEnd('render format', 3);

if (banner) magicString.prepend(banner + '\n');
if (footer) (<any>magicString).append('\n' + footer); // TODO TypeScript: Awaiting MagicString PR
if (footer) magicString.append('\n' + footer);

const prevCode = magicString.toString();
let map: RawSourceMap = null;
let map: SourceMap = null;
const bundleSourcemapChain: RawSourceMap[] = [];

return transformBundle(prevCode, this.graph.plugins, bundleSourcemapChain, options).then(
Expand All @@ -797,13 +797,13 @@ export default class Chunk {
Boolean(plugin.transform || plugin.transformBundle)
)
) {
map = <any>magicString.generateMap({}); // TODO TypeScript: Awaiting missing version in SourceMap type
map = magicString.generateMap({});
if (typeof map.mappings === 'string') {
map.mappings = decode(map.mappings);
}
map = collapseSourcemaps(this, file, map, usedModules, bundleSourcemapChain);
} else {
map = <any>magicString.generateMap({ file, includeContent: true }); // TODO TypeScript: Awaiting missing version in SourceMap type
map = magicString.generateMap({ file, includeContent: true });
}

map.sources = map.sources.map(normalize);
Expand All @@ -812,7 +812,7 @@ export default class Chunk {
}

if (code[code.length - 1] !== '\n') code += '\n';
return { code, map } as { code: string; map: any }; // TODO TypeScript: Awaiting missing version in SourceMap type
return { code, map };
}
);
});
Expand Down
5 changes: 4 additions & 1 deletion src/ast/nodes/Literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export default class Literal<T = LiteralValueTypes> extends NodeBase {

render(code: MagicString, _options: RenderOptions) {
if (typeof this.value === 'string') {
(<any>code).indentExclusionRanges.push([this.start + 1, this.end - 1]); // TODO TypeScript: Awaiting MagicString PR
(<[number, number][]>code.indentExclusionRanges).push(<[number, number]>[
this.start + 1,
this.end - 1
]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/TemplateLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class TemplateLiteral extends NodeBase {
expressions: ExpressionNode[];

render(code: MagicString, options: RenderOptions) {
(<any>code).indentExclusionRanges.push([this.start, this.end]); // TODO TypeScript: Awaiting PR
(<[number, number][]>code.indentExclusionRanges).push(<[number, number]>[this.start, this.end]);
super.render(code, options);
}
}
8 changes: 4 additions & 4 deletions src/finalisers/amd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export default function amd(
if (intro) magicString.prepend(intro);

const exportBlock = getExportBlock(exports, dependencies, exportMode);
if (exportBlock) (<any>magicString).append('\n\n' + exportBlock); // TODO TypeScript: Awaiting PR
if (exportBlock) magicString.append('\n\n' + exportBlock);
if (exportMode === 'named' && options.legacy !== true && chunk.isEntryModuleFacade)
(<any>magicString).append(`\n\n${esModuleExport}`); // TODO TypeScript: Awaiting PR
if (outro) (<any>magicString).append(outro);
magicString.append(`\n\n${esModuleExport}`);
if (outro) magicString.append(outro);

return (<any>magicString) // TODO TypeScript: Awaiting PR
return magicString
.indent(indentString)
.append('\n\n});')
.prepend(wrapperStart);
Expand Down
4 changes: 2 additions & 2 deletions src/finalisers/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export default function cjs(

magicString.prepend(intro);

if (exportBlock) (<any>magicString).append('\n\n' + exportBlock); // TODO TypeScript: Awaiting PR
if (outro) (<any>magicString).append(outro); // TODO TypeScript: Awaiting PR
if (exportBlock) magicString.append('\n\n' + exportBlock);
if (outro) magicString.append(outro);

return magicString;
}
6 changes: 3 additions & 3 deletions src/finalisers/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export default function es(
exportBlock.push(`export { ${exportDeclaration.join(', ')} };`);
}

if (exportBlock.length) (<any>magicString).append('\n\n' + exportBlock.join('\n').trim()); // TODO TypeScript: Awaiting PR
if (exportBlock.length) magicString.append('\n\n' + exportBlock.join('\n').trim());

if (outro) (<any>magicString).append(outro); // TODO TypeScript: Awaiting PR
if (outro) magicString.append(outro);

return (<any>magicString).trim(); // TODO TypeScript: Awaiting PR
return magicString.trim();
}
8 changes: 4 additions & 4 deletions src/finalisers/iife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ export default function iife(
moduleDeclarations.dependencies,
exportMode
);
if (exportBlock) (<any>magicString).append('\n\n' + exportBlock); // TODO TypeScript: Awaiting PR
if (outro) (<any>magicString).append(outro); // TODO TypeScript: Awaiting PR
if (exportBlock) magicString.append('\n\n' + exportBlock);
if (outro) magicString.append(outro);

return (<any>magicString)
.indent(indentString) // TODO TypeScript: Awaiting PR
return magicString
.indent(indentString)
.prepend(wrapperIntro)
.append(wrapperOutro);
}
4 changes: 2 additions & 2 deletions src/finalisers/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ ${functionExports.length ? `${t}${t}${t}` + functionExports.join(`\n${t}${t}${t}

if (intro) magicString.prepend(intro);

if (outro) (<any>magicString).append(outro);
if (outro) magicString.append(outro);

return (<any>magicString) // TODO TypeScript: Awaiting PR
return magicString
.indent(`${t}${t}${t}`)
.append(`\n\n${t}${t}}\n${t}};\n});`)
.prepend(wrapperStart);
Expand Down
10 changes: 5 additions & 5 deletions src/finalisers/umd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ export default function umd(
moduleDeclarations.dependencies,
exportMode
);
if (exportBlock) (<any>magicString).append('\n\n' + exportBlock); // TODO TypeScript: Awaiting PR
if (exportBlock) magicString.append('\n\n' + exportBlock);
if (exportMode === 'named' && options.legacy !== true)
(<any>magicString).append(`\n\n${esModuleExport}`); // TODO TypeScript: Awaiting PR
if (outro) (<any>magicString).append(outro); // TODO TypeScript: Awaiting PR
magicString.append(`\n\n${esModuleExport}`);
if (outro) magicString.append(outro);

return (<any>magicString)
.trim() // TODO TypeScript: Awaiting PR
return magicString
.trim()
.indent(indentString)
.append(wrapperOutro)
.prepend(wrapperIntro);
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { ModuleJSON } from '../Module';
import { RawSourceMap } from 'source-map';
import Program from '../ast/nodes/Program';
import { Node } from '../ast/nodes/shared/Node';
import { SourceMap } from 'magic-string';
import { WatcherOptions } from '../watch/index';
import { Deprecation } from '../utils/deprecateOptions';
import Graph from '../Graph';
import { TransformContext } from '../utils/transform';
import ensureArray from '../utils/ensureArray';
import { SourceMap } from 'magic-string';

export const VERSION = '<@VERSION@>';

Expand Down
3 changes: 2 additions & 1 deletion src/utils/collapseSourcemaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { basename, dirname, relative, resolve } from './path';
import Module from '../Module';
import { RawSourceMap } from 'source-map';
import Chunk from '../Chunk';
import { SourceMap } from 'magic-string';

class Source {
isOriginal: boolean;
Expand Down Expand Up @@ -128,7 +129,7 @@ class Link {
export default function collapseSourcemaps(
bundle: Chunk,
file: string,
map: RawSourceMap,
map: SourceMap,
modules: Module[],
bundleSourcemapChain: RawSourceMap[]
) {
Expand Down