rollup-plugin-json
Advanced tools
Comparing version 2.0.2 to 2.1.0
# rollup-plugin-json changelog | ||
## 2.1.0 | ||
* Add support for `preferConst` option ([#16](https://github.com/rollup/rollup-plugin-json/pull/16)) | ||
* Handle JSON files with no valid identifier keys ([#19](https://github.com/rollup/rollup-plugin-json/issues/19)) | ||
## 2.0.2 | ||
@@ -4,0 +9,0 @@ |
@@ -14,6 +14,9 @@ 'use strict'; | ||
transform: function transform ( json, id ) { | ||
if ( id.slice( -5 ) !== '.json' ) return null; | ||
if ( !filter( id ) ) return null; | ||
if ( id.slice( -5 ) !== '.json' ) { return null; } | ||
if ( !filter( id ) ) { return null; } | ||
var code; | ||
var code = ''; | ||
// Manipulating properties so keeping as `let` | ||
// eslint-disable-next-line prefer-const | ||
var ast = { | ||
@@ -57,5 +60,7 @@ type: 'Program', | ||
var char = 0; | ||
var namedExports = validKeys.map( function (key) { | ||
var declaration = "export var " + key + " = " + (JSON.stringify( data[ key ] )) + ";"; | ||
validKeys.forEach( function (key) { | ||
var declarationType = options.preferConst ? 'const' : 'var'; | ||
var declaration = "export " + declarationType + " " + key + " = " + (JSON.stringify( data[ key ] )) + ";"; | ||
var start = char; | ||
@@ -71,3 +76,3 @@ var end = start + declaration.length; | ||
type: 'VariableDeclaration', | ||
start: start + 7, | ||
start: start + 7, // 'export '.length | ||
end: end, | ||
@@ -77,8 +82,8 @@ declarations: [ | ||
type: 'VariableDeclarator', | ||
start: start + 11, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: end - 1, | ||
id: { | ||
type: 'Identifier', | ||
start: start + 11, | ||
end: start + 11 + key.length, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: start + 7 + declarationType.length + 1 + key.length, // `export ${declarationType} ${key}`.length | ||
name: key | ||
@@ -88,3 +93,3 @@ }, | ||
type: 'Literal', | ||
start: start + 11 + key.length + 3, | ||
start: start + 7 + declarationType.length + 1 + key.length + 3, // `export ${declarationType} ${key} = `.length | ||
end: end - 1, | ||
@@ -96,3 +101,3 @@ value: null, | ||
], | ||
kind: 'var' | ||
kind: declarationType | ||
}, | ||
@@ -104,3 +109,3 @@ specifiers: [], | ||
char = end + 1; | ||
return declaration; | ||
code += declaration + "\n"; | ||
}); | ||
@@ -155,6 +160,4 @@ | ||
var defaultExportString = "export default {\n\t" + (defaultExportRows.join( ',\n\t' )) + "\n};"; | ||
code += "export default {\n\t" + (defaultExportRows.join( ',\n\t' )) + "\n};"; | ||
ast.body.push( defaultExportNode ); | ||
code = (namedExports.join( '\n' )) + "\n" + defaultExportString; | ||
@@ -161,0 +164,0 @@ var end = code.length; |
@@ -1,2 +0,2 @@ | ||
import { makeLegalIdentifier, createFilter } from 'rollup-pluginutils'; | ||
import { createFilter, makeLegalIdentifier } from 'rollup-pluginutils'; | ||
@@ -12,6 +12,9 @@ function json ( options ) { | ||
transform: function transform ( json, id ) { | ||
if ( id.slice( -5 ) !== '.json' ) return null; | ||
if ( !filter( id ) ) return null; | ||
if ( id.slice( -5 ) !== '.json' ) { return null; } | ||
if ( !filter( id ) ) { return null; } | ||
var code; | ||
var code = ''; | ||
// Manipulating properties so keeping as `let` | ||
// eslint-disable-next-line prefer-const | ||
var ast = { | ||
@@ -55,5 +58,7 @@ type: 'Program', | ||
var char = 0; | ||
var namedExports = validKeys.map( function (key) { | ||
var declaration = "export var " + key + " = " + (JSON.stringify( data[ key ] )) + ";"; | ||
validKeys.forEach( function (key) { | ||
var declarationType = options.preferConst ? 'const' : 'var'; | ||
var declaration = "export " + declarationType + " " + key + " = " + (JSON.stringify( data[ key ] )) + ";"; | ||
var start = char; | ||
@@ -69,3 +74,3 @@ var end = start + declaration.length; | ||
type: 'VariableDeclaration', | ||
start: start + 7, | ||
start: start + 7, // 'export '.length | ||
end: end, | ||
@@ -75,8 +80,8 @@ declarations: [ | ||
type: 'VariableDeclarator', | ||
start: start + 11, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: end - 1, | ||
id: { | ||
type: 'Identifier', | ||
start: start + 11, | ||
end: start + 11 + key.length, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: start + 7 + declarationType.length + 1 + key.length, // `export ${declarationType} ${key}`.length | ||
name: key | ||
@@ -86,3 +91,3 @@ }, | ||
type: 'Literal', | ||
start: start + 11 + key.length + 3, | ||
start: start + 7 + declarationType.length + 1 + key.length + 3, // `export ${declarationType} ${key} = `.length | ||
end: end - 1, | ||
@@ -94,3 +99,3 @@ value: null, | ||
], | ||
kind: 'var' | ||
kind: declarationType | ||
}, | ||
@@ -102,3 +107,3 @@ specifiers: [], | ||
char = end + 1; | ||
return declaration; | ||
code += declaration + "\n"; | ||
}); | ||
@@ -153,6 +158,4 @@ | ||
var defaultExportString = "export default {\n\t" + (defaultExportRows.join( ',\n\t' )) + "\n};"; | ||
code += "export default {\n\t" + (defaultExportRows.join( ',\n\t' )) + "\n};"; | ||
ast.body.push( defaultExportNode ); | ||
code = (namedExports.join( '\n' )) + "\n" + defaultExportString; | ||
@@ -159,0 +162,0 @@ var end = code.length; |
{ | ||
"name": "rollup-plugin-json", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"main": "dist/rollup-plugin-json.cjs.js", | ||
@@ -9,5 +9,6 @@ "module": "dist/rollup-plugin-json.es.js", | ||
"eslint": "^3.4.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"mocha": "^3.0.2", | ||
"rollup": "^0.34.13", | ||
"rollup-plugin-buble": "^0.13.0", | ||
"rollup": "^0.37.0", | ||
"rollup-plugin-buble": "^0.15.0", | ||
"rollup-plugin-node-resolve": "^2.0.0", | ||
@@ -28,2 +29,6 @@ "source-map-support": "^0.4.2" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/rollup/rollup-plugin-json.git" | ||
}, | ||
"dependencies": { | ||
@@ -30,0 +35,0 @@ "rollup-pluginutils": "^1.5.2" |
@@ -38,2 +38,3 @@ # rollup-plugin-json | ||
exclude: [ 'node_modules/foo/**', 'node_modules/bar/**' ], // Default: undefined | ||
preferConst: true, // Default: false | ||
}) | ||
@@ -40,0 +41,0 @@ ] |
@@ -13,3 +13,6 @@ import { createFilter, makeLegalIdentifier } from 'rollup-pluginutils'; | ||
let code; | ||
let code = ''; | ||
// Manipulating properties so keeping as `let` | ||
// eslint-disable-next-line prefer-const | ||
let ast = { | ||
@@ -53,5 +56,7 @@ type: 'Program', | ||
let char = 0; | ||
const namedExports = validKeys.map( key => { | ||
const declaration = `export var ${key} = ${JSON.stringify( data[ key ] )};`; | ||
validKeys.forEach( key => { | ||
const declarationType = options.preferConst ? 'const' : 'var'; | ||
const declaration = `export ${declarationType} ${key} = ${JSON.stringify( data[ key ] )};`; | ||
const start = char; | ||
@@ -67,3 +72,3 @@ const end = start + declaration.length; | ||
type: 'VariableDeclaration', | ||
start: start + 7, | ||
start: start + 7, // 'export '.length | ||
end, | ||
@@ -73,8 +78,8 @@ declarations: [ | ||
type: 'VariableDeclarator', | ||
start: start + 11, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: end - 1, | ||
id: { | ||
type: 'Identifier', | ||
start: start + 11, | ||
end: start + 11 + key.length, | ||
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length | ||
end: start + 7 + declarationType.length + 1 + key.length, // `export ${declarationType} ${key}`.length | ||
name: key | ||
@@ -84,3 +89,3 @@ }, | ||
type: 'Literal', | ||
start: start + 11 + key.length + 3, | ||
start: start + 7 + declarationType.length + 1 + key.length + 3, // `export ${declarationType} ${key} = `.length | ||
end: end - 1, | ||
@@ -92,3 +97,3 @@ value: null, | ||
], | ||
kind: 'var' | ||
kind: declarationType | ||
}, | ||
@@ -100,3 +105,3 @@ specifiers: [], | ||
char = end + 1; | ||
return declaration; | ||
code += `${declaration}\n`; | ||
}); | ||
@@ -140,3 +145,3 @@ | ||
start: start + key.length + 2, | ||
end: end, | ||
end, | ||
name: key | ||
@@ -152,6 +157,4 @@ }, | ||
const defaultExportString = `export default {\n\t${defaultExportRows.join( ',\n\t' )}\n};`; | ||
code += `export default {\n\t${defaultExportRows.join( ',\n\t' )}\n};`; | ||
ast.body.push( defaultExportNode ); | ||
code = `${namedExports.join( '\n' )}\n${defaultExportString}`; | ||
@@ -158,0 +161,0 @@ const end = code.length; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
32560
418
48
0
7