diff --git a/lib/index.js b/lib/index.js index ad3cd16..601ac31 100644 --- a/lib/index.js +++ b/lib/index.js @@ -24,7 +24,7 @@ function generateCode(content) { var code = ''; var value = typeof content === 'string' ? JSON.parse(content) : content; - value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029'); + value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029').replace(/\\/g, '\\\\'); code += 'function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push(\'' + value.replace(/\u0027/g, '\\u0027') + '\')\n}\n'; return code; diff --git a/package.json b/package.json index 45d6fa1..3ffb81b 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "babel-preset-es2015": "^6.22.0", "conventional-changelog-cli": "^1.2.0", "conventional-github-releaser": "^1.1.3", + "cross-env": "^5.0.5", "eslint": "^3.18.0", "eslint-config-vue": "^2.0.2", "eslint-plugin-vue": "^2.0.1", @@ -48,15 +49,15 @@ "url": "git+https://github.com/kazupon/vue-i18n-loader.git" }, "scripts": { - "build": "BABEL_ENV=production babel ./src --out-dir ./lib", + "build": "cross-env BABEL_ENV=production babel ./src --out-dir ./lib", "changelog": "conventional-changelog -i CHANGELOG.md -s -n ./node_modules/git-commit-message-convention/convention.js", "clean": "rm -rf ./coverage && rm -rf ./lib/*.js*", "coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov", "lint": "eslint ./src ./test", "release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js", "test": "npm run lint && npm run test:cover", - "test:cover": "BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava", - "test:unit": "BABEL_ENV=test ava", - "watch": "BABEL_ENV=development babel ./src --out-dir ./lib --watch" + "test:cover": "cross-env BABEL_ENV=test ./node_modules/.bin/nyc report --reporter=html ava", + "test:unit": "cross-env BABEL_ENV=test ava", + "watch": "cross-env BABEL_ENV=development babel ./src --out-dir ./lib --watch" } } diff --git a/src/index.js b/src/index.js index cb3bc89..48f93d7 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,7 @@ function generateCode (content) { value = JSON.stringify(value) .replace(/\u2028/g, '\\u2028') .replace(/\u2029/g, '\\u2029') + .replace(/\\/g, '\\\\') code += `function (Component) { Component.options.__i18n = Component.options.__i18n || [] diff --git a/test/index.test.js b/test/index.test.js index 36b7259..e819185 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -25,6 +25,16 @@ function assert (t, content) { ) } +function assertSpecial (t, content) { + const loader = new Loader(content, 2) + t.deepEqual( + loader._callback.content, `module.exports = function (Component) { + Component.options.__i18n = Component.options.__i18n || [] + Component.options.__i18n.push('{\"en\":{\"hello\":\"hello\\\\ngreat\\\\t\\\\\"world\\\\\"\"}}') +}\n` + ) +} + test('string', t => { const json = JSON.stringify({ en: { @@ -45,6 +55,26 @@ test('object', t => { assert(t, json) }) +test('string with special characters', t => { + const json = JSON.stringify({ + en: { + 'hello': 'hello\ngreat\t"world"' + } + }) + + assertSpecial(t, json) +}) + +test('object with special characters', t => { + const json = { + en: { + 'hello': 'hello\ngreat\t"world"' + } + } + + assertSpecial(t, json) +}) + test('version 2 less', t => { const loader = new Loader({}) t.deepEqual(loader._emitError, 'support webpack 2 later')