"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.depAssetRewritePlugin = exports.depAssetExternalPlugin = void 0;
const path_1 = __importDefault(require("path"));
const es_module_lexer_1 = require("es-module-lexer");
const css_1 = require("../plugins/css");
const magic_string_1 = __importDefault(require("magic-string"));
const utils_1 = require("../utils");
exports.depAssetExternalPlugin = (config) => ({
    name: 'vite:dep-assets-external',
    resolveId(id) {
        if (css_1.isCSSRequest(id) || config.assetsInclude(id)) {
            return {
                id,
                external: true
            };
        }
    }
});
exports.depAssetRewritePlugin = (config) => {
    return {
        name: 'vite:dep-assets-rewrite',
        async transform(code, id) {
            if (id.endsWith('.js')) {
                await es_module_lexer_1.init;
                const [imports] = es_module_lexer_1.parse(code);
                if (imports.length) {
                    let s;
                    for (let i = 0; i < imports.length; i++) {
                        const { s: start, e: end, d: dynamicIndex, ss: statementStart, se: statementEnd } = imports[i];
                        if (dynamicIndex === -1) {
                            const importee = code.slice(start, end);
                            if (css_1.isCSSRequest(importee) || config.assetsInclude(importee)) {
                                // replace css/asset imports to deep imports to their original
                                // location
                                s = s || new magic_string_1.default(code);
                                // #903 rollup-plugin-commonjs will inject proxy helper,
                                // it is unnecessary for assets
                                if (importee.endsWith('?commonjs-proxy')) {
                                    s.remove(statementStart, statementEnd);
                                    continue;
                                }
                                const deepPath = `/@fs/${utils_1.normalizePath(utils_1.bareImportRE.test(importee)
                                    ? utils_1.resolveFrom(config.root, importee)
                                    : path_1.default.resolve(path_1.default.dirname(id), importee))}`;
                                s.overwrite(start, end, deepPath);
                            }
                        }
                        else {
                            // ignore dynamic import
                        }
                    }
                    if (s) {
                        return s.toString();
                    }
                }
            }
            return null;
        }
    };
};
//# sourceMappingURL=depAssetPlugin.js.map