difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
 
const Path = require('path');
allowReturnOutsideFunctionInBrowserifyTransform();
const Fs = require('fs');
 
const preludePath = Path.resolve(__dirname, '../browserify_prelude.js');
const prelude = Fs.readFileSync(preludePath, 'utf8');
 
function allowReturnOutsideFunctionInBrowserifyTransform () {
    var paths = [
        'browserify/node_modules/syntax-error/node_modules/acorn',
        'syntax-error/node_modules/acorn',
        'acorn'
    ];
    function patch (path) {
        var acorn = require(path);
        var parse = acorn.parse;
        if (typeof parse === 'function') {
            if (acorn.parse.name !== 'monkeyPatchedParse') {
                acorn.parse = function monkeyPatchedParse(input, options) {
                    if (options) {
                        options.allowReturnOutsideFunction = true;
                    }
                    else {
                        options = {
                            allowReturnOutsideFunction: true
                        };
                    }
                    return parse(input, options);
                };
            }
        }
        else {
            console.error('Can not find acorn.parse to patch');
        }
    }
 
    var patched = false;
    for (var i = 0; i < paths.length; i++) {
        try {
            patch(paths[i]);
            patched = true;
        }
        catch (e) {
        }
    }
    if (!patched) {
        console.error('Can not find acorn to patch');
    }
}
 
/*
 * @param [options.sourcemaps = true]
 * @param [options.babelifyOpt]
 * @param [options.aliasifyConfig]
 */
module.exports = function createBundler(entryFiles, options) {
    // https://github.com/substack/node-browserify#methods
    var browserifyOpt = {
        entries: [].concat(entryFiles),
        debug: (options && 'sourcemaps' in options) ? options.sourcemaps : true,
        detectGlobals: false,    // dont insert `process`, `global`, `__filename`, and `__dirname`
        bundleExternal: false,   // dont bundle external modules
        //standalone: 'engine-framework',
        //basedir: tempScriptDir
 
        // define custom prelude to optimize script evaluate time
        prelude: prelude,
        preludePath: Path.relative(process.cwd(), preludePath),
    };
 
    // var presets = [
    //     [ 'es2015', { loose: true } ],
    // ];
 
    var plugins = [
        // https://babeljs.io/docs/plugins/transform-es2015-shorthand-properties/
        'babel-plugin-transform-es2015-shorthand-properties',
        // https://babeljs.io/docs/plugins/transform-es2015-template-literals/
        'babel-plugin-transform-es2015-template-literals',
        // http://babeljs.io/docs/plugins/transform-es2015-block-scoping/
        'babel-plugin-transform-es2015-block-scoping',
 
        // < 6.16.0
        [ 'babel-plugin-parser-opts', { allowReturnOutsideFunction: true } ]
    ];
 
    var Babelify;
    try {
        Babelify = require('babelify');
    } catch (e) {
        console.error('Please run "npm install babelify".');
        throw e;
    }
    var aliasify;
    try {
        aliasify = require('aliasify');
    } catch (e) {
        console.error('Please run "npm install aliasify".');
        throw e;
    }
 
    var b;
    if (options && options.cacheDir) {
        // https://github.com/royriojas/persistify
        const Persistify = require('persistify');
        b = Persistify(browserifyOpt, {
            recreate: false,
            cacheId: require('../../package.json').version + entryFiles,
            cacheDir: options.cacheDir
        });
    }
    else {
        const Browserify = require('browserify');
        b = new Browserify(browserifyOpt);
    }
 
    return b
        .exclude(Path.join(__dirname, '../../package.json'))
        .transform(Babelify, (options && options.babelifyOpt) || {
            // presets: presets,
            plugins: plugins,
 
            // >= 6.16.0
            // parserOpts: {
            //     allowReturnOutsideFunction: true,
            // },
 
            ast: false,
            babelrc: false,
            highlightCode: false,
            sourceMaps: true,
            compact: false
        })
        .transform(aliasify, (options && options.aliasifyConfig) || {});
};