Skip to content

Commit

Permalink
Fix the paths for source maps in the release (#5)
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
bryanforbes committed Jul 13, 2018
1 parent b8f78f4 commit 54a0a46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
"devDependencies": {
"@types/node": "^6.0.54",
"@types/yargs": "^8.0.2",
"codecov.io": "0.1.6",
"codecov": "~3.0.4",
"copyfiles": "^1.2.0",
"husky": "^0.14.3",
"intern": "~4.1.4",
"intern": "~4.2.0",
"lint-staged": "^6.0.0",
"prettier": "^1.10.2",
"rimraf": "^2.6.2",
Expand Down
26 changes: 24 additions & 2 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ const extensionMapByDir: { [key: string]: { [key: string]: string } } = {
};

const contentTransformsByDir: { [key: string]: { [key: string]: ContentTransform } } = {
cjs: {
'.js.map': fixSourceMap
},
esm: {
'.mjs': remapMjsSourceMap,
'.mjs.map': fixMjsSourceMap
},
umd: {
'.js.map': fixSourceMap
}
};

Expand All @@ -53,7 +59,7 @@ destDirectories.forEach(({ dest: destDir, flat, packageJson }) => {

const destFile = path.join(destDirFullPath, parsed.path, parsed.file + parsed.extension);

copy(sourceFile, destFile, transformMap[parsed.extension]);
copy(sourceFile, destFile, flat, transformMap[parsed.extension]);
});
});

Expand All @@ -76,7 +82,23 @@ function remapMjsSourceMap(contents: string): string {
return contents.replace(/(\/\/.*sourceMappingURL=.*?)(\.js\.map)/g, '$1.mjs.map');
}

function fixMjsSourceMap(contents: string): string {
function fixSourceMap(contents: string, flat: boolean): string {
if (flat) {
const json = JSON.parse(contents);

if (json.sources) {
json.sources = json.sources.map((source: string) => path.basename(source));
}

return JSON.stringify(json);
}

return contents;
}

function fixMjsSourceMap(contents: string, flat: boolean): string {
contents = fixSourceMap(contents, flat);

const json = JSON.parse(contents);

if (json.file) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export function glob(base: string): string[] {
}

export interface ContentTransform {
(content: string): string;
(content: string, flat: boolean): string;
}

export function copy(sourceFile: string, destFile: string, transform?: ContentTransform) {
export function copy(sourceFile: string, destFile: string, flat: boolean, transform?: ContentTransform) {
// dest file path must exist all the way down
const directoriesThatNeedToExist = [];
let base = path.dirname(destFile);
Expand All @@ -39,7 +39,7 @@ export function copy(sourceFile: string, destFile: string, transform?: ContentTr

let content: string | Buffer = fs.readFileSync(sourceFile);
if (transform) {
content = transform(content.toString());
content = transform(content.toString(), flat);
}

fs.writeFileSync(destFile, content);
Expand Down

0 comments on commit 54a0a46

Please sign in to comment.