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

Add ability to generate maps for merged files #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
58 changes: 51 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,55 @@ const fs = require('fs');
const glob = require('glob');
const { promisify } = require('es6-promisify');
const revHash = require('rev-hash');
const { SourceMapGenerator } = require('source-map');
const path = require('path');
const { sources, Compilation } = require('webpack');

const plugin = { name: 'MergeIntoFile' };

const readFile = promisify(fs.readFile);
const listFiles = promisify(glob);

const joinContent = async (promises, separator) => promises
.reduce(async (acc, curr) => `${await acc}${(await acc).length ? separator : ''}${await curr}`, '');
const getLineNumber = function (str, start, stop) {
const i1 = (start === undefined || start < 0) ? 0 : start;
const i2 = (stop === undefined || stop >= str.length) ? str.length - 1 : stop;
let ret = 1;
for (let i = i1; i <= i2; i++) if (str.charAt(i) === '\n') ret++;
return ret;
};

const joinContentWithMap = async (promises, separator, sourceRoot, inlineSources) => promises.reduce(async (acc, curr) => {
const lines = getLineNumber(await curr.content);
const relativePath = sourceRoot ? path.relative(sourceRoot, curr.path) : curr.path;

if (inlineSources) {
(await acc).map.setSourceContent(relativePath, await curr.content);
}

for (let offset = 0; offset < lines; offset++) {
(await acc).map.addMapping({
source: relativePath,
original: { line: 1 + offset, column: 0 },
generated: { line: (await acc).lines + offset, column: 0 },
});
}

return {
code: `${(await acc).code}${(await acc).code.length ? separator : ''}${await curr.content}`,
lines: (await acc).lines + lines,
map: (await acc).map,
};
}, {
code: '',
lines: 1,
map: new SourceMapGenerator(),
});

const joinContent = async (promises, separator) => promises.reduce(async (acc, curr) => ({
code: `${(await acc).code}${(await acc).code.length ? separator : ''}${await curr.content}`,
}), {
code: '',
});

class MergeIntoFile {
constructor(options, onComplete) {
Expand Down Expand Up @@ -66,6 +106,7 @@ class MergeIntoFile {
chunks,
hash,
transformFileName,
sourceMap,
} = this.options;
if (chunks && compilation.chunks && compilation.chunks
.filter((chunk) => chunks.indexOf(chunk.name) >= 0 && chunk.rendered).length === 0) {
Expand All @@ -89,20 +130,23 @@ class MergeIntoFile {
filesCanonical.forEach((fileTransform) => {
if (typeof fileTransform.dest === 'string') {
const destFileName = fileTransform.dest;
fileTransform.dest = (code) => ({ // eslint-disable-line no-param-reassign
fileTransform.dest = (code, map) => ({ // eslint-disable-line no-param-reassign
[destFileName]: (transform && transform[destFileName])
? transform[destFileName](code)
? transform[destFileName](code, map)
: code,
});
}
});
const sourceMapEnabled = !!sourceMap;
const sourceMapRoot = sourceMap && sourceMap.sourceRoot;
const sourceMapInlineSources = sourceMap && sourceMap.inlineSources;
const finalPromises = filesCanonical.map(async (fileTransform) => {
const { separator = '\n' } = this.options;
const listOfLists = await Promise.all(fileTransform.src.map((path) => listFiles(path, null)));
const flattenedList = Array.prototype.concat.apply([], listOfLists);
const filesContentPromises = flattenedList.map((path) => readFile(path, encoding || 'utf-8'));
const content = await joinContent(filesContentPromises, separator);
const resultsFiles = await fileTransform.dest(content);
const filesContentPromises = flattenedList.map(path => ({ path, content: readFile(path, encoding || 'utf-8') }));
const content = sourceMapEnabled ? await joinContentWithMap(filesContentPromises, separator, sourceMapRoot, sourceMapInlineSources) : await joinContent(filesContentPromises, separator);
const resultsFiles = await fileTransform.dest(content.code, content.map);
// eslint-disable-next-line no-restricted-syntax
for (const resultsFile in resultsFiles) {
if (typeof resultsFiles[resultsFile] === 'object') {
Expand Down
Loading