webpack json-language-features

json
This commit is contained in:
Martin Aeschlimann 2018-08-25 20:48:56 +02:00
parent 3f8088b5c5
commit df77466bfa
11 changed files with 214 additions and 88 deletions

View file

@ -44,11 +44,15 @@ function fromLocal(extensionPath, sourceMappingURLBase) {
contents: fs.createReadStream(filePath)
}); });
var filesStream = es.readArray(files);
// check for a webpack configuration file, then invoke webpack
// check for a webpack configuration files, then invoke webpack
// and merge its output with the files stream. also rewrite the package.json
// file to a new entry point
if (fs.existsSync(path.join(extensionPath, 'extension.webpack.config.js'))) {
var packageJsonFilter = filter('package.json', { restore: true });
var pattern = path.join(extensionPath, '/**/extension.webpack.config.js');
var webpackConfigLocations = glob.sync(pattern, { ignore: ['**/node_modules'] });
if (webpackConfigLocations.length) {
//console.log('-----' + webpackConfigLocations.join(','));
//console.log('-----' + fileNames.join(','));
var packageJsonFilter = filter('**/package.json', { restore: true });
var patchFilesStream = filesStream
.pipe(packageJsonFilter)
.pipe(buffer())
@ -58,38 +62,36 @@ function fromLocal(extensionPath, sourceMappingURLBase) {
return data;
}))
.pipe(packageJsonFilter.restore);
var webpackConfig = __assign({}, require(path.join(extensionPath, 'extension.webpack.config.js')), { mode: 'production', stats: 'errors-only' });
var webpackStream = webpackGulp(webpackConfig, webpack)
.pipe(es.through(function (data) {
data.stat = data.stat || {};
data.base = extensionPath;
this.emit('data', data);
}))
.pipe(es.through(function (data) {
// source map handling:
// * rewrite sourceMappingURL
// * save to disk so that upload-task picks this up
if (sourceMappingURLBase) {
var 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) + "/dist/" + g1;
}), 'utf8');
if (/\.js\.map$/.test(data.path)) {
if (!fs.existsSync(path.dirname(data.path))) {
fs.mkdirSync(path.dirname(data.path));
var webpackStreams = webpackConfigLocations.map(function (webpackConfigPath) {
var webpackConfig = __assign({}, require(webpackConfigPath), { mode: 'production', stats: 'errors-only' });
var relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
var webpackBaseDir = path.dirname(webpackConfigPath);
return webpackGulp(webpackConfig, webpack)
.pipe(es.through(function (data) {
data.stat = data.stat || {};
data.base = extensionPath;
this.emit('data', data);
}))
.pipe(es.through(function (data) {
// source map handling:
// * rewrite sourceMappingURL
// * save to disk so that upload-task picks this up
if (sourceMappingURLBase) {
var 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');
if (/\.js\.map$/.test(data.path)) {
if (!fs.existsSync(path.dirname(data.path))) {
fs.mkdirSync(path.dirname(data.path));
}
fs.writeFileSync(data.path, data.contents);
}
fs.writeFileSync(data.path, data.contents);
}
}
this.emit('data', data);
}));
es.merge(webpackStream, patchFilesStream)
// .pipe(es.through(function (data) {
// // debug
// console.log('out', data.path, data.contents.length);
// this.emit('data', data);
// }))
.pipe(result);
this.emit('data', data);
}));
});
es.merge.apply(es, webpackStreams.concat([patchFilesStream])).pipe(result);
}
else {
filesStream.pipe(result);

View file

@ -41,11 +41,16 @@ export function fromLocal(extensionPath: string, sourceMappingURLBase?: string):
const filesStream = es.readArray(files);
// check for a webpack configuration file, then invoke webpack
// check for a webpack configuration files, then invoke webpack
// and merge its output with the files stream. also rewrite the package.json
// file to a new entry point
if (fs.existsSync(path.join(extensionPath, 'extension.webpack.config.js'))) {
const packageJsonFilter = filter('package.json', { restore: true });
const pattern = path.join(extensionPath, '/**/extension.webpack.config.js');
const webpackConfigLocations = (<string[]>glob.sync(pattern, { ignore: ['**/node_modules'] }));
if (webpackConfigLocations.length) {
//console.log('-----' + webpackConfigLocations.join(','));
//console.log('-----' + fileNames.join(','));
const packageJsonFilter = filter('**/package.json', { restore: true });
const patchFilesStream = filesStream
.pipe(packageJsonFilter)
@ -57,38 +62,42 @@ export function fromLocal(extensionPath: string, sourceMappingURLBase?: string):
}))
.pipe(packageJsonFilter.restore);
const webpackConfig = {
...require(path.join(extensionPath, 'extension.webpack.config.js')),
...{ mode: 'production', stats: 'errors-only' }
};
const webpackStream = webpackGulp(webpackConfig, webpack)
.pipe(es.through(function (data) {
data.stat = data.stat || {};
data.base = extensionPath;
this.emit('data', data);
}))
.pipe(es.through(function (data: File) {
// source map handling:
// * rewrite sourceMappingURL
// * save to disk so that upload-task picks this up
if (sourceMappingURLBase) {
const contents = (<Buffer>data.contents).toString('utf8');
data.contents = Buffer.from(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, function (_m, g1) {
return `\n//# sourceMappingURL=${sourceMappingURLBase}/extensions/${path.basename(extensionPath)}/dist/${g1}`;
}), 'utf8');
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
const webpackConfig = {
...require(webpackConfigPath),
...{ mode: 'production', stats: 'errors-only' }
};
let relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
let webpackBaseDir = path.dirname(webpackConfigPath);
if (/\.js\.map$/.test(data.path)) {
if (!fs.existsSync(path.dirname(data.path))) {
fs.mkdirSync(path.dirname(data.path));
return webpackGulp(webpackConfig, webpack)
.pipe(es.through(function (data) {
data.stat = data.stat || {};
data.base = extensionPath;
this.emit('data', data);
}))
.pipe(es.through(function (data: File) {
// source map handling:
// * rewrite sourceMappingURL
// * save to disk so that upload-task picks this up
if (sourceMappingURLBase) {
const contents = (<Buffer>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');
if (/\.js\.map$/.test(data.path)) {
if (!fs.existsSync(path.dirname(data.path))) {
fs.mkdirSync(path.dirname(data.path));
}
fs.writeFileSync(data.path, data.contents);
}
fs.writeFileSync(data.path, data.contents);
}
}
this.emit('data', data);
}))
;
this.emit('data', data);
}));
});
es.merge(webpackStream, patchFilesStream)
es.merge(...webpackStreams, patchFilesStream)
// .pipe(es.through(function (data) {
// // debug
// console.log('out', data.path, data.contents.length);

