Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# rollup-plugin-sourcemaps

[![npm](https://img.shields.io/npm/v/rollup-plugin-sourcemaps.svg)](https://www.npmjs.com/package/rollup-plugin-sourcemaps)
[![Build Status](https://img.shields.io/travis/maxdavidson/rollup-plugin-sourcemaps/master.svg)](https://travis-ci.org/maxdavidson/rollup-plugin-sourcemaps)
[![Coverage Status](https://img.shields.io/coveralls/maxdavidson/rollup-plugin-sourcemaps/master.svg)](https://coveralls.io/github/maxdavidson/rollup-plugin-sourcemaps?branch=master)

# rollup-plugin-include-sourcemaps

[Rollup](https://rollupjs.org) plugin for loading files with existing source maps.
Inspired by [webpack/source-map-loader](https://github.com/webpack/source-map-loader).

Works with rollup 0.31.2 or later.

If you use [rollup-plugin-babel](https://github.com/rollup/rollup-plugin-babel),
you might be able to use the [`inputSourceMap`](https://babeljs.io/docs/en/options#inputsourcemap) option instead of this plugin.

Expand All @@ -21,7 +14,7 @@ you might be able to use the [`inputSourceMap`](https://babeljs.io/docs/en/optio
## Usage

```javascript
import sourcemaps from 'rollup-plugin-sourcemaps';
import sourcemaps from 'rollup-plugin-include-sourcemaps';

export default {
input: 'src/index.js',
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-sourcemaps",
"version": "0.6.3",
"name": "rollup-plugin-include-sourcemaps",
"version": "0.7.0",
"description": "Rollup plugin for grabbing source maps from sourceMappingURLs",
"author": "Max Davidson <davidsonmax@gmail.com>",
"license": "MIT",
Expand All @@ -16,7 +16,7 @@
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/maxdavidson/rollup-plugin-sourcemaps"
"url": "https://github.com/IIIMADDINIII/rollup-plugin-include-sourcemaps"
},
"engines": {
"node": ">=10.0.0"
Expand All @@ -40,12 +40,15 @@
"dist"
],
"dependencies": {
"@rollup/pluginutils": "^3.0.9",
"source-map-resolve": "^0.6.0"
"@rollup/pluginutils": "^5.0.2",
"decode-uri-component": "^0.2.0",
"atob": "^2.1.2"
},
"devDependencies": {
"@rollup/plugin-typescript": "6.0.0",
"@types/node": "^10.17.21",
"@types/node": "^18.11.9",
"@types/decode-uri-component": "^0.2.0",
"@types/atob": "^2.1.2",
"@typescript-eslint/eslint-plugin": "^4.0.0",
"@typescript-eslint/parser": "^4.0.0",
"eslint": "^7.1.0",
Expand All @@ -67,4 +70,4 @@
"optional": true
}
}
}
}
42 changes: 25 additions & 17 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const sourceMapPath = path.format({
ext: '.js.map',
});

function comparePath(a: string, b: string): boolean {
const first = a.split(/[\\/]/);
const second = b.split(/[\\/]/);
if (process.platform == 'win32') {
first[0] = first[0].toLowerCase();
second[0] = second[0].toLowerCase();
}
return first.every((v, i) => v == second[i]);
}

async function rollupBundle({
outputText,
sourceMapText,
Expand All @@ -31,16 +41,14 @@ async function rollupBundle({
pluginOptions?: SourcemapsPluginOptions;
}) {
const load = async (path: string) => {
switch (path) {
case inputPath:
return inputText;
case outputPath:
return outputText;
case sourceMapPath:
return sourceMapText!;
default:
throw new Error(`Unexpected path: ${path}`);
if (comparePath(path, inputPath)) {
return inputText;
} else if (comparePath(path, outputPath)) {
return outputText;
} else if (comparePath(path, sourceMapPath)) {
return sourceMapText!;
}
throw new Error(`Unexpected path: ${path}`);
};

const { generate } = await rollup({
Expand Down Expand Up @@ -82,7 +90,7 @@ it('ignores files with no source maps', async () => {
const { map } = await rollupBundle({ outputText, sourceMapText });

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([outputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([outputPath]);
expect(map!.sourcesContent).toStrictEqual([outputText]);
});

Expand Down Expand Up @@ -123,7 +131,7 @@ describe('detects files with source maps', () => {
const { map } = await rollupBundle({ outputText, sourceMapText });

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([inputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([inputPath]);
expect(map!.sourcesContent).toStrictEqual([inputText]);
},
);
Expand All @@ -150,7 +158,7 @@ describe('ignores filtered files', () => {
});

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([outputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([outputPath]);
expect(map!.sourcesContent).toStrictEqual([outputText]);
});

Expand All @@ -169,12 +177,12 @@ describe('ignores filtered files', () => {
outputText,
sourceMapText,
pluginOptions: {
exclude: [path.relative(process.cwd(), outputPath)],
exclude: [path.relative(process.cwd(), outputPath).split('\\').join('/')],
},
});

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([outputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([outputPath]);
expect(map!.sourcesContent).toStrictEqual([outputText]);
});
});
Expand All @@ -194,14 +202,14 @@ it('delegates failing file reads to the next plugin', async () => {
outputText,
sourceMapText,
pluginOptions: {
readFile(_path, cb) {
readFile(_path: string, cb: (error: Error | null, data: Buffer | string) => void) {
cb(new Error('Failed!'), '');
},
},
});

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([outputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([outputPath]);
expect(map!.sourcesContent).toStrictEqual([outputText]);
});

Expand Down Expand Up @@ -234,6 +242,6 @@ it('handles failing source maps reads', async () => {
});

expect(map).toBeDefined();
expect(map!.sources).toStrictEqual([outputPath]);
expect(map!.sources.map(path.normalize)).toStrictEqual([outputPath]);
expect(map!.sourcesContent).toStrictEqual([outputText]);
});
64 changes: 0 additions & 64 deletions src/env.d.ts

This file was deleted.

99 changes: 91 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,97 @@
import fs from 'fs';
import { promisify } from 'util';
import { Plugin, ExistingRawSourceMap } from 'rollup';
import pluginUtils, { CreateFilter } from '@rollup/pluginutils';
import sourceMapResolve from 'source-map-resolve';
import { CreateFilter, createFilter } from '@rollup/pluginutils';
import atob from 'atob';
import * as urlLib from 'url';
import decodeUriComponentLib from 'decode-uri-component';

const { createFilter } = pluginUtils;
const { resolveSourceMap, resolveSources } = sourceMapResolve;
interface ResolvedSourceMap {
map: ExistingRawSourceMap;
url: string | null;
sourcesRelativeTo: string;
sourceMappingURL: string;
}

function resolveUrl(...args: string[]): string {
return args.reduce((resolved, nextUrl) => urlLib.resolve(resolved, nextUrl));
}

function customDecodeUriComponent(string: string): string {
return decodeUriComponentLib(string.replace(/\+/g, '%2B'));
}

function parseMapToJSON(string: string): ExistingRawSourceMap {
return <ExistingRawSourceMap>JSON.parse(string.replace(/^\)\]\}'/, ''));
}

const sourceMappingURLRegex = RegExp(
'(?:/\\*(?:\\s*\\r?\\n(?://)?)?(?:[#@] sourceMappingURL=([^\\s\'"]*))\\s*\\*/|//(?:[#@] sourceMappingURL=([^\\s\'"]*)))\\s*',
);

const promisifiedResolveSourceMap = promisify(resolveSourceMap);
const promisifiedResolveSources = promisify(resolveSources);
function getSourceMappingUrl(code: string): string | null {
const match = sourceMappingURLRegex.exec(code);
return match ? match[1] || match[2] || '' : null;
}

async function resolveSourceMap(
code: string,
codeUrl: string,
read: (path: string) => Promise<Buffer | string>,
): Promise<ResolvedSourceMap | null> {
const sourceMappingURL = getSourceMappingUrl(code);
if (!sourceMappingURL) {
return null;
}
const dataUri = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/.exec(sourceMappingURL);
if (dataUri) {
const mimeType = dataUri[1] || 'text/plain';
if (!/^(?:application|text)\/json$/.test(mimeType)) {
throw new Error('Unuseful data uri mime type: ' + mimeType);
}
const map = parseMapToJSON(
(dataUri[2] === ';base64' ? atob : decodeURIComponent)(dataUri[3] || ''),
);
return { sourceMappingURL, url: null, sourcesRelativeTo: codeUrl, map };
}
const url = resolveUrl(codeUrl, sourceMappingURL);
const map = parseMapToJSON(String(await read(customDecodeUriComponent(url))));
return { sourceMappingURL, url, sourcesRelativeTo: url, map };
}

interface ResolvedSources {
sourcesResolved: string[];
sourcesContent: (string | Error)[];
}

async function resolveSources(
map: ExistingRawSourceMap,
mapUrl: string,
read: (path: string) => Promise<Buffer | string>,
): Promise<ResolvedSources> {
const sourcesResolved: string[] = [];
const sourcesContent: (string | Error)[] = [];
for (let index = 0, len = map.sources.length; index < len; index++) {
const sourceRoot = map.sourceRoot;
const sourceContent = (map.sourcesContent || [])[index];
const resolvePaths = [mapUrl, map.sources[index]];
if (sourceRoot !== undefined && sourceRoot !== '') {
resolvePaths.splice(1, 0, sourceRoot.replace(/\/?$/, '/'));
}
sourcesResolved[index] = resolveUrl(...resolvePaths);
if (typeof sourceContent === 'string') {
sourcesContent[index] = sourceContent;
continue;
}
try {
const source = await read(customDecodeUriComponent(sourcesResolved[index]));
sourcesContent[index] = String(source);
} catch (error) {
sourcesContent[index] = <Error>error;
}
}
return { sourcesResolved, sourcesContent };
}

export interface SourcemapsPluginOptions {
include?: Parameters<CreateFilter>[0];
Expand Down Expand Up @@ -42,7 +125,7 @@ export default function sourcemaps({

let map: ExistingRawSourceMap;
try {
const result = await promisifiedResolveSourceMap(code, id, readFile);
const result = await resolveSourceMap(code, id, promisifiedReadFile);

// The code contained no sourceMappingURL
if (result === null) {
Expand All @@ -58,7 +141,7 @@ export default function sourcemaps({
// Resolve sources if they're not included
if (map.sourcesContent === undefined) {
try {
const { sourcesContent } = await promisifiedResolveSources(map, id, readFile);
const { sourcesContent } = await resolveSources(map, id, promisifiedReadFile);
if (sourcesContent.every(item => typeof item === 'string')) {
map.sourcesContent = sourcesContent as string[];
}
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"sourceMap": true,
"inlineSources": true,
"strict": true,
"esModuleInterop": true
"esModuleInterop": true,
"lib": ["ESNext"],
"allowSyntheticDefaultImports": true,
},
"include": ["src"],
"exclude": ["node_modules"]
}
}