const path = require('path');
const fs = require('fs');
const escodegen = require('escodegen');

module.exports = function () {

    // 构建 import components && comp config map
    const serviceImports = [];

    // insert pages && components to service.js
    let insertAst = {
        "type": "Program",
        "body": [
            {
                "type": "ImportDeclaration",
                "specifiers": [],
                "source": {
                    "type": "Literal",
                    "value": "bili-small-app/runtime/header",
                    "raw": "'bili-small-app/runtime/header'",
                },
            },
            {
                "type": "ImportDeclaration",
                "specifiers": [],
                "source": {
                    "type": "Literal",
                    "value": "%/app.js",
                    "raw": "'%/app.js'",
                },
            }
        ],
        "sourceType": "module",
    };

    insertAst.body.push(...serviceImports);

    const importCode = escodegen.generate(insertAst);
    // append 
    const appendCode = fs.readFileSync(path.join(path.dirname(__dirname), 'splitChunksConfig', 'service.js'), 'utf8');

    return importCode + appendCode;
};