View file

@ -1,11 +1,26 @@
test/**
.vscode/**
client/tsconfig.json
server/.vscode/**
node_modules/**
server/node_modules/**
client/src/**
server/bin
server/tsconfig.json
server/src/**
client/out/**
server/out/**
client/tsconfig.json
server/tsconfig.json
server/test/**
server/node_modules/@types/**
**/node_modules/*/lib/esm/**
**/node_modules/@types/**
server/bin/**
server/build/**
server/yarn.lock
server/.npmignore
yarn.lock
server/extension.webpack.config.js
extension.webpack.config.js
!node_modules/vscode-nls/**
!node_modules/applicationinsights/**
!node_modules/diagnostic-channel-publishers/**
!node_modules/diagnostic-channel/**
!node_modules/semver/**
!node_modules/vscode-extension-telemetry/**
!node_modules/zone.js/**
!server/node_modules/vscode-nls/**

View file

@ -5,6 +5,7 @@
'use strict';
import * as path from 'path';
import * as fs from 'fs';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
@ -62,8 +63,9 @@ export function activate(context: ExtensionContext) {
let packageInfo = getPackageInfo(context);
telemetryReporter = packageInfo && new TelemetryReporter(packageInfo.name, packageInfo.version, packageInfo.aiKey);
// The server is implemented in node
let serverModule = context.asAbsolutePath(path.join('server', 'out', 'jsonServerMain.js'));
let serverMain = readJSONFile(context.asAbsolutePath('./server/package.json')).main;
let serverModule = context.asAbsolutePath(path.join('server', serverMain));
// The debug options for the server
let debugOptions = { execArgv: ['--nolazy', '--inspect=' + (9000 + Math.round(Math.random() * 10000))] };
@ -259,7 +261,7 @@ function getSchemaId(schema: JSONSchemaSettings, rootPath?: string) {
}
function getPackageInfo(context: ExtensionContext): IPackageInfo | undefined {
let extensionPackage = require(context.asAbsolutePath('./package.json'));
let extensionPackage = readJSONFile(context.asAbsolutePath('./package.json'));
if (extensionPackage) {
return {
name: extensionPackage.name,
@ -269,3 +271,13 @@ function getPackageInfo(context: ExtensionContext): IPackageInfo | undefined {
}
return void 0;
}
function readJSONFile(location: string) {
try {
return JSON.parse(fs.readFileSync(location).toString());
} catch (e) {
console.log(`Problems reading ${location}: ${e}`);
return {};
}
}

View file

@ -0,0 +1,40 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
'use strict';
const withDefaults = require('../shared.webpack.config');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
module.exports = withDefaults({
context: path.join(__dirname, 'client'),
entry: {
extension: './src/jsonMain.ts',
},
resolve: {
mainFields: ['module', 'main'],
extensions: ['.ts', '.js'] // support ts-files and js-files
},
node: {
__dirname: false // leave the __dirname-behaviour intact
},
output: {
filename: 'jsonMain.js',
path: path.join(__dirname, 'client', 'dist'),
libraryTarget: "commonjs",
},
externals: {
'./files': 'commonjs', // ignored because it doesn't exist
},
plugins: [
new CopyWebpackPlugin([
{ from: './out/*.sh', to: '[name].sh' },
{ from: './out/nls.*.json', to: '[name].json' }
])
]
});

View file

@ -101,7 +101,7 @@
},
"dependencies": {
"vscode-extension-telemetry": "0.0.18",
"vscode-languageclient": "^4.4.2",
"vscode-languageclient": "^5.1.0-next.4",
"vscode-nls": "^3.2.4"
},
"devDependencies": {

View file

@ -0,0 +1,5 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
module.exports = {};

View file

@ -0,0 +1,38 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
'use strict';
const withDefaults = require('../../shared.webpack.config');
const path = require('path');
var webpack = require('webpack');
module.exports = withDefaults({
context: path.join(__dirname),
entry: {
extension: './src/jsonServerMain.ts',
},
resolve: {
mainFields: ['module', 'main'],
extensions: ['.ts', '.js'] // support ts-files and js-files
},
node: {
__dirname: false // leave the __dirname-behaviour intact
},
output: {
filename: 'jsonServerMain.js',
path: path.join(__dirname, 'dist'),
libraryTarget: "commonjs",
},
plugins: [
new webpack.NormalModuleReplacementPlugin(
/\/vscode-languageserver\/lib\/files\.js/,
require.resolve('./build/files')
),
new webpack.IgnorePlugin(/vertx/)
],
});

View file

@ -10,11 +10,12 @@
"bin": {
"vscode-json-languageserver": "./bin/vscode-json-languageserver"
},
"main": "./dist/jsonServerMain",
"dependencies": {
"jsonc-parser": "^2.0.1",
"request-light": "^0.2.3",
"vscode-json-languageservice": "^3.1.5",
"vscode-languageserver": "^4.4.2",
"vscode-languageserver": "^5.1.0-next.3",
"vscode-nls": "^3.2.4",
"vscode-uri": "^1.0.5"
},
@ -34,4 +35,4 @@
"install-server-local": "yarn link vscode-languageserver-server",
"version": "git commit -m \"JSON Language Server $npm_package_version\" package.json"
}
}
}

View file

@ -75,22 +75,26 @@ vscode-jsonrpc@^3.6.2:
version "3.6.2"
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-3.6.2.tgz#3b5eef691159a15556ecc500e9a8a0dd143470c8"
vscode-languageserver-protocol@^3.10.3:
version "3.10.3"
resolved "https://registry.yarnpkg.com/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.10.3.tgz#59841c9602a6a6baab68613c2a47760994657196"
vscode-languageserver-protocol@^3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.12.0.tgz#5b23501292abad88f0463b01e83ff98e64a37652"
dependencies:
vscode-jsonrpc "^3.6.2"
vscode-languageserver-types "^3.10.1"
vscode-languageserver-types "^3.12.0"
vscode-languageserver-types@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.10.1.tgz#d5d5f44f688a3b2aa9857dc53cb9cacca73fe35a"
vscode-languageserver@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/vscode-languageserver/-/vscode-languageserver-4.4.2.tgz#600ae9cc7a6ff1e84d93c7807840c2cb5b22821b"
vscode-languageserver-types@^3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.12.0.tgz#f96051381b6a050b7175b37d6cb5d2f2eb64b944"
vscode-languageserver@^5.1.0-next.3:
version "5.1.0-next.3"
resolved "https://registry.yarnpkg.com/vscode-languageserver/-/vscode-languageserver-5.1.0-next.3.tgz#352ce1f0f8bdfc47c1236277ebe865e25b087ccd"
dependencies:
vscode-languageserver-protocol "^3.10.3"
vscode-languageserver-protocol "^3.12.0"
vscode-uri "^1.0.5"
vscode-nls@^3.2.2, vscode-nls@^3.2.4:

View file

@ -38,9 +38,9 @@ vscode-jsonrpc@^3.6.2:
version "3.6.2"
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-3.6.2.tgz#3b5eef691159a15556ecc500e9a8a0dd143470c8"
vscode-languageclient@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/vscode-languageclient/-/vscode-languageclient-4.4.2.tgz#a341a7b4ac403e481f011ed4572854646e8968c4"
vscode-languageclient@^5.1.0-next.4:
version "5.1.0-next.4"
resolved "https://registry.yarnpkg.com/vscode-languageclient/-/vscode-languageclient-5.1.0-next.4.tgz#2f96b4aa198c45f3e897b7f330c597a401ca95f2"
dependencies:
vscode-languageserver-protocol "^3.10.3"