首页 > 其他分享 >uniapp 复制编译后sourcemap复制到工程内

uniapp 复制编译后sourcemap复制到工程内

时间:2024-09-03 18:04:08浏览次数:7  
标签:复制到 uniapp fs const sourcemap 文件夹 CopyFilePath path

sourcemap.js

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

/**
 * 将/dist/dev/.sourcemap文件复制到/dist/dev/mp-toutiao/.sourcemap
 */
let triggerMove = false;
module.exports = async () => {
    if (triggerMove) return;
    triggerMove = true;
    console.log('exec...');
    // if (process.env.NODE_ENV == 'development') {
    const basePath = path.join(__dirname, '../dist');

    let OriginFilePath = `${basePath}/dev/.sourcemap`;
    let CopyFilePath = `${basePath}/dev/mp-toutiao/.sourcemap`;

    //判断要创建的文件夹(newFile)是否存在,不存在就创建一个
    if (!fs.existsSync(CopyFilePath)) {
        fs.mkdir(CopyFilePath, (err) => {
            // console.log(err)
        });
    }
    //传入路径
    copyFiles(OriginFilePath, CopyFilePath);
    // }
};

/**
 * 文件从旧文件夹复制到新文件夹
 * @param {string} OriginFilePath
 * @param {string} CopyFilePath
 */
function copyFiles(OriginFilePath, CopyFilePath) {
    //读取newFile文件夹下的文件
    fs.readdir(OriginFilePath, { withFileTypes: true }, (err, files) => {
        for (let file of files) {
            //判断是否是文件夹,不是则直接复制文件到newFile中
            if (!file.isDirectory()) {
                //获取旧文件夹中要复制的文件
                const OriginFile = path.resolve(OriginFilePath, file.name);
                //获取新文件夹中复制的地方
                const CopyFile = path.resolve(CopyFilePath, file.name);
                //将文件从旧文件夹复制到新文件夹中
                fs.copyFileSync(OriginFile, CopyFile);
            } else {
                //如果是文件夹就递归变量把最新的文件夹路径传过去
                const CopyDirPath = path.resolve(CopyFilePath, file.name);
                const OriginDirPath = path.resolve(OriginFilePath, file.name);
                fs.mkdir(CopyDirPath, (err) => {});
                // OriginFilePath = OriginPath
                // CopyFilePath = DirPath
                copyFiles(OriginDirPath, CopyDirPath);
            }
        }
    });
}

vue.config.js

const fs = require('fs');
// const upload = require('./build/upload');
const sourcemap = require('./build/sourcemap');
const manifestPath = `${__dirname}/src/manifest.json`;

let Manifest = fs.readFileSync(manifestPath, {
    encoding: 'utf-8',
});

function replaceManifest(path, value) {
    const arr = path.split('.');
    const len = arr.length;
    const lastItem = arr[len - 1];

    let i = 0;
    let ManifestArr = Manifest.split(/\n/);

    for (let index = 0; index < ManifestArr.length; index++) {
        const item = ManifestArr[index];
        if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
        if (i === len) {
            const hasComma = /,/.test(item);
            ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`);
            break;
        }
    }

    Manifest = ManifestArr.join('\n');
}

// 读取环境变量内容
replaceManifest('name', `"${process.env.VUE_APP_NAME}"`);
replaceManifest('mp-toutiao.appid', `"${process.env.VUE_APP_APPID}"`);

fs.writeFileSync(manifestPath, Manifest, {
    flag: 'w',
});

module.exports = {
    transpileDependencies: ['@dcloudio/uni-ui', 'uview-ui'],
    chainWebpack: (config) => {
        // if (process.env.NODE_ENV === 'production') {
        //     config.plugin('done').use(require('webpack').ProgressPlugin, [
        //         {
        //             handler: (percentage, message, ...args) => {
        //                 if (percentage === 1 && ['test', 'pre'].indexOf(process.env.VUE_APP_ENV) > -1) {
        //                     // 上传小程序代码到mp后台,这里只有测试环境、预发环境才上传,其他手动上传
        //                     upload();
        //                 }
        //             },
        //         },
        //     ]);
        // }
        if (process.env.NODE_ENV === 'development') {
            config.plugin('done').use(require('webpack').ProgressPlugin, [
                {
                    handler: (percentage, message, ...args) => {
                        if (percentage === 1) {
                            sourcemap();
                        }
                    },
                },
            ]);
        }
    },
};

 

标签:复制到,uniapp,fs,const,sourcemap,文件夹,CopyFilePath,path
From: https://www.cnblogs.com/guxingzhe/p/18395136

相关文章