const {Bundler} = require('@parcel/plugin');

// A very dumb bundler just for tests... (probably doesn't produce working code)
module.exports = new Bundler({
  bundle({bundleGraph}) {
    bundleGraph.traverse((node, context) => {
      if (node.type !== 'dependency') {
        return context;
      }

      let dependency = node.value;
      let assets = bundleGraph.getDependencyAssets(dependency);
      let bundleGroup = bundleGraph.createBundleGroup(dependency, dependency.target || context.target);

      for (let asset of assets) {
        let bundle = bundleGraph.createBundle({
          entryAsset: asset,
          needsStableName: Boolean(dependency.isEntry),
          target: bundleGroup.target,
        });

        bundleGraph.addAssetGraphToBundle(asset, bundle);
        bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
      }

      return {
        target: dependency.target || context.target
      };
    });
  },
  optimize() {}
});
