Skip to content

Commit

Permalink
Merge 03d9431 into ab2e39e
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand committed Jul 14, 2020
2 parents ab2e39e + 03d9431 commit 3f2d7aa
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 247 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- 8
- 9
- 10
- 11
- 12
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"@types/node": "^13.9.5",
"@types/node-sass": "^4.11.0",
"@types/tape": "^4.2.34",
"clean-css": "^4.2.3",
"coveralls": "^3.0.11",
"dart-sass": "^1.25.0",
"node-sass": "^4.13.1",
"nyc": "^15.0.0",
"tap-spec": "^5.0.0",
Expand Down
38 changes: 25 additions & 13 deletions src/lib/Rebaser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isAbsolute, posix, dirname} from "path";
import {parse, Url} from "url";
import {isAbsolute, posix, dirname, relative} from "path";
import {parse, Url, fileURLToPath} from "url";
import {SourceMapConsumer, SourceNode} from "source-map";
import {fromSource} from "convert-source-map";
import {EventEmitter} from "events";
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Rebaser extends EventEmitter {
return !isAbsolute(url.href) && (url.host === null) && ((url.hash === null) || (url.path !== null));
};

type NodeToRebase = {
type UrlToRebase = {
sourceMapNode: SourceNode,
originalPath: string,
rebasedPath: string,
Expand All @@ -92,8 +92,8 @@ export class Rebaser extends EventEmitter {
throw new Error("A map is required, either inline or explicitly passed as an option.");
}

let nodesToRebase: Array<NodeToRebase> = [];
let urlRegExp: RegExp = /^url\('(.*?)'\)|^url\("(.*?)"\)|^url\((.*?)\)/;
let urlsToRebase: Array<UrlToRebase> = [];
let urlRegExp: RegExp = /url\('(.*?)'\)|url\("(.*?)"\)|url\((.*?)\)/mg;

new SourceMapConsumer(map.toString()).then((consumer) => {
let sourceMapNode = SourceNode.fromStringWithSourceMap(originalCss, consumer);
Expand All @@ -102,22 +102,34 @@ export class Rebaser extends EventEmitter {
if (node.children) {
for (let child of node.children) {
if (typeof child === 'string') {
let match: RegExpExecArray = urlRegExp.exec(child);
let match: RegExpExecArray;

if (match !== null) {
while ((match = urlRegExp.exec(child)) != null) {
let urlStr: string = match[1] || match[2] || match[3];
let url: Url = parse(urlStr);

if (isRebasable(url)) {
let resolvedPath = posix.join(dirname(node.source), url.pathname);
let nodeSourceUrl = parse(node.source);

let nodeSource: string;

if (nodeSourceUrl.protocol === 'file:') {
nodeSource = fileURLToPath(node.source);
} else {
nodeSource = node.source;
}

nodeSource = relative('.', nodeSource);

let resolvedPath = posix.join(dirname(nodeSource), url.pathname);

const done: RebaseHandlerCallback = (rebasedPath) => {
if (rebasedPath !== false) {
if (!rebasedPath) { // default rebasing
rebasedPath = resolvedPath;
}

nodesToRebase.push({
urlsToRebase.push({
sourceMapNode: node,
originalPath: urlStr,
rebasedPath: rebasedPath,
Expand All @@ -133,7 +145,7 @@ export class Rebaser extends EventEmitter {
};
}

rebase(node.source, resolvedPath, done);
rebase(nodeSource, resolvedPath, done);
}
}
}
Expand All @@ -145,10 +157,10 @@ export class Rebaser extends EventEmitter {

handleNode(sourceMapNode);

for (let nodeToRebase of nodesToRebase) {
nodeToRebase.sourceMapNode.replaceRight(nodeToRebase.originalPath, nodeToRebase.rebasedPath);
for (let urlToRebase of urlsToRebase) {
urlToRebase.sourceMapNode.replaceRight(urlToRebase.originalPath, urlToRebase.rebasedPath);

this.emit("rebase", nodeToRebase.rebasedPath, nodeToRebase.resolvedPath);
this.emit("rebase", urlToRebase.rebasedPath, urlToRebase.resolvedPath);
}

let codeWithSourceMap = sourceMapNode.toStringWithSourceMap();
Expand Down
Loading

0 comments on commit 3f2d7aa

Please sign in to comment.