Skip to content

Commit 9de8a85

Browse files
committed
extensions in web build
1 parent dbb542d commit 9de8a85

4 files changed

Lines changed: 138 additions & 73 deletions

File tree

build/lib/extensions.js

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66
Object.defineProperty(exports, "__esModule", { value: true });
7-
exports.packageMarketplaceExtensionsStream = exports.packageLocalExtensionsStream = exports.fromMarketplace = void 0;
7+
exports.packageMarketplaceExtensionsStream = exports.packageLocalWebExtensionsStream = exports.packageLocalExtensionsStream = exports.fromMarketplace = void 0;
88
const es = require("event-stream");
99
const fs = require("fs");
1010
const glob = require("glob");
@@ -28,11 +28,7 @@ const util = require('./util');
2828
const root = path.dirname(path.dirname(__dirname));
2929
const commit = util.getVersion(root);
3030
const sourceMappingURLBase = `https://ticino.blob.core.windows.net/sourcemaps/${commit}`;
31-
function fromLocal(extensionPath) {
32-
const webpackFilename = path.join(extensionPath, 'extension.webpack.config.js');
33-
const input = fs.existsSync(webpackFilename)
34-
? fromLocalWebpack(extensionPath)
35-
: fromLocalNormal(extensionPath);
31+
function minimizeLanguageJSON(input) {
3632
const tmLanguageJsonFilter = filter('**/*.tmLanguage.json', { restore: true });
3733
return input
3834
.pipe(tmLanguageJsonFilter)
@@ -43,12 +39,49 @@ function fromLocal(extensionPath) {
4339
}))
4440
.pipe(tmLanguageJsonFilter.restore);
4541
}
46-
function fromLocalWebpack(extensionPath) {
42+
function updateExtensionPackageJSON(extensionPath, input, update) {
43+
const packageJsonFilter = filter((f) => f.path === path.join(extensionPath, 'package.json'), { restore: true });
44+
return input
45+
.pipe(packageJsonFilter)
46+
.pipe(buffer())
47+
.pipe(es.mapSync((f) => {
48+
const data = JSON.parse(f.contents.toString('utf8'));
49+
f.contents = Buffer.from(JSON.stringify(update(data)));
50+
return f;
51+
}))
52+
.pipe(packageJsonFilter.restore);
53+
}
54+
function fromLocal(extensionPath, forWeb) {
55+
const webpackConfigFileName = forWeb ? 'extension-browser.webpack.config.js' : 'extension.webpack.config.js';
56+
const isWebPacked = fs.existsSync(path.join(extensionPath, webpackConfigFileName));
57+
let input = isWebPacked
58+
? fromLocalWebpack(extensionPath, webpackConfigFileName)
59+
: fromLocalNormal(extensionPath);
60+
if (forWeb) {
61+
input = updateExtensionPackageJSON(extensionPath, input, (data) => {
62+
if (data.browser) {
63+
data.main = data.browser;
64+
}
65+
data.extensionKind = ['web'];
66+
return data;
67+
});
68+
}
69+
else if (isWebPacked) {
70+
input = updateExtensionPackageJSON(extensionPath, input, (data) => {
71+
if (data.main) {
72+
data.main = data.main.replace('/out/', /dist/);
73+
}
74+
return data;
75+
});
76+
}
77+
return minimizeLanguageJSON(input);
78+
}
79+
function fromLocalWebpack(extensionPath, webpackConfigFileName) {
4780
const result = es.through();
4881
const packagedDependencies = [];
4982
const packageJsonConfig = require(path.join(extensionPath, 'package.json'));
5083
if (packageJsonConfig.dependencies) {
51-
const webpackRootConfig = require(path.join(extensionPath, 'extension.webpack.config.js'));
84+
const webpackRootConfig = require(path.join(extensionPath, webpackConfigFileName));
5285
for (const key in webpackRootConfig.externals) {
5386
if (key in packageJsonConfig.dependencies) {
5487
packagedDependencies.push(key);
@@ -64,30 +97,9 @@ function fromLocalWebpack(extensionPath) {
6497
base: extensionPath,
6598
contents: fs.createReadStream(filePath)
6699
}));
67-
const filesStream = es.readArray(files);
68100
// check for a webpack configuration files, then invoke webpack
69-
// and merge its output with the files stream. also rewrite the package.json
70-
// file to a new entry point
71-
const webpackConfigLocations = glob.sync(path.join(extensionPath, '/**/extension.webpack.config.js'), { ignore: ['**/node_modules'] });
72-
const packageJsonFilter = filter(f => {
73-
if (path.basename(f.path) === 'package.json') {
74-
// only modify package.json's next to the webpack file.
75-
// to be safe, use existsSync instead of path comparison.
76-
return fs.existsSync(path.join(path.dirname(f.path), 'extension.webpack.config.js'));
77-
}
78-
return false;
79-
}, { restore: true });
80-
const patchFilesStream = filesStream
81-
.pipe(packageJsonFilter)
82-
.pipe(buffer())
83-
.pipe(json((data) => {
84-
if (data.main) {
85-
// hardcoded entry point directory!
86-
data.main = data.main.replace('/out/', /dist/);
87-
}
88-
return data;
89-
}))
90-
.pipe(packageJsonFilter.restore);
101+
// and merge its output with the files stream.
102+
const webpackConfigLocations = glob.sync(path.join(extensionPath, '**', webpackConfigFileName), { ignore: ['**/node_modules'] });
91103
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
92104
const webpackDone = (err, stats) => {
93105
fancyLog(`Bundled extension: ${ansiColors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
@@ -121,7 +133,7 @@ function fromLocalWebpack(extensionPath) {
121133
this.emit('data', data);
122134
}));
123135
});
124-
es.merge(...webpackStreams, patchFilesStream)
136+
es.merge(...webpackStreams, es.readArray(files))
125137
// .pipe(es.through(function (data) {
126138
// // debug
127139
// console.log('out', data.path, data.contents.length);
@@ -198,15 +210,32 @@ function packageLocalExtensionsStream() {
198210
})
199211
.filter(({ name }) => excludedExtensions.indexOf(name) === -1)
200212
.filter(({ name }) => builtInExtensions.every(b => b.name !== name));
201-
const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
202213
const localExtensions = localExtensionDescriptions.map(extension => {
203-
return fromLocal(extension.path)
214+
return fromLocal(extension.path, false)
204215
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
205216
});
217+
const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
206218
return es.merge(nodeModules, ...localExtensions)
207219
.pipe(util2.setExecutableBit(['**/*.sh']));
208220
}
209221
exports.packageLocalExtensionsStream = packageLocalExtensionsStream;
222+
function packageLocalWebExtensionsStream() {
223+
const localExtensionDescriptions = glob.sync('extensions/*/package.json')
224+
.filter(manifestPath => {
225+
const packageJsonConfig = require(path.join(root, manifestPath));
226+
return !packageJsonConfig.main || packageJsonConfig.browser;
227+
})
228+
.map(manifestPath => {
229+
const extensionPath = path.dirname(path.join(root, manifestPath));
230+
const extensionName = path.basename(extensionPath);
231+
return { name: extensionName, path: extensionPath };
232+
});
233+
return es.merge(...localExtensionDescriptions.map(extension => {
234+
return fromLocal(extension.path, true)
235+
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
236+
}));
237+
}
238+
exports.packageLocalWebExtensionsStream = packageLocalWebExtensionsStream;
210239
function packageMarketplaceExtensionsStream() {
211240
const extensions = builtInExtensions.map(extension => {
212241
return fromMarketplace(extension.name, extension.version, extension.metadata)

build/lib/extensions.ts

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ const root = path.dirname(path.dirname(__dirname));
2828
const commit = util.getVersion(root);
2929
const sourceMappingURLBase = `https://ticino.blob.core.windows.net/sourcemaps/${commit}`;
3030

31-
function fromLocal(extensionPath: string): Stream {
32-
const webpackFilename = path.join(extensionPath, 'extension.webpack.config.js');
33-
const input = fs.existsSync(webpackFilename)
34-
? fromLocalWebpack(extensionPath)
35-
: fromLocalNormal(extensionPath);
36-
31+
function minimizeLanguageJSON(input: Stream): Stream {
3732
const tmLanguageJsonFilter = filter('**/*.tmLanguage.json', { restore: true });
38-
3933
return input
4034
.pipe(tmLanguageJsonFilter)
4135
.pipe(buffer())
@@ -46,13 +40,55 @@ function fromLocal(extensionPath: string): Stream {
4640
.pipe(tmLanguageJsonFilter.restore);
4741
}
4842

49-
function fromLocalWebpack(extensionPath: string): Stream {
43+
function updateExtensionPackageJSON(extensionPath: string, input: Stream, update: (data: any) => any): Stream {
44+
const packageJsonFilter = filter((f: File) => f.path === path.join(extensionPath, 'package.json'), { restore: true });
45+
return input
46+
.pipe(packageJsonFilter)
47+
.pipe(buffer())
48+
.pipe(es.mapSync((f: File) => {
49+
const data = JSON.parse(f.contents.toString('utf8'));
50+
f.contents = Buffer.from(JSON.stringify(update(data)));
51+
return f;
52+
}))
53+
.pipe(packageJsonFilter.restore);
54+
}
55+
56+
function fromLocal(extensionPath: string, forWeb: boolean): Stream {
57+
const webpackConfigFileName = forWeb ? 'extension-browser.webpack.config.js' : 'extension.webpack.config.js';
58+
59+
const isWebPacked = fs.existsSync(path.join(extensionPath, webpackConfigFileName));
60+
let input = isWebPacked
61+
? fromLocalWebpack(extensionPath, webpackConfigFileName)
62+
: fromLocalNormal(extensionPath);
63+
64+
if (forWeb) {
65+
input = updateExtensionPackageJSON(extensionPath, input, (data: any) => {
66+
if (data.browser) {
67+
data.main = data.browser;
68+
}
69+
data.extensionKind = ['web'];
70+
return data;
71+
});
72+
} else if (isWebPacked) {
73+
input = updateExtensionPackageJSON(extensionPath, input, (data: any) => {
74+
if (data.main) {
75+
data.main = data.main.replace('/out/', /dist/);
76+
}
77+
return data;
78+
});
79+
}
80+
81+
return minimizeLanguageJSON(input)
82+
}
83+
84+
85+
function fromLocalWebpack(extensionPath: string, webpackConfigFileName: string): Stream {
5086
const result = es.through();
5187

5288
const packagedDependencies: string[] = [];
5389
const packageJsonConfig = require(path.join(extensionPath, 'package.json'));
5490
if (packageJsonConfig.dependencies) {
55-
const webpackRootConfig = require(path.join(extensionPath, 'extension.webpack.config.js'));
91+
const webpackRootConfig = require(path.join(extensionPath, webpackConfigFileName));
5692
for (const key in webpackRootConfig.externals) {
5793
if (key in packageJsonConfig.dependencies) {
5894
packagedDependencies.push(key);
@@ -70,38 +106,13 @@ function fromLocalWebpack(extensionPath: string): Stream {
70106
contents: fs.createReadStream(filePath) as any
71107
}));
72108

73-
const filesStream = es.readArray(files);
74-
75109
// check for a webpack configuration files, then invoke webpack
76-
// and merge its output with the files stream. also rewrite the package.json
77-
// file to a new entry point
110+
// and merge its output with the files stream.
78111
const webpackConfigLocations = (<string[]>glob.sync(
79-
path.join(extensionPath, '/**/extension.webpack.config.js'),
112+
path.join(extensionPath, '**', webpackConfigFileName),
80113
{ ignore: ['**/node_modules'] }
81114
));
82115

83-
const packageJsonFilter = filter(f => {
84-
if (path.basename(f.path) === 'package.json') {
85-
// only modify package.json's next to the webpack file.
86-
// to be safe, use existsSync instead of path comparison.
87-
return fs.existsSync(path.join(path.dirname(f.path), 'extension.webpack.config.js'));
88-
}
89-
return false;
90-
}, { restore: true });
91-
92-
const patchFilesStream = filesStream
93-
.pipe(packageJsonFilter)
94-
.pipe(buffer())
95-
.pipe(json((data: any) => {
96-
if (data.main) {
97-
// hardcoded entry point directory!
98-
data.main = data.main.replace('/out/', /dist/);
99-
}
100-
return data;
101-
}))
102-
.pipe(packageJsonFilter.restore);
103-
104-
105116
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
106117

107118
const webpackDone = (err: any, stats: any) => {
@@ -143,7 +154,7 @@ function fromLocalWebpack(extensionPath: string): Stream {
143154
}));
144155
});
145156

146-
es.merge(...webpackStreams, patchFilesStream)
157+
es.merge(...webpackStreams, es.readArray(files))
147158
// .pipe(es.through(function (data) {
148159
// // debug
149160
// console.log('out', data.path, data.contents.length);
@@ -242,16 +253,35 @@ export function packageLocalExtensionsStream(): NodeJS.ReadWriteStream {
242253
.filter(({ name }) => excludedExtensions.indexOf(name) === -1)
243254
.filter(({ name }) => builtInExtensions.every(b => b.name !== name));
244255

245-
const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
256+
246257
const localExtensions = localExtensionDescriptions.map(extension => {
247-
return fromLocal(extension.path)
258+
return fromLocal(extension.path, false)
248259
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
249260
});
250261

262+
const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
251263
return es.merge(nodeModules, ...localExtensions)
252264
.pipe(util2.setExecutableBit(['**/*.sh']));
253265
}
254266

267+
export function packageLocalWebExtensionsStream(): NodeJS.ReadWriteStream {
268+
const localExtensionDescriptions = (<string[]>glob.sync('extensions/*/package.json'))
269+
.filter(manifestPath => {
270+
const packageJsonConfig = require(path.join(root, manifestPath));
271+
return !packageJsonConfig.main || packageJsonConfig.browser;
272+
})
273+
.map(manifestPath => {
274+
const extensionPath = path.dirname(path.join(root, manifestPath));
275+
const extensionName = path.basename(extensionPath);
276+
return { name: extensionName, path: extensionPath };
277+
});
278+
279+
return es.merge(...localExtensionDescriptions.map(extension => {
280+
return fromLocal(extension.path, true)
281+
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
282+
}));
283+
}
284+
255285
export function packageMarketplaceExtensionsStream(): NodeJS.ReadWriteStream {
256286
const extensions = builtInExtensions.map(extension => {
257287
return fromMarketplace(extension.name, extension.version, extension.metadata)

extensions/css-language-features/extension-browser.webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const clientConfig = withDefaults({
2525
output: {
2626
filename: 'cssClientMain.js',
2727
path: path.join(__dirname, 'client', 'dist', 'browser')
28+
},
29+
performance: {
30+
hints: false
2831
}
2932
});
3033
clientConfig.plugins[1] = vscodeNlsReplacement; // replace nls bundler

extensions/css-language-features/server/extension-browser.webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const serverConfig = withDefaults({
2626
filename: 'cssServerMain.js',
2727
path: path.join(__dirname, 'dist', 'browser'),
2828
libraryTarget: 'var'
29+
},
30+
performance: {
31+
hints: false
2932
}
3033
});
3134
serverConfig.plugins[1] = vscodeNlsReplacement; // replace nls bundler

0 commit comments

Comments
 (0)