Skip to content

Commit 6966fa9

Browse files
author
Benjamin E. Coe
authored
fix(webpack): skip readFileSync if not defined (#117)
1 parent c755582 commit 6966fa9

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

lib/index.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Y18N {
120120

121121
// if a %d placeholder is provided, add quantity
122122
// to the arguments expanded by util.format.
123-
var values: (string|number)[] = [str]
123+
const values: (string|number)[] = [str]
124124
if (~str.indexOf('%d')) values.push(quantity)
125125

126126
return shim.format.apply(shim.format, values.concat(args))
@@ -147,7 +147,7 @@ class Y18N {
147147
_taggedLiteral (parts: string[], ...args: string[]) {
148148
let str = ''
149149
parts.forEach(function (part, i) {
150-
var arg = args[i + 1]
150+
const arg = args[i + 1]
151151
str += part
152152
if (typeof arg !== 'undefined') {
153153
str += '%s'
@@ -162,16 +162,16 @@ class Y18N {
162162
}
163163

164164
_processWriteQueue () {
165-
var _this = this
166-
var work = this.writeQueue[0]
165+
const _this = this
166+
const work = this.writeQueue[0]
167167

168168
// destructure the enqueued work.
169-
var directory = work.directory
170-
var locale = work.locale
171-
var cb = work.cb
169+
const directory = work.directory
170+
const locale = work.locale
171+
const cb = work.cb
172172

173-
var languageFile = this._resolveLocaleFile(directory, locale)
174-
var serializedLocale = JSON.stringify(this.cache[locale], null, 2)
173+
const languageFile = this._resolveLocaleFile(directory, locale)
174+
const serializedLocale = JSON.stringify(this.cache[locale], null, 2)
175175

176176
shim.fs.writeFile(languageFile, serializedLocale, 'utf-8', function (err: Error) {
177177
_this.writeQueue.shift()
@@ -181,11 +181,14 @@ class Y18N {
181181
}
182182

183183
_readLocaleFile () {
184-
var localeLookup = {}
185-
var languageFile = this._resolveLocaleFile(this.directory, this.locale)
184+
let localeLookup = {}
185+
const languageFile = this._resolveLocaleFile(this.directory, this.locale)
186186

187187
try {
188-
localeLookup = JSON.parse(shim.fs.readFileSync(languageFile, 'utf-8'))
188+
// When using a bundler such as webpack, readFileSync may not be defined:
189+
if (shim.fs.readFileSync) {
190+
localeLookup = JSON.parse(shim.fs.readFileSync(languageFile, 'utf-8'))
191+
}
189192
} catch (err) {
190193
if (err instanceof SyntaxError) {
191194
err.message = 'syntax error in ' + languageFile
@@ -199,10 +202,10 @@ class Y18N {
199202
}
200203

201204
_resolveLocaleFile (directory: string, locale: string) {
202-
var file = shim.resolve(directory, './', locale + '.json')
205+
let file = shim.resolve(directory, './', locale + '.json')
203206
if (this.fallbackToLanguage && !this._fileExistsSync(file) && ~locale.lastIndexOf('_')) {
204207
// attempt fallback to language only
205-
var languageFile = shim.resolve(directory, './', locale.split('_')[0] + '.json')
208+
const languageFile = shim.resolve(directory, './', locale.split('_')[0] + '.json')
206209
if (this._fileExistsSync(languageFile)) file = languageFile
207210
}
208211
return file

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"author": "Ben Coe <[email protected]>",
2828
"main": "./build/index.cjs",
2929
"scripts": {
30-
"check": "standardx '**/*.ts' '**/*.cjs' '**/*.mjs'",
31-
"fix": "standardx --fix '**/*.ts' '**/*.cjs' '**/*.mjs'",
30+
"check": "standardx **/*.ts **/*.cjs **/*.mjs",
31+
"fix": "standardx --fix **/*.ts **/*.cjs **/*.mjs",
3232
"pretest": "rimraf build && tsc -p tsconfig.test.json && cross-env NODE_ENV=test npm run build:cjs",
3333
"test": "c8 --reporter=text --reporter=html mocha test/*.cjs",
3434
"test:esm": "c8 --reporter=text --reporter=html mocha test/esm/*.mjs",
@@ -50,7 +50,7 @@
5050
"mocha": "^8.0.0",
5151
"rimraf": "^3.0.2",
5252
"rollup": "^2.26.10",
53-
"standardx": "^6.0.0",
53+
"standardx": "^7.0.0",
5454
"ts-transform-default-export": "^1.0.2",
5555
"typescript": "^4.0.0"
5656
},

test/y18n-test.cjs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('y18n', function () {
1919
describe('_readLocaleFile', function () {
2020
it('throws a helpful error if language file has invalid syntax', function () {
2121
expect(function () {
22-
var __ = y18n({
22+
const __ = y18n({
2323
locale: 'bad-locale',
2424
directory: path.join(__dirname, 'locales')
2525
}).__
@@ -31,30 +31,30 @@ describe('y18n', function () {
3131

3232
describe('__', function () {
3333
it('can be used as a tag for template literals', function () {
34-
var __ = y18n({
34+
const __ = y18n({
3535
locale: 'pirate',
3636
directory: path.join(__dirname, 'locales')
3737
}).__
3838

3939
__`Hi, ${'Ben'} ${'Coe'}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben Coe!')
4040
})
4141
it('can be used as a tag for template literals with falsy arguments', function () {
42-
var __ = y18n({
42+
const __ = y18n({
4343
locale: 'pirate',
4444
directory: path.join(__dirname, 'locales')
4545
}).__
4646
__`Hi, ${'Ben'} ${''}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben !')
4747
})
4848
it('uses replacements from the default locale if none is configured', function () {
49-
var __ = y18n({
49+
const __ = y18n({
5050
directory: path.join(__dirname, 'locales')
5151
}).__
5252

5353
__('Hello').should.equal('Hello!')
5454
})
5555

5656
it('uses replacements from the configured locale', function () {
57-
var __ = y18n({
57+
const __ = y18n({
5858
locale: 'pirate',
5959
directory: path.join(__dirname, 'locales')
6060
}).__
@@ -63,7 +63,7 @@ describe('y18n', function () {
6363
})
6464

6565
it('uses language file if language_territory file does not exist', function () {
66-
var __ = y18n({
66+
const __ = y18n({
6767
locale: 'pirate_JM',
6868
directory: path.join(__dirname, 'locales')
6969
}).__
@@ -72,7 +72,7 @@ describe('y18n', function () {
7272
})
7373

7474
it('does not fallback to language file if fallbackToLanguage is false', function () {
75-
var __ = y18n({
75+
const __ = y18n({
7676
locale: 'pirate_JM',
7777
fallbackToLanguage: false,
7878
updateFiles: false,
@@ -83,7 +83,7 @@ describe('y18n', function () {
8383
})
8484

8585
it('uses strings as given if no matching locale files found', function () {
86-
var __ = y18n({
86+
const __ = y18n({
8787
locale: 'zz_ZZ',
8888
updateFiles: false,
8989
directory: path.join(__dirname, 'locales')
@@ -93,7 +93,7 @@ describe('y18n', function () {
9393
})
9494

9595
it('expands arguments into %s placeholders', function () {
96-
var __ = y18n({
96+
const __ = y18n({
9797
directory: path.join(__dirname, 'locales')
9898
}).__
9999

@@ -108,7 +108,7 @@ describe('y18n', function () {
108108
})
109109

110110
it('returns the word immediately', function () {
111-
var __ = y18n({
111+
const __ = y18n({
112112
locale: 'fr',
113113
directory: path.join(__dirname, 'locales')
114114
}).__
@@ -117,13 +117,13 @@ describe('y18n', function () {
117117
})
118118

119119
it('writes new word to locale file if updateFiles is true', function (done) {
120-
var __ = y18n({
120+
const __ = y18n({
121121
locale: 'fr_FR',
122122
directory: path.join(__dirname, 'locales')
123123
}).__
124124

125125
__('banana', function (err) {
126-
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
126+
const locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
127127
locale.banana.should.equal('banana')
128128
return done(err)
129129
})
@@ -132,14 +132,14 @@ describe('y18n', function () {
132132
it('writes new word to language file if language_territory file does not exist', function (done) {
133133
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
134134

135-
var __ = y18n({
135+
const __ = y18n({
136136
locale: 'fr_FR',
137137
directory: path.join(__dirname, 'locales')
138138
}).__
139139

140140
__('meow').should.equal('le meow')
141141
__('banana', function (err) {
142-
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
142+
const locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
143143
locale.banana.should.equal('banana')
144144
return done(err)
145145
})
@@ -148,20 +148,20 @@ describe('y18n', function () {
148148
it('writes word to missing locale file, if no fallback takes place', function (done) {
149149
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
150150

151-
var __ = y18n({
151+
const __ = y18n({
152152
locale: 'fr_FR',
153153
fallbackToLanguage: false,
154154
directory: path.join(__dirname, 'locales')
155155
}).__
156156

157157
__('banana', function (err) {
158158
// 'banana' should be written to fr_FR.json
159-
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
159+
const locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
160160
locale.should.deep.equal({
161161
banana: 'banana'
162162
})
163163
// fr.json should remain untouched
164-
var frJson = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
164+
const frJson = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
165165
frJson.should.deep.equal({
166166
meow: 'le meow'
167167
})
@@ -170,7 +170,7 @@ describe('y18n', function () {
170170
})
171171

172172
it('handles enqueuing multiple writes at the same time', function (done) {
173-
var __ = y18n({
173+
const __ = y18n({
174174
locale: 'fr',
175175
directory: path.join(__dirname, 'locales')
176176
}).__
@@ -179,7 +179,7 @@ describe('y18n', function () {
179179
__('banana', function () {
180180
__('foo')
181181
__('bar', function (err) {
182-
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
182+
const locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
183183
locale.apple.should.equal('apple')
184184
locale.banana.should.equal('banana')
185185
locale.foo.should.equal('foo')
@@ -190,7 +190,7 @@ describe('y18n', function () {
190190
})
191191

192192
it('does not write the locale file if updateFiles is false', function (done) {
193-
var __ = y18n({
193+
const __ = y18n({
194194
locale: 'fr',
195195
updateFiles: false,
196196
directory: path.join(__dirname, 'locales')
@@ -206,31 +206,31 @@ describe('y18n', function () {
206206

207207
describe('__n', function () {
208208
it('uses the singular form if quantity is 1', function () {
209-
var __n = y18n({
209+
const __n = y18n({
210210
directory: path.join(__dirname, 'locales')
211211
}).__n
212212

213213
__n('%d cat', '%d cats', 1).should.equal('1 cat')
214214
})
215215

216216
it('uses the plural form if quantity is greater than 1', function () {
217-
var __n = y18n({
217+
const __n = y18n({
218218
directory: path.join(__dirname, 'locales')
219219
}).__n
220220

221221
__n('%d cat', '%d cats', 2).should.equal('2 cats')
222222
})
223223

224224
it('allows additional arguments to be printed', function () {
225-
var __n = y18n({
225+
const __n = y18n({
226226
directory: path.join(__dirname, 'locales')
227227
}).__n
228228

229229
__n('%d %s cat', '%d %s cats', 2, 'black').should.equal('2 black cats')
230230
})
231231

232232
it('allows an alternative locale to be set', function () {
233-
var __n = y18n({
233+
const __n = y18n({
234234
locale: 'pirate',
235235
directory: path.join(__dirname, 'locales')
236236
}).__n
@@ -241,12 +241,12 @@ describe('y18n', function () {
241241

242242
// See: https://github.com/bcoe/yargs/pull/210
243243
it('allows a quantity placeholder to be provided in the plural but not singular form', function () {
244-
var __n = y18n({
244+
const __n = y18n({
245245
directory: path.join(__dirname, 'locales')
246246
}).__n
247247

248-
var singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, 'tree')
249-
var plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, 'tree')
248+
const singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, 'tree')
249+
const plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, 'tree')
250250

251251
singular.should.equal('There is one monkey in the tree')
252252
plural.should.equal('There are 3 monkeys in the tree')
@@ -260,7 +260,7 @@ describe('y18n', function () {
260260
})
261261

262262
it('returns the pluralization immediately', function () {
263-
var __n = y18n({
263+
const __n = y18n({
264264
locale: 'fr',
265265
directory: path.join(__dirname, 'locales')
266266
}).__n
@@ -269,21 +269,21 @@ describe('y18n', function () {
269269
})
270270

271271
it('writes to the locale file if updateFiles is true', function (done) {
272-
var __n = y18n({
272+
const __n = y18n({
273273
locale: 'fr',
274274
directory: path.join(__dirname, 'locales')
275275
}).__n
276276

277277
__n('%d apple %s', '%d apples %s', 2, 'dude', function (err) {
278-
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
278+
const locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
279279
locale['%d apple %s'].one.should.equal('%d apple %s')
280280
locale['%d apple %s'].other.should.equal('%d apples %s')
281281
return done(err)
282282
})
283283
})
284284

285285
it('does not write the locale file if updateFiles is false', function (done) {
286-
var __n = y18n({
286+
const __n = y18n({
287287
locale: 'fr',
288288
updateFiles: false,
289289
directory: path.join(__dirname, 'locales')
@@ -299,7 +299,7 @@ describe('y18n', function () {
299299

300300
describe('setLocale', function () {
301301
it('switches the locale', function () {
302-
var i18n = y18n({
302+
const i18n = y18n({
303303
directory: path.join(__dirname, 'locales')
304304
})
305305

@@ -317,7 +317,7 @@ describe('y18n', function () {
317317
})
318318

319319
it('updates the locale with the new lookups provided', function () {
320-
var i18n = y18n({
320+
const i18n = y18n({
321321
locale: 'fr',
322322
directory: path.join(__dirname, 'locales')
323323
})
@@ -332,7 +332,7 @@ describe('y18n', function () {
332332
it('loads the locale from disk prior to updating the map', function () {
333333
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
334334

335-
var i18n = y18n({
335+
const i18n = y18n({
336336
locale: 'fr',
337337
directory: path.join(__dirname, 'locales')
338338
})

0 commit comments

Comments
 (0)