@@ -14,28 +14,61 @@ const rename = require('gulp-rename');
1414const util = require ( 'gulp-util' ) ;
1515const buffer = require ( 'gulp-buffer' ) ;
1616const json = require ( 'gulp-json-editor' ) ;
17+ const webpack = require ( 'webpack' ) ;
18+ const webpackGulp = require ( 'webpack-stream' ) ;
1719import * as fs from 'fs' ;
1820import * as path from 'path' ;
1921import * as vsce from 'vsce' ;
2022import * as File from 'vinyl' ;
23+ import { rebase } from './util' ;
2124
2225export function fromLocal ( extensionPath : string ) : Stream {
23- const result = es . through ( ) ;
24-
25- vsce . listFiles ( { cwd : extensionPath , packageManager : vsce . PackageManager . Yarn } )
26- . then ( fileNames => {
27- const files = fileNames
28- . map ( fileName => path . join ( extensionPath , fileName ) )
29- . map ( filePath => new File ( {
30- path : filePath ,
31- stat : fs . statSync ( filePath ) ,
32- base : extensionPath ,
33- contents : fs . createReadStream ( filePath ) as any
26+ let result = es . through ( ) ;
27+
28+ vsce . listFiles ( { cwd : extensionPath , packageManager : vsce . PackageManager . Yarn } ) . then ( fileNames => {
29+ const files = fileNames
30+ . map ( fileName => path . join ( extensionPath , fileName ) )
31+ . map ( filePath => new File ( {
32+ path : filePath ,
33+ stat : fs . statSync ( filePath ) ,
34+ base : extensionPath ,
35+ contents : fs . createReadStream ( filePath ) as any
36+ } ) ) ;
37+
38+ const filesStream = es . readArray ( files ) ;
39+
40+ // check for a webpack configuration file, then invoke webpack
41+ // and merge its output with the files stream. also rewrite the package.json
42+ // file to a new entry point
43+ if ( fs . existsSync ( path . join ( extensionPath , 'extension.webpack.config.js' ) ) ) {
44+ const packageJsonFilter = filter ( 'package.json' , { restore : true } ) ;
45+
46+ const patchFilesStream = filesStream
47+ . pipe ( packageJsonFilter )
48+ . pipe ( buffer ( ) )
49+ . pipe ( json ( { main : './dist/main.bundle' } ) ) // hardcoded entry point!
50+ . pipe ( packageJsonFilter . restore ) ;
51+
52+ const webpackConfig = require ( path . join ( extensionPath , 'extension.webpack.config.js' ) ) ;
53+ const webpackStream = webpackGulp ( webpackConfig , webpack )
54+ . pipe ( es . through ( function ( data ) {
55+ data . base = extensionPath ;
56+ this . emit ( 'data' , data ) ;
3457 } ) ) ;
3558
36- es . readArray ( files ) . pipe ( result ) ;
37- } )
38- . catch ( err => result . emit ( 'error' , err ) ) ;
59+ es . merge ( webpackStream , patchFilesStream )
60+ // .pipe(es.through(function (data) {
61+ // // debug
62+ // console.log('out', data.path, data.base, data.contents.length);
63+ // this.emit('data', data);
64+ // }))
65+ . pipe ( result ) ;
66+
67+ } else {
68+ filesStream . pipe ( result ) ;
69+ }
70+
71+ } ) . catch ( err => result . emit ( 'error' , err ) ) ;
3972
4073 return result ;
4174}
0 commit comments