Ship ESM variant; Ship AMD variant using webpack

This commit is contained in:
Alex Dima 2018-03-08 16:44:27 +01:00
parent 6a61202ff4
commit e2b014d8d7
10 changed files with 6531 additions and 1592 deletions

View file

@ -1,8 +1,10 @@
/.vscode/ /.vscode/
/lib/ /lib/
/out/ /out/
/scripts/
/src/ /src/
/test/ /test/
/release/dev/ /release/dev/
/gulpfile.js
/.npmignore /.npmignore
/webpack.dev.config.js
/webpack.min.config.js

View file

@ -1,160 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var gulp = require('gulp');
var tsb = require('gulp-tsb');
var assign = require('object-assign');
var fs = require('fs');
var path = require('path');
var merge = require('merge-stream');
var rjs = require('gulp-requirejs');
var uglify = require('gulp-uglify');
var rimraf = require('rimraf');
var es = require('event-stream');
gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
gulp.task('release', ['clean-release'], function() {
var sha1 = getGitVersion(__dirname);
var semver = require('./package.json').version;
var headerVersion = semver + '(' + sha1 + ')';
var BUNDLED_FILE_HEADER = [
'/*!-----------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * monaco-json version: ' + headerVersion,
' * Released under the MIT license',
' * https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md',
' *-----------------------------------------------------------------------------*/',
''
].join('\n');
function getDependencyLocation(name, libLocation, container) {
var location = __dirname + '/node_modules/' + name + '/' + libLocation;
if (!fs.existsSync(location)) {
var oldLocation = __dirname + '/node_modules/' + container + '/node_modules/' + name + '/' + libLocation;
if (!fs.existsSync(oldLocation)) {
console.error('Unable to find ' + name + ' node module at ' + location + ' or ' + oldLocation);
return;
}
return oldLocation;
}
return location;
}
var jsoncLocation = getDependencyLocation('jsonc-parser', 'lib/umd', 'vscode-json-languageservice');
var uriLocation = getDependencyLocation('vscode-uri', 'lib/umd', 'vscode-json-languageservice');
function bundleOne(moduleId, exclude) {
return rjs({
baseUrl: '/out/',
name: 'vs/language/json/' + moduleId,
out: moduleId + '.js',
exclude: exclude,
paths: {
'vs/language/json': __dirname + '/out'
},
packages: [{
name: 'vscode-json-languageservice',
location: __dirname + '/node_modules/vscode-json-languageservice/lib/umd',
main: 'jsonLanguageService'
}, {
name: 'vscode-languageserver-types',
location: __dirname + '/node_modules/vscode-languageserver-types/lib/umd',
main: 'main'
}, {
name: 'vscode-uri',
location: uriLocation,
main: 'index'
}, {
name: 'jsonc-parser',
location: jsoncLocation,
main: 'main'
}, {
name: 'vscode-uri',
location: uriLocation,
main: 'index'
}, {
name: 'vscode-nls',
location: __dirname + '/out/fillers',
main: 'vscode-nls'
}]
})
}
return merge(
merge(
bundleOne('monaco.contribution', ['vs/language/json/jsonMode']),
bundleOne('jsonMode'),
bundleOne('jsonWorker')
)
.pipe(es.through(function(data) {
data.contents = new Buffer(
BUNDLED_FILE_HEADER
+ data.contents.toString()
);
this.emit('data', data);
}))
.pipe(gulp.dest('./release/dev'))
.pipe(uglify({
preserveComments: 'some'
}))
.pipe(gulp.dest('./release/min')),
gulp.src('src/monaco.d.ts').pipe(gulp.dest('./release/min'))
);
});
function getGitVersion(repo) {
var git = path.join(repo, '.git');
var headPath = path.join(git, 'HEAD');
var head;
try {
head = fs.readFileSync(headPath, 'utf8').trim();
} catch (e) {
return void 0;
}
if (/^[0-9a-f]{40}$/i.test(head)) {
return head;
}
var refMatch = /^ref: (.*)$/.exec(head);
if (!refMatch) {
return void 0;
}
var ref = refMatch[1];
var refPath = path.join(git, ref);
try {
return fs.readFileSync(refPath, 'utf8').trim();
} catch (e) {
// noop
}
var packedRefsPath = path.join(git, 'packed-refs');
var refsRaw;
try {
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
} catch (e) {
return void 0;
}
var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
var refsMatch;
var refs = {};
while (refsMatch = refsRegex.exec(refsRaw)) {
refs[refsMatch[2]] = refsMatch[1];
}
return refs[ref];
}

7678
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"compile": "node ./scripts/rmdir.js ./out && tsc -p ./src && tsc -p ./src/tsconfig.esm.json", "compile": "node ./scripts/rmdir.js ./out && tsc -p ./src && tsc -p ./src/tsconfig.esm.json",
"watch": "tsc -p ./src --watch", "watch": "tsc -p ./src --watch",
"prepublish": "npm run compile && gulp release", "prepublish": "node ./scripts/rmdir.js ./release && npm run compile && node ./scripts/release.js && webpack --config webpack.dev.config.js && webpack --config webpack.min.config.js && node ./scripts/copy.js ./src/monaco.d.ts ./release/min/monaco.d.ts",
"install-service-next": "npm install vscode-json-languageservice@next -f -D && npm install vscode-languageserver-types@next -f -D", "install-service-next": "npm install vscode-json-languageservice@next -f -D && npm install vscode-languageserver-types@next -f -D",
"install-service-local": "npm install ../vscode-json-languageservice -f -D && npm install ../vscode-languageserver-node/types -f -D" "install-service-local": "npm install ../vscode-json-languageservice -f -D && npm install ../vscode-languageserver-node/types -f -D"
}, },
@ -19,19 +19,13 @@
"url": "https://github.com/Microsoft/monaco-json/issues" "url": "https://github.com/Microsoft/monaco-json/issues"
}, },
"devDependencies": { "devDependencies": {
"event-stream": "^3.3.2",
"gulp": "^3.9.1",
"gulp-requirejs": "^0.1.3",
"gulp-tsb": "^2.0.0",
"gulp-uglify": "^1.5.3",
"jsonc-parser": "2.0.0-next.1", "jsonc-parser": "2.0.0-next.1",
"merge-stream": "^1.0.0",
"monaco-editor-core": "^0.11.1", "monaco-editor-core": "^0.11.1",
"monaco-languages": "^0.9.0", "monaco-languages": "^0.9.0",
"object-assign": "^4.1.0",
"rimraf": "^2.5.2",
"typescript": "^2.7.1", "typescript": "^2.7.1",
"vscode-json-languageservice": "^3.0.12", "vscode-json-languageservice": "^3.0.12",
"vscode-languageserver-types": "^3.6.1" "vscode-languageserver-types": "^3.6.1",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.10"
} }
} }

25
scripts/copy.js Normal file
View file

@ -0,0 +1,25 @@
const fs = require('fs');
const path = require('path');
const source = path.join(process.cwd(), process.argv[2]);
const destination = path.join(process.cwd(), process.argv[3]);
// ensure target dir
(function () {
let dirs = [];
let dirname = path.dirname(destination);
while (dirname !== process.cwd()) {
dirs.push(dirname);
dirname = path.dirname(dirname);
}
dirs.reverse();
dirs.forEach((dir) => {
try { fs.mkdirSync(dir); } catch (err) { }
})
})();
fs.writeFileSync(destination, fs.readFileSync(source));
console.log(`Copied ${process.argv[2]} to ${process.argv[3]}`);

173
scripts/release.js Normal file
View file

@ -0,0 +1,173 @@
"use strict";
exports.__esModule = true;
var fs = require("fs");
var ts = require("typescript");
var path = require("path");
var REPO_ROOT = path.join(__dirname, '../');
process({
repoRoot: REPO_ROOT,
esmSource: 'out-esm',
esmDestination: 'release/esm',
entryPoints: [
'monaco.contribution.js',
'jsonMode.js',
'json.worker.js'
],
resolveAlias: {
'vscode-nls': path.join(REPO_ROOT, "out-esm/fillers/vscode-nls.js")
},
resolveSkip: [
'monaco-editor-core'
],
destinationFolderSimplification: {
'node_modules': '_deps',
'jsonc-parser/lib/esm': 'jsonc-parser',
'vscode-languageserver-types/lib/esm': 'vscode-languageserver-types',
'vscode-uri/lib/esm': 'vscode-uri',
'vscode-json-languageservice/lib/esm': 'vscode-json-languageservice'
}
});
function process(options) {
options.repoRoot = path.normalize(options.repoRoot).replace(/(\/|\\)$/, '');
var ESM_SRC = path.join(options.repoRoot, options.esmSource);
var ESM_DEST = path.join(options.repoRoot, options.esmDestination);
var in_queue = Object.create(null);
var queue = [];
var enqueue = function (filePath) {
if (in_queue[filePath]) {
return;
}
in_queue[filePath] = true;
queue.push(filePath);
};
var seenDir = {};
var createDirectoryRecursive = function (dir) {
if (seenDir[dir]) {
return;
}
var lastSlash = dir.lastIndexOf('/');
if (lastSlash === -1) {
lastSlash = dir.lastIndexOf('\\');
}
if (lastSlash !== -1) {
createDirectoryRecursive(dir.substring(0, lastSlash));
}
seenDir[dir] = true;
try {
fs.mkdirSync(dir);
}
catch (err) { }
};
seenDir[options.repoRoot] = true;
var applyDestinationFolderSimplifications = function (filePath) {
filePath = filePath.replace(/\\/g, '/');
for (var key in options.destinationFolderSimplification) {
var test = key.replace(/\\/g, '/');
while (filePath.indexOf(test) >= 0) {
filePath = filePath.replace(test, options.destinationFolderSimplification[test]);
}
}
return filePath;
};
var shouldSkipImport = function (importText) {
for (var i = 0; i < options.resolveSkip.length; i++) {
var skip = options.resolveSkip[i];
if (importText.indexOf(skip) === 0) {
return true;
}
}
return false;
};
var computeDestinationFilePath = function (filePath) {
if (filePath.indexOf(ESM_SRC) === 0) {
// This file is from our sources
return path.join(ESM_DEST, path.relative(ESM_SRC, filePath));
}
else {
// This file is from node_modules
return path.normalize(applyDestinationFolderSimplifications(path.join(ESM_DEST, path.relative(options.repoRoot, filePath))));
}
};
var write = function (filePath, fileContents) {
var finalFilePath = computeDestinationFilePath(filePath);
createDirectoryRecursive(path.dirname(finalFilePath));
fs.writeFileSync(finalFilePath, fileContents);
};
options.entryPoints.forEach(function (filePath) {
enqueue(path.join(ESM_SRC, filePath));
});
while (queue.length > 0) {
var filePath = queue.shift();
var fileContents = fs.readFileSync(filePath).toString();
var info = ts.preProcessFile(fileContents);
for (var i = info.importedFiles.length - 1; i >= 0; i--) {
var importText = info.importedFiles[i].fileName;
if (shouldSkipImport(importText)) {
continue;
}
var pos = info.importedFiles[i].pos;
var end = info.importedFiles[i].end;
if (/(^\.\/)|(^\.\.\/)/.test(importText)) {
// Relative import
var importedFilename = path.join(path.dirname(filePath), importText) + '.js';
enqueue(importedFilename);
}
else {
var importedFilename = void 0;
if (options.resolveAlias[importText]) {
importedFilename = options.resolveAlias[importText];
}
else {
importedFilename = findNodeModuleImport(options.repoRoot, importText, filePath);
}
var myDestinationPath = computeDestinationFilePath(filePath);
var importDestinationPath = computeDestinationFilePath(importedFilename);
var relativePath = path.relative(path.dirname(myDestinationPath), importDestinationPath);
if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
relativePath = './' + relativePath;
}
relativePath = relativePath.replace(/\\/g, '/');
relativePath = relativePath.replace(/\.js$/, '');
fileContents = (fileContents.substring(0, pos + 1)
+ relativePath
+ fileContents.substring(end + 1));
enqueue(importedFilename);
}
}
write(filePath, fileContents);
}
}
function findNodeModuleImport(repoRoot, module, sourceFilePath) {
var modulePath = findNodeModule(repoRoot, module, sourceFilePath);
var modulePackagePath = path.join(modulePath, 'package.json');
if (!fs.existsSync(modulePackagePath)) {
throw new Error("Missing " + modulePackagePath + " in node module " + modulePath);
}
var modulePackage = JSON.parse(fs.readFileSync(modulePackagePath).toString());
if (typeof modulePackage.module !== 'string') {
throw new Error("Missing property 'module' package.json at " + modulePackagePath);
}
var result = path.join(modulePath, modulePackage.module);
if (!fs.existsSync(result)) {
throw new Error("Missing file " + result);
}
return result;
function findNodeModule(repoRoot, module, sourceFilePath) {
var modulePaths = generatePaths(repoRoot, module, sourceFilePath);
for (var i = 0; i < modulePaths.length; i++) {
if (fs.existsSync(modulePaths[i])) {
return modulePaths[i];
}
}
throw new Error("Cannot find module " + module + " requested by " + sourceFilePath);
}
function generatePaths(repoRoot, module, sourceFilePath) {
var sourceDir = path.dirname(sourceFilePath);
var result = [];
while (sourceDir.length >= repoRoot.length) {
result.push(path.join(sourceDir, 'node_modules', module));
sourceDir = path.dirname(sourceDir);
}
return result;
}
}

29
scripts/webpack.js Normal file
View file

@ -0,0 +1,29 @@
const path = require('path');
const webpack = require('webpack');
const REPO_ROOT = path.resolve(__dirname, '..');
exports.createWebpackConfig = function (isDev) {
let targetFolder = isDev ? './release/dev' : './release/min';
let mode = isDev ? 'development' : 'production';
return {
entry: {
"monaco.contribution": './release/esm/monaco.contribution',
"jsonMode": './release/esm/jsonMode',
"jsonWorker": './release/esm/jsonWorker'
},
output: {
filename: '[name].js',
path: path.resolve(REPO_ROOT, targetFolder),
libraryTarget: "amd"
},
mode: mode,
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
})
],
};
}

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
'use strict'; 'use strict';
import * as worker from '../node_modules/monaco-editor-core/esm/vs/editor/editor.worker'; import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker';
import { JSONWorker } from './jsonWorker'; import { JSONWorker } from './jsonWorker';
self.onmessage = () => { self.onmessage = () => {

1
webpack.dev.config.js Normal file
View file

@ -0,0 +1 @@
module.exports = require('./scripts/webpack').createWebpackConfig(true);

1
webpack.min.config.js Normal file
View file

@ -0,0 +1 @@
module.exports = require('./scripts/webpack').createWebpackConfig(false);