microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
gulp_scripts/builder.js
131lines · modecode
| 1 | const gulp = require("gulp"); |
| 2 | const { series } = require("gulp"); |
| 3 | const getFormatter = require("./formatter"); |
| 4 | const getWebpackBundle = require("./webpackBundle"); |
| 5 | const getCleaner = require("./cleaner"); |
| 6 | const ts = require("gulp-typescript"); |
| 7 | const sourcemaps = require("gulp-sourcemaps"); |
| 8 | const nls = require("vscode-nls-dev"); |
| 9 | const filter = require("gulp-filter"); |
| 10 | const minimist = require("minimist"); |
| 11 | const log = require("fancy-log"); |
| 12 | const preprocess = require("gulp-preprocess"); |
| 13 | const es = require("event-stream"); |
| 14 | |
| 15 | const tsProject = ts.createProject("tsconfig.json"); |
| 16 | const knownOptions = { |
| 17 | string: "env", |
| 18 | default: { env: "production" }, |
| 19 | }; |
| 20 | const options = minimist(process.argv.slice(2), knownOptions); |
| 21 | |
| 22 | const buildTask = gulp.series(getFormatter.lint, function runBuild(done) { |
| 23 | build(true, true).once("finish", () => { |
| 24 | done(); |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | // Generates ./dist/nls.bundle.<language_id>.json from files in ./i18n/** *//<src_path>/<filename>.i18n.json |
| 29 | // Localized strings are read from these files at runtime. |
| 30 | const generateSrcLocBundle = () => { |
| 31 | // Transpile the TS to JS, and let vscode-nls-dev scan the files for calls to localize. |
| 32 | return tsProject |
| 33 | .src() |
| 34 | .pipe(sourcemaps.init()) |
| 35 | .pipe(tsProject()) |
| 36 | .js.pipe(nls.createMetaDataFiles()) |
| 37 | .pipe(nls.createAdditionalLanguageFiles(defaultLanguages, "i18n")) |
| 38 | .pipe(nls.bundleMetaDataFiles(fullExtensionName, "dist")) |
| 39 | .pipe(nls.bundleLanguageFiles()) |
| 40 | .pipe( |
| 41 | filter([ |
| 42 | "**/nls.bundle.*.json", |
| 43 | "**/nls.metadata.header.json", |
| 44 | "**/nls.metadata.json", |
| 45 | "!src/**", |
| 46 | ]), |
| 47 | ) |
| 48 | .pipe(gulp.dest("dist")); |
| 49 | }; |
| 50 | |
| 51 | const defaultLanguages = [ |
| 52 | { id: "zh-tw", folderName: "cht", transifexId: "zh-hant" }, |
| 53 | { id: "zh-cn", folderName: "chs", transifexId: "zh-hans" }, |
| 54 | { id: "ja", folderName: "jpn" }, |
| 55 | { id: "ko", folderName: "kor" }, |
| 56 | { id: "de", folderName: "deu" }, |
| 57 | { id: "fr", folderName: "fra" }, |
| 58 | { id: "es", folderName: "esn" }, |
| 59 | { id: "ru", folderName: "rus" }, |
| 60 | { id: "it", folderName: "ita" }, |
| 61 | |
| 62 | // These language-pack languages are included for VS but excluded from the vscode package |
| 63 | { id: "cs", folderName: "csy" }, |
| 64 | { id: "tr", folderName: "trk" }, |
| 65 | { id: "pt-br", folderName: "ptb", transifexId: "pt-BR" }, |
| 66 | { id: "pl", folderName: "plk" }, |
| 67 | ]; |
| 68 | |
| 69 | /** |
| 70 | * Whether we're running a nightly build. |
| 71 | */ |
| 72 | const isNightly = process.argv.includes("--nightly"); |
| 73 | |
| 74 | const fullExtensionName = isNightly |
| 75 | ? "msjsdiag.vscode-react-native-preview" |
| 76 | : "msjsdiag.vscode-react-native"; |
| 77 | |
| 78 | function build(failOnError, buildNls) { |
| 79 | const isProd = options.env === "production"; |
| 80 | const preprocessorContext = isProd ? { PROD: true } : { DEBUG: true }; |
| 81 | let gotError = false; |
| 82 | log(`Building with preprocessor context: ${JSON.stringify(preprocessorContext)}`); |
| 83 | const tsResult = tsProject |
| 84 | .src() |
| 85 | .pipe(preprocess({ context: preprocessorContext })) //To set environment variables in-line |
| 86 | .pipe(sourcemaps.init()) |
| 87 | .pipe(tsProject()); |
| 88 | |
| 89 | return tsResult.js |
| 90 | .pipe(buildNls ? nls.rewriteLocalizeCalls() : es.through()) |
| 91 | .pipe( |
| 92 | buildNls |
| 93 | ? nls.createAdditionalLanguageFiles(defaultLanguages, "i18n", ".") |
| 94 | : es.through(), |
| 95 | ) |
| 96 | .pipe(buildNls ? nls.bundleMetaDataFiles(fullExtensionName, ".") : es.through()) |
| 97 | .pipe(buildNls ? nls.bundleLanguageFiles() : es.through()) |
| 98 | .pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "." })) |
| 99 | .pipe(gulp.dest(file => file.cwd)) |
| 100 | .once("error", () => { |
| 101 | gotError = true; |
| 102 | }) |
| 103 | .once("finish", () => { |
| 104 | if (failOnError && gotError) { |
| 105 | process.exit(1); |
| 106 | } |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | // TODO: The file property should point to the generated source (this implementation adds an extra folder to the path) |
| 111 | // We should also make sure that we always generate urls in all the path properties (We shouldn"t have \\s. This seems to |
| 112 | // be an issue on Windows platforms) |
| 113 | function runBuild(done) { |
| 114 | build(true, true).once("finish", () => { |
| 115 | done(); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | function buildDev(done) { |
| 120 | build(true, false).once("finish", () => { |
| 121 | done(); |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | const buildProd = series(getCleaner.clean, getWebpackBundle.webpackBundle, generateSrcLocBundle); |
| 126 | |
| 127 | module.exports = { |
| 128 | buildTask, |
| 129 | buildDev, |
| 130 | buildProd, |
| 131 | }; |
| 132 | |