File size: 3,635 Bytes
c592d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// Utilties for configuring the bundler to use.
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
0 && (module.exports = {
    Bundler: null,
    finalizeBundlerFromConfig: null,
    parseBundlerArgs: null
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    Bundler: function() {
        return Bundler;
    },
    finalizeBundlerFromConfig: function() {
        return finalizeBundlerFromConfig;
    },
    parseBundlerArgs: function() {
        return parseBundlerArgs;
    }
});
var Bundler = /*#__PURE__*/ function(Bundler) {
    Bundler[Bundler["Turbopack"] = 0] = "Turbopack";
    Bundler[Bundler["Webpack"] = 1] = "Webpack";
    Bundler[Bundler["Rspack"] = 2] = "Rspack";
    return Bundler;
}({});
function parseBundlerArgs(options) {
    const bundlerFlags = new Map();
    const setBundlerFlag = (bundler, flag)=>{
        bundlerFlags.set(bundler, (bundlerFlags.get(bundler) ?? []).concat(flag));
    };
    // What turbo flag was set? We allow multiple to be set, which is silly but not ambiguous, just pick the most relevant one.
    if (options.turbopack) {
        setBundlerFlag(0, '--turbopack');
    }
    if (options.turbo) {
        setBundlerFlag(0, '--turbo');
    } else if (process.env.TURBOPACK) {
        // We don't really want to support this but it is trivial and not really confusing.
        // If we don't support it and someone sets it, we would have inconsistent behavior
        // since some parts of next would read the return value of this function and other
        // parts will read the env variable.
        setBundlerFlag(0, `TURBOPACK=${process.env.TURBOPACK}`);
    } else if (process.env.IS_TURBOPACK_TEST) {
        setBundlerFlag(0, `IS_TURBOPACK_TEST=${process.env.IS_TURBOPACK_TEST}`);
    }
    if (options.webpack) {
        setBundlerFlag(1, '--webpack');
    }
    if (process.env.IS_WEBPACK_TEST) {
        setBundlerFlag(1, `IS_WEBPACK_TEST=${process.env.IS_WEBPACK_TEST}`);
    }
    // Mostly this is set via the NextConfig but it can also be set via the command line which is
    // common for testing.
    if (process.env.NEXT_RSPACK) {
        setBundlerFlag(2, `NEXT_RSPACK=${process.env.NEXT_RSPACK}`);
    }
    if (process.env.NEXT_TEST_USE_RSPACK) {
        setBundlerFlag(2, `NEXT_TEST_USE_RSPACK=${process.env.NEXT_TEST_USE_RSPACK}`);
    }
    if (bundlerFlags.size > 1) {
        console.error(`Multiple bundler flags set: ${Array.from(bundlerFlags.values()).flat().join(', ')}.

Edit your command or your package.json script to configure only one bundler.`);
        process.exit(1);
    }
    // The default is turbopack when nothing is configured.
    if (bundlerFlags.size === 0) {
        process.env.TURBOPACK = 'auto';
        return 0;
    }
    if (bundlerFlags.has(0)) {
        // Only conditionally assign to the environment variable, preserving already set values.
        // If it was set to 'auto' because no flag was set and this function is called a second time we
        // would upgrade to '1' but we don't really want that.
        process.env.TURBOPACK ??= '1';
        return 0;
    }
    // Otherwise it is one of rspack or webpack. At this point there must be exactly one key in the map.
    return bundlerFlags.keys().next().value;
}
function finalizeBundlerFromConfig(fromOptions) {
    // Reading the next config can set NEXT_RSPACK environment variables.
    if (process.env.NEXT_RSPACK) {
        return 2;
    }
    return fromOptions;
}

//# sourceMappingURL=bundler.js.map