未验证 提交 9198116d 编写于 作者: A Alex Dima

Add `IOptimizeTaskOpts.fileContentMapper`

上级 15e798e0
...@@ -70,7 +70,7 @@ function loader(src, bundledFileHeader, bundleLoader) { ...@@ -70,7 +70,7 @@ function loader(src, bundledFileHeader, bundleLoader) {
})) }))
.pipe(concat('vs/loader.js'))); .pipe(concat('vs/loader.js')));
} }
function toConcatStream(src, bundledFileHeader, sources, dest) { function toConcatStream(src, bundledFileHeader, sources, dest, fileContentMapper) {
const useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest); const useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest);
// If a bundle ends up including in any of the sources our copyright, then // If a bundle ends up including in any of the sources our copyright, then
// insert a fake source at the beginning of each bundle with our copyright // insert a fake source at the beginning of each bundle with our copyright
...@@ -91,10 +91,12 @@ function toConcatStream(src, bundledFileHeader, sources, dest) { ...@@ -91,10 +91,12 @@ function toConcatStream(src, bundledFileHeader, sources, dest) {
const treatedSources = sources.map(function (source) { const treatedSources = sources.map(function (source) {
const root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : ''; const root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : '';
const base = source.path ? root + `/${src}` : ''; const base = source.path ? root + `/${src}` : '';
const path = source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake';
const contents = source.path ? fileContentMapper(source.contents, path) : source.contents;
return new VinylFile({ return new VinylFile({
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake', path: path,
base: base, base: base,
contents: Buffer.from(source.contents) contents: Buffer.from(contents)
}); });
}); });
return es.readArray(treatedSources) return es.readArray(treatedSources)
...@@ -102,9 +104,9 @@ function toConcatStream(src, bundledFileHeader, sources, dest) { ...@@ -102,9 +104,9 @@ function toConcatStream(src, bundledFileHeader, sources, dest) {
.pipe(concat(dest)) .pipe(concat(dest))
.pipe(stats_1.createStatsStream(dest)); .pipe(stats_1.createStatsStream(dest));
} }
function toBundleStream(src, bundledFileHeader, bundles) { function toBundleStream(src, bundledFileHeader, bundles, fileContentMapper) {
return es.merge(bundles.map(function (bundle) { return es.merge(bundles.map(function (bundle) {
return toConcatStream(src, bundledFileHeader, bundle.sources, bundle.dest); return toConcatStream(src, bundledFileHeader, bundle.sources, bundle.dest, fileContentMapper);
})); }));
} }
const DEFAULT_FILE_HEADER = [ const DEFAULT_FILE_HEADER = [
...@@ -120,6 +122,7 @@ function optimizeTask(opts) { ...@@ -120,6 +122,7 @@ function optimizeTask(opts) {
const bundledFileHeader = opts.header || DEFAULT_FILE_HEADER; const bundledFileHeader = opts.header || DEFAULT_FILE_HEADER;
const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader); const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
const out = opts.out; const out = opts.out;
const fileContentMapper = opts.fileContentMapper || ((contents, _path) => contents);
return function () { return function () {
const bundlesStream = es.through(); // this stream will contain the bundled files const bundlesStream = es.through(); // this stream will contain the bundled files
const resourcesStream = es.through(); // this stream will contain the resources const resourcesStream = es.through(); // this stream will contain the resources
...@@ -128,7 +131,7 @@ function optimizeTask(opts) { ...@@ -128,7 +131,7 @@ function optimizeTask(opts) {
if (err || !result) { if (err || !result) {
return bundlesStream.emit('error', JSON.stringify(err)); return bundlesStream.emit('error', JSON.stringify(err));
} }
toBundleStream(src, bundledFileHeader, result.files).pipe(bundlesStream); toBundleStream(src, bundledFileHeader, result.files, fileContentMapper).pipe(bundlesStream);
// Remove css inlined resources // Remove css inlined resources
const filteredResources = resources.slice(); const filteredResources = resources.slice();
result.cssInlinedResources.forEach(function (resource) { result.cssInlinedResources.forEach(function (resource) {
......
...@@ -18,7 +18,6 @@ import * as fancyLog from 'fancy-log'; ...@@ -18,7 +18,6 @@ import * as fancyLog from 'fancy-log';
import * as ansiColors from 'ansi-colors'; import * as ansiColors from 'ansi-colors';
import * as path from 'path'; import * as path from 'path';
import * as pump from 'pump'; import * as pump from 'pump';
import * as sm from 'source-map';
import * as terser from 'terser'; import * as terser from 'terser';
import * as VinylFile from 'vinyl'; import * as VinylFile from 'vinyl';
import * as bundle from './bundle'; import * as bundle from './bundle';
...@@ -48,10 +47,6 @@ export function loaderConfig() { ...@@ -48,10 +47,6 @@ export function loaderConfig() {
const IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i; const IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
declare class FileSourceMap extends VinylFile {
public sourceMap: sm.RawSourceMap;
}
function loader(src: string, bundledFileHeader: string, bundleLoader: boolean): NodeJS.ReadWriteStream { function loader(src: string, bundledFileHeader: string, bundleLoader: boolean): NodeJS.ReadWriteStream {
let sources = [ let sources = [
`${src}/vs/loader.js` `${src}/vs/loader.js`
...@@ -84,7 +79,7 @@ function loader(src: string, bundledFileHeader: string, bundleLoader: boolean): ...@@ -84,7 +79,7 @@ function loader(src: string, bundledFileHeader: string, bundleLoader: boolean):
); );
} }
function toConcatStream(src: string, bundledFileHeader: string, sources: bundle.IFile[], dest: string): NodeJS.ReadWriteStream { function toConcatStream(src: string, bundledFileHeader: string, sources: bundle.IFile[], dest: string, fileContentMapper: (contents: string, path: string) => string): NodeJS.ReadWriteStream {
const useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest); const useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest);
// If a bundle ends up including in any of the sources our copyright, then // If a bundle ends up including in any of the sources our copyright, then
...@@ -108,11 +103,13 @@ function toConcatStream(src: string, bundledFileHeader: string, sources: bundle. ...@@ -108,11 +103,13 @@ function toConcatStream(src: string, bundledFileHeader: string, sources: bundle.
const treatedSources = sources.map(function (source) { const treatedSources = sources.map(function (source) {
const root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : ''; const root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : '';
const base = source.path ? root + `/${src}` : ''; const base = source.path ? root + `/${src}` : '';
const path = source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake';
const contents = source.path ? fileContentMapper(source.contents, path) : source.contents;
return new VinylFile({ return new VinylFile({
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake', path: path,
base: base, base: base,
contents: Buffer.from(source.contents) contents: Buffer.from(contents)
}); });
}); });
...@@ -122,9 +119,9 @@ function toConcatStream(src: string, bundledFileHeader: string, sources: bundle. ...@@ -122,9 +119,9 @@ function toConcatStream(src: string, bundledFileHeader: string, sources: bundle.
.pipe(createStatsStream(dest)); .pipe(createStatsStream(dest));
} }
function toBundleStream(src: string, bundledFileHeader: string, bundles: bundle.IConcatFile[]): NodeJS.ReadWriteStream { function toBundleStream(src: string, bundledFileHeader: string, bundles: bundle.IConcatFile[], fileContentMapper: (contents: string, path: string) => string): NodeJS.ReadWriteStream {
return es.merge(bundles.map(function (bundle) { return es.merge(bundles.map(function (bundle) {
return toConcatStream(src, bundledFileHeader, bundle.sources, bundle.dest); return toConcatStream(src, bundledFileHeader, bundle.sources, bundle.dest, fileContentMapper);
})); }));
} }
...@@ -162,6 +159,12 @@ export interface IOptimizeTaskOpts { ...@@ -162,6 +159,12 @@ export interface IOptimizeTaskOpts {
* (out folder name) * (out folder name)
*/ */
languages?: Language[]; languages?: Language[];
/**
* File contents interceptor
* @param contents The contens of the file
* @param path The absolute file path, always using `/`, even on Windows
*/
fileContentMapper?: (contents: string, path: string) => string;
} }
const DEFAULT_FILE_HEADER = [ const DEFAULT_FILE_HEADER = [
...@@ -178,6 +181,7 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr ...@@ -178,6 +181,7 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr
const bundledFileHeader = opts.header || DEFAULT_FILE_HEADER; const bundledFileHeader = opts.header || DEFAULT_FILE_HEADER;
const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader); const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
const out = opts.out; const out = opts.out;
const fileContentMapper = opts.fileContentMapper || ((contents: string, _path: string) => contents);
return function () { return function () {
const bundlesStream = es.through(); // this stream will contain the bundled files const bundlesStream = es.through(); // this stream will contain the bundled files
...@@ -187,7 +191,7 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr ...@@ -187,7 +191,7 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr
bundle.bundle(entryPoints, loaderConfig, function (err, result) { bundle.bundle(entryPoints, loaderConfig, function (err, result) {
if (err || !result) { return bundlesStream.emit('error', JSON.stringify(err)); } if (err || !result) { return bundlesStream.emit('error', JSON.stringify(err)); }
toBundleStream(src, bundledFileHeader, result.files).pipe(bundlesStream); toBundleStream(src, bundledFileHeader, result.files, fileContentMapper).pipe(bundlesStream);
// Remove css inlined resources // Remove css inlined resources
const filteredResources = resources.slice(); const filteredResources = resources.slice();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册