extensions.js 9.9 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6
"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
M
Matt Bierner 已提交
7
exports.packageMarketplaceExtensionsStream = exports.packageLocalExtensionsStream = exports.fromMarketplace = void 0;
8 9 10 11 12 13 14 15 16
const es = require("event-stream");
const fs = require("fs");
const glob = require("glob");
const gulp = require("gulp");
const path = require("path");
const File = require("vinyl");
const vsce = require("vsce");
const stats_1 = require("./stats");
const util2 = require("./util");
J
Joao Moreno 已提交
17
const remote = require("gulp-remote-retry-src");
18 19 20
const vzip = require('gulp-vinyl-zip');
const filter = require("gulp-filter");
const rename = require("gulp-rename");
A
Alex Dima 已提交
21 22
const fancyLog = require("fancy-log");
const ansiColors = require("ansi-colors");
23 24 25 26
const buffer = require('gulp-buffer');
const json = require("gulp-json-editor");
const webpack = require('webpack');
const webpackGulp = require('webpack-stream');
J
Joao Moreno 已提交
27
const util = require('./util');
J
Joao Moreno 已提交
28 29 30 31
const root = path.dirname(path.dirname(__dirname));
const commit = util.getVersion(root);
const sourceMappingURLBase = `https://ticino.blob.core.windows.net/sourcemaps/${commit}`;
function fromLocal(extensionPath) {
32
    const webpackFilename = path.join(extensionPath, 'extension.webpack.config.js');
J
Joao Moreno 已提交
33 34 35 36 37 38 39 40 41 42 43 44
    const input = fs.existsSync(webpackFilename)
        ? fromLocalWebpack(extensionPath)
        : fromLocalNormal(extensionPath);
    const tmLanguageJsonFilter = filter('**/*.tmLanguage.json', { restore: true });
    return input
        .pipe(tmLanguageJsonFilter)
        .pipe(buffer())
        .pipe(es.mapSync((f) => {
        f.contents = Buffer.from(JSON.stringify(JSON.parse(f.contents.toString('utf8'))));
        return f;
    }))
        .pipe(tmLanguageJsonFilter.restore);
J
Johannes Rieken 已提交
45
}
J
Joao Moreno 已提交
46
function fromLocalWebpack(extensionPath) {
47 48 49
    const result = es.through();
    const packagedDependencies = [];
    const packageJsonConfig = require(path.join(extensionPath, 'package.json'));
C
Christof Marti 已提交
50 51 52 53 54 55
    if (packageJsonConfig.dependencies) {
        const webpackRootConfig = require(path.join(extensionPath, 'extension.webpack.config.js'));
        for (const key in webpackRootConfig.externals) {
            if (key in packageJsonConfig.dependencies) {
                packagedDependencies.push(key);
            }
J
Johannes Rieken 已提交
56 57
        }
    }
58 59 60 61
    vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn, packagedDependencies }).then(fileNames => {
        const files = fileNames
            .map(fileName => path.join(extensionPath, fileName))
            .map(filePath => new File({
62
            path: filePath,
J
Joao Moreno 已提交
63
            stat: fs.statSync(filePath),
64 65
            base: extensionPath,
            contents: fs.createReadStream(filePath)
66 67
        }));
        const filesStream = es.readArray(files);
68
        // check for a webpack configuration files, then invoke webpack
69 70
        // and merge its output with the files stream. also rewrite the package.json
        // file to a new entry point
71 72
        const webpackConfigLocations = glob.sync(path.join(extensionPath, '/**/extension.webpack.config.js'), { ignore: ['**/node_modules'] });
        const packageJsonFilter = filter(f => {
J
Johannes Rieken 已提交
73 74 75 76 77 78 79
            if (path.basename(f.path) === 'package.json') {
                // only modify package.json's next to the webpack file.
                // to be safe, use existsSync instead of path comparison.
                return fs.existsSync(path.join(path.dirname(f.path), 'extension.webpack.config.js'));
            }
            return false;
        }, { restore: true });
80
        const patchFilesStream = filesStream
J
Johannes Rieken 已提交
81 82
            .pipe(packageJsonFilter)
            .pipe(buffer())
83
            .pipe(json((data) => {
A
Alex Dima 已提交
84 85 86 87
            if (data.main) {
                // hardcoded entry point directory!
                data.main = data.main.replace('/out/', /dist/);
            }
J
Johannes Rieken 已提交
88 89 90
            return data;
        }))
            .pipe(packageJsonFilter.restore);
J
Joao Moreno 已提交
91
        const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
92
            const webpackDone = (err, stats) => {
A
Alex Dima 已提交
93
                fancyLog(`Bundled extension: ${ansiColors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
J
Johannes Rieken 已提交
94 95 96
                if (err) {
                    result.emit('error', err);
                }
97
                const { compilation } = stats;
J
Johannes Rieken 已提交
98 99
                if (compilation.errors.length > 0) {
                    result.emit('error', compilation.errors.join('\n'));
100
                }
J
Johannes Rieken 已提交
101 102 103 104
                if (compilation.warnings.length > 0) {
                    result.emit('error', compilation.warnings.join('\n'));
                }
            };
M
Matt Bierner 已提交
105
            const webpackConfig = Object.assign(Object.assign({}, require(webpackConfigPath)), { mode: 'production' });
106
            const relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
J
Johannes Rieken 已提交
107 108 109 110 111
            return webpackGulp(webpackConfig, webpack, webpackDone)
                .pipe(es.through(function (data) {
                data.stat = data.stat || {};
                data.base = extensionPath;
                this.emit('data', data);
112
            }))
J
Johannes Rieken 已提交
113 114 115 116
                .pipe(es.through(function (data) {
                // source map handling:
                // * rewrite sourceMappingURL
                // * save to disk so that upload-task picks this up
J
Joao Moreno 已提交
117 118 119 120
                const contents = data.contents.toString('utf8');
                data.contents = Buffer.from(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, function (_m, g1) {
                    return `\n//# sourceMappingURL=${sourceMappingURLBase}/extensions/${path.basename(extensionPath)}/${relativeOutputPath}/${g1}`;
                }), 'utf8');
J
Johannes Rieken 已提交
121 122 123
                this.emit('data', data);
            }));
        });
J
Joao Moreno 已提交
124
        es.merge(...webpackStreams, patchFilesStream)
125 126 127 128 129 130 131
            // .pipe(es.through(function (data) {
            // 	// debug
            // 	console.log('out', data.path, data.contents.length);
            // 	this.emit('data', data);
            // }))
            .pipe(result);
    }).catch(err => {
132 133 134 135
        console.error(extensionPath);
        console.error(packagedDependencies);
        result.emit('error', err);
    });
136
    return result.pipe(stats_1.createStatsStream(path.basename(extensionPath)));
137
}
J
Johannes Rieken 已提交
138
function fromLocalNormal(extensionPath) {
139
    const result = es.through();
J
Johannes Rieken 已提交
140
    vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn })
141 142 143 144
        .then(fileNames => {
        const files = fileNames
            .map(fileName => path.join(extensionPath, fileName))
            .map(filePath => new File({
J
Johannes Rieken 已提交
145 146 147 148
            path: filePath,
            stat: fs.statSync(filePath),
            base: extensionPath,
            contents: fs.createReadStream(filePath)
149
        }));
J
Johannes Rieken 已提交
150 151
        es.readArray(files).pipe(result);
    })
152
        .catch(err => result.emit('error', err));
153
    return result.pipe(stats_1.createStatsStream(path.basename(extensionPath)));
J
Johannes Rieken 已提交
154
}
155
const baseHeaders = {
J
Joao Moreno 已提交
156 157
    'X-Market-Client-Id': 'VSCode Build',
    'User-Agent': 'VSCode Build',
J
Joao Moreno 已提交
158
    'X-Market-User-Id': '291C1CD0-051A-4123-9B4B-30D60EF52EE2',
J
Joao Moreno 已提交
159
};
J
Joao Moreno 已提交
160
function fromMarketplace(extensionName, version, metadata) {
161 162
    const [publisher, name] = extensionName.split('.');
    const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${publisher}/vsextensions/${name}/${version}/vspackage`;
A
Alex Dima 已提交
163
    fancyLog('Downloading extension:', ansiColors.yellow(`${extensionName}@${version}`), '...');
164
    const options = {
J
Joao Moreno 已提交
165
        base: url,
J
Joao Moreno 已提交
166 167
        requestOptions: {
            gzip: true,
J
Joao Moreno 已提交
168
            headers: baseHeaders
J
Joao Moreno 已提交
169 170
        }
    };
171
    const packageJsonFilter = filter('package.json', { restore: true });
J
Joao Moreno 已提交
172
    return remote('', options)
J
build  
Joao Moreno 已提交
173 174
        .pipe(vzip.src())
        .pipe(filter('extension/**'))
175
        .pipe(rename(p => p.dirname = p.dirname.replace(/^extension\/?/, '')))
J
build  
Joao Moreno 已提交
176 177 178 179
        .pipe(packageJsonFilter)
        .pipe(buffer())
        .pipe(json({ __metadata: metadata }))
        .pipe(packageJsonFilter.restore);
J
Joao Moreno 已提交
180
}
181
exports.fromMarketplace = fromMarketplace;
182
const excludedExtensions = [
183
    'vscode-api-tests',
M
Matt Bierner 已提交
184
    'vscode-web-playground',
185
    'vscode-colorize-tests',
A
Alex Dima 已提交
186
    'vscode-test-resolver',
187 188
    'ms-vscode.node-debug',
    'ms-vscode.node-debug2',
R
rebornix 已提交
189
    'vscode-notebook-tests'
190
];
191
const builtInExtensions = JSON.parse(fs.readFileSync(path.join(__dirname, '../../product.json'), 'utf8')).builtInExtensions;
J
Joao Moreno 已提交
192
function packageLocalExtensionsStream() {
193 194 195 196
    const localExtensionDescriptions = glob.sync('extensions/*/package.json')
        .map(manifestPath => {
        const extensionPath = path.dirname(path.join(root, manifestPath));
        const extensionName = path.basename(extensionPath);
197 198
        return { name: extensionName, path: extensionPath };
    })
199 200
        .filter(({ name }) => excludedExtensions.indexOf(name) === -1)
        .filter(({ name }) => builtInExtensions.every(b => b.name !== name));
201 202
    const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
    const localExtensions = localExtensionDescriptions.map(extension => {
J
Joao Moreno 已提交
203 204
        return fromLocal(extension.path)
            .pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
205 206 207
    });
    return es.merge(nodeModules, ...localExtensions)
        .pipe(util2.setExecutableBit(['**/*.sh']));
J
Joao Moreno 已提交
208 209 210
}
exports.packageLocalExtensionsStream = packageLocalExtensionsStream;
function packageMarketplaceExtensionsStream() {
211
    const extensions = builtInExtensions.map(extension => {
J
Joao Moreno 已提交
212
        return fromMarketplace(extension.name, extension.version, extension.metadata)
213
            .pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
214 215 216
    });
    return es.merge(extensions)
        .pipe(util2.setExecutableBit(['**/*.sh']));
217
}
J
Joao Moreno 已提交
218
exports.packageMarketplaceExtensionsStream = packageMarketplaceExtensionsStream;