Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Add compiled outputs data for bundle, buildStatic methods #693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sergei-startsev
Copy link

I've added the additional field outputs to the output of bundle, buildStatic methods. The field contains a sorted array of sources and appropriate source map data before merging them to the single bundle:
outputs
It allows to add plugins that can build custom bundles based on provided data, e.g. eval bundle:

eval(
   $__System.register(...);
   //# sourceMappingURL=moduleA.js.map
);
eval(...);
...
eval(...);

Here are more examples of different bundles that Webpack uses: devtool

@guybedford
Copy link
Member

Thanks, I like the idea. How can the outputs object be used after being modified though? Can you provide an example of the sort of API you see this being used with?

@sergei-startsev
Copy link
Author

sergei-startsev commented Sep 20, 2016

Here is how it could be used for building eval bundles (each module is executed with eval, sourceMappingURL is also added to the eval):

return builder.buildStatic(url, {options}).then(function (output) {
    //resulted eval bundle with inline sourceMaps
    var bundle = "";
    output.outputs.forEach(function (el) {
        bundle += `
        eval("${JSON.stringify(el.source)}\n
            //# sourceMappingURL=data:application/json;base64,${new Buffer(el.sourceMaps).toString("base64")}"
        )\n`;
    });
    return bundle;
});

another example uses the custom algorithm for concatenation sourcemaps:

return builder.buildStatic(url, {options}).then(function (output) {
    //maps output to the expected format
    var files = output.outputs.map(function (el) {
        if (el.source) {
            //fixes source urls
            el.sourceMap.sources = el.sourceMap.sources.map(function (sourceURL) {
                return sourceURL.replace(/\\/g, "/");
            });
            return {
                code: el.source,
                map: el.sourceMap
            };
        } else {
            return {
                code: el,
                map: undefined
            };
        }
    });
    //builds concatenated bundle with inline sourcemaps
    var concatenated = concat(files, { delimiter: "\n" }).toStringWithSourceMap();
    var bundle = `${concatenated.code}\n
        //# sourceMappingURL=data:application/json;base64,${new Buffer(concatenated.map.toString()).toString("base64")}`;

    return bundle;
});

The idea is that you could customize your output bundle based on data in outputs field.

@guybedford
Copy link
Member

I think it would be better to accept an outputs hook as a build option something like:

builder.bundle('x', {
  hook: {
    output: (outputs) => {
      return modifiedOutputs;
    }
  }
}).then(...);

That said, i'm weary to rush into a hook architecture on a whim without fully thinking it through as well.

@lastmjs
Copy link

lastmjs commented Dec 21, 2016

Could this pull request relate to my questions here about a static build with the semantics of compile?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants