Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Allow overriding basedir to fix web #41

Merged
merged 3 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ language servers written in TS and JS. It also contains helper methods to conver
[![NPM Version](https://img.shields.io/npm/v/vscode-nls-dev.svg)](https://npmjs.org/package/vscode-nls-dev)
[![NPM Downloads](https://img.shields.io/npm/dm/vscode-nls-dev.svg)](https://npmjs.org/package/vscode-nls-dev)

### 4.0.1

* [Allow overriding of baseDir in proccessing files.](https://github.com/microsoft/vscode-nls-dev/pull/41)

### 4.0.0-next.1

* [Add support for comments in messages (e.g. package.nls.json)](https://github.com/microsoft/vscode-nls-dev/issues/32)
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:

- job: Windows
pool:
vmImage: VS2017-Win2016
vmImage: 'windows-latest'
steps:
- template: build/azure-pipelines/win32/build.yml

- job: Indexing
pool:
vmImage: VS2017-Win2016
vmImage: 'windows-latest'
steps:
- template: build/azure-pipelines/index/build.yml
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-nls-dev",
"version": "4.0.0",
"version": "4.0.1",
"description": "Development time npm module to generate strings bundles from Javascript files",
"author": "Microsoft Corporation",
"license": "MIT",
Expand Down
10 changes: 5 additions & 5 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class TextModel {
}
}

function analyze(contents: string, relativeFilename: string | undefined, options: ts.CompilerOptions = {}): AnalysisResult {
function analyze(contents: string, relativeFilename?: string, baseDir?: string, options: ts.CompilerOptions = {}): AnalysisResult {

const vscodeRegExp = /^\s*(["'])vscode-nls\1\s*$/;

Expand Down Expand Up @@ -553,9 +553,10 @@ function analyze(contents: string, relativeFilename: string | undefined, options
loadCalls.reduce((memo, loadCall) => {
if (loadCall.arguments.length === 0) {
const args = loadCall.arguments;
const dir = baseDir ? JSON.stringify(baseDir) : '__dirname';
patches.push({
span: { start: ts.getLineAndCharacterOfPosition(sourceFile, args.pos), end: ts.getLineAndCharacterOfPosition(sourceFile, args.end) },
content: relativeFilename ? `require('path').join(__dirname, '${relativeFilename.replace(/\\/g, '\\\\')}')` : '__filename',
content: relativeFilename ? `require('path').join(${dir}, '${relativeFilename.replace(/\\/g, '\\\\')}')` : '__filename',
});
}
return memo;
Expand Down Expand Up @@ -640,9 +641,8 @@ function analyze(contents: string, relativeFilename: string | undefined, options
};
}

export function processFile(contents: string, relativeFileName: string | undefined, sourceMap?: string | RawSourceMap): { contents: string | undefined, sourceMap: string | undefined, bundle: JavaScriptMessageBundle | undefined, errors: string[] } {

const analysisResult = analyze(contents, relativeFileName);
export function processFile(contents: string, relativeFileName?: string, baseDir?: string, sourceMap?: string | RawSourceMap): { contents?: string, sourceMap?: string, bundle?: JavaScriptMessageBundle, errors: string[] } {
const analysisResult = analyze(contents, relativeFileName, baseDir);
if (analysisResult.patches.length === 0) {
return {
contents: undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function rewriteLocalizeCalls(): ThroughStream {
const content = buffer.toString('utf8');
const sourceMap = file.sourceMap;

const result = processFile(content, undefined, sourceMap);
const result = processFile(content, undefined, undefined, sourceMap);
let messagesFile: File | undefined;
let metaDataFile: File | undefined;
if (result.errors && result.errors.length > 0) {
Expand Down Expand Up @@ -89,7 +89,7 @@ export function createMetaDataFiles(): ThroughStream {
return;
}

let result = processFile(file.contents.toString('utf8'), undefined, undefined);
let result = processFile(file.contents.toString('utf8'));
if (result.errors && result.errors.length > 0) {
result.errors.forEach(error => console.error(`${file.relative}${error}`));
this.emit('error', `Failed to rewrite file: ${file.path}`);
Expand Down
18 changes: 16 additions & 2 deletions src/tests/analyze.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ suite('Localize', () => {
'localize(0, null, \'Hello\', \'World\');',
'//# sourceMappingURL=test.js.map'
];
let result = nlsDev.processFile(code.join('\r\n'), undefined, sourceMap);
let result = nlsDev.processFile(code.join('\r\n'), undefined, undefined, sourceMap);

assert.strictEqual(result.contents, expected.join('\r\n'));
assert.strictEqual(result.sourceMap, '{"version":3,"sources":["test.ts"],"names":[],"mappings":"AAAA,IAAY,GAAG,WAAM,YAAY,CAAC,CAAA;AAClC,IAAI,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,YAAE,CAAC;AAC9D,QAAQ,CAAC,aAAyB,EAAE,OAAO,CAAC,CAAC","sourceRoot":""}');
Expand Down Expand Up @@ -53,7 +53,7 @@ suite('Localize', () => {
'localize(0, null, \'Hello\', \'World\');',
'//# sourceMappingURL=test.js.map'
];
let result = nlsDev.processFile(code.join('\r\n'), undefined, sourceMap);
let result = nlsDev.processFile(code.join('\r\n'), undefined, undefined, sourceMap);
assert.strictEqual(result.contents, expected.join('\r\n'));
assert.strictEqual(result.sourceMap, '{"version":3,"sources":["test.ts"],"names":[],"mappings":"AAAA,IAAY,GAAG,WAAM,YAAY,CAAC,CAAA;AAClC,IAAI,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,YAAE,CAAC;AAC9D,QAAQ,CAAC,CAGR,EAAE,IAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC","sourceRoot":""}');
assert.deepStrictEqual(result.bundle, {
Expand Down Expand Up @@ -143,6 +143,20 @@ suite('Localize', () => {
assert.strictEqual(result.contents, expected.join('\n'));
});

test('allows overriding base directory', () => {
let code: string[] = [
'const nls = __importStar(require(\'vscode-nls\'))',
'var localize = nls.loadMessageBundle();',
'localize(\'keyOne\', \'{0} {1}\', \'Hello\', \'World\');'
];
let result = nlsDev.processFile(code.join('\n'), 'foo.js', '/');
let expected: string[] = [
'const nls = __importStar(require(\'vscode-nls\'))',
'var localize = nls.loadMessageBundle(require(\'path\').join("/", \'foo.js\'));',
'localize(0, null, \'Hello\', \'World\');'
];
assert.strictEqual(result.contents, expected.join('\n'));
});

test('https://github.com/Microsoft/vscode/issues/56792', () => {
let code: string[] = [
Expand Down
2 changes: 1 addition & 1 deletion src/vscl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ argv._.forEach(element => {
}

const relativeFilename = keepFilenames && rootDir ? path.relative(rootDir, resolvedFile) : undefined;
const result = processFile(contents, relativeFilename, sourceMapContent);
const result = processFile(contents, relativeFilename, undefined, sourceMapContent);

if (result.errors && result.errors.length > 0) {
result.errors.forEach(error => console.error(`${file}${error}`));
Expand Down
6 changes: 5 additions & 1 deletion src/webpack-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module.exports = function (this: any, content: any, map: any, meta: any) {

const callback = this.async();
const relativePath = relative(this.query.base, this.resourcePath);
const result = processFile(content, relativePath, map);
const result = processFile(
content,
relativePath,
this.target === 'webworker' || this.target === 'web' ? '/' : undefined,
map);

if (result.errors && result.errors.length > 0) {
// error
Expand Down