Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Now longer create a fake AST but let rollup handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 11, 2018
1 parent 50db997 commit d3f1293
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 577 deletions.
172 changes: 19 additions & 153 deletions src/index.js
@@ -1,7 +1,9 @@
import { createFilter, makeLegalIdentifier } from 'rollup-pluginutils';
import {createFilter, makeLegalIdentifier} from 'rollup-pluginutils';

export default function json(options = {}) {
const filter = createFilter(options.include, options.exclude);
const indent = 'indent' in options ? options.indent : '\t';
const declarationType = options.preferConst ? 'const' : 'var';

return {
name: 'json',
Expand All @@ -11,161 +13,25 @@ export default function json(options = {}) {
if (!filter(id)) return null;

const data = JSON.parse(json);
let code = '';

const ast = {
type: 'Program',
sourceType: 'module',
start: 0,
end: null,
body: []
};

if (Object.prototype.toString.call(data) !== '[object Object]') {
code = `export default ${json};`;

ast.body.push({
type: 'ExportDefaultDeclaration',
start: 0,
end: code.length,
declaration: {
type: 'Literal',
start: 15,
end: code.length - 1,
value: null,
raw: 'null'
}
});
} else {
const indent = 'indent' in options ? options.indent : '\t';

const validKeys = [];
const invalidKeys = [];

Object.keys(data).forEach(key => {
if (key === makeLegalIdentifier(key)) {
validKeys.push(key);
} else {
invalidKeys.push(key);
}
});

let char = 0;

validKeys.forEach(key => {
const declarationType = options.preferConst ? 'const' : 'var';
const declaration = `export ${declarationType} ${key} = ${JSON.stringify(data[key])};`;

const start = char;
const end = start + declaration.length;

// generate fake AST node while we're here
ast.body.push({
type: 'ExportNamedDeclaration',
start: char,
end: char + declaration.length,
declaration: {
type: 'VariableDeclaration',
start: start + 7, // 'export '.length
end,
declarations: [
{
type: 'VariableDeclarator',
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length
end: end - 1,
id: {
type: 'Identifier',
start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length
end: start + 7 + declarationType.length + 1 + key.length, // `export ${declarationType} ${key}`.length
name: key
},
init: {
type: 'Literal',
start: start +
7 +
declarationType.length +
1 +
key.length +
3, // `export ${declarationType} ${key} = `.length
end: end - 1,
value: null,
raw: 'null'
}
}
],
kind: declarationType
},
specifiers: [],
source: null
});

char = end + 1;
code += `${declaration}\n`;
});

const defaultExportNode = {
type: 'ExportDefaultDeclaration',
start: char,
end: null,
declaration: {
type: 'ObjectExpression',
start: char + 15,
end: null,
properties: []
}
};

char += 17 + indent.length; // 'export default {\n\t'.length'

const defaultExportRows = validKeys
.map(key => {
const row = `${key}: ${key}`;

const start = char;
const end = start + row.length;

defaultExportNode.declaration.properties.push({
type: 'Property',
start,
end,
method: false,
shorthand: false,
computed: false,
key: {
type: 'Identifier',
start,
end: start + key.length,
name: key
},
value: {
type: 'Identifier',
start: start + key.length + 2,
end,
name: key
},
kind: 'init'
});

char += row.length + (2 + indent.length); // ',\n\t'.length

return row;
})
.concat(
invalidKeys.map(key => `"${key}": ${JSON.stringify(data[key])}`)
);

code += `export default {\n${indent}${defaultExportRows.join(`,\n${indent}`)}\n};`;
ast.body.push(defaultExportNode);

const end = code.length;

defaultExportNode.declaration.end = end - 1;
defaultExportNode.end = end;
return {code: `export default ${json};\n`, map: {mappings: ''}};
}

ast.end = code.length;

return { ast, code, map: { mappings: '' } };
let namedExportCode = '';
const defaultExportRows = [];
Object.keys(data).forEach(key => {
if (key === makeLegalIdentifier(key)) {
defaultExportRows.push(`${key}: ${key}`);
namedExportCode += `export ${declarationType} ${key} = ${JSON.stringify(data[key])};\n`;
} else {
defaultExportRows.push(`"${key}": ${JSON.stringify(data[key])}`);
}
});

return {
code: namedExportCode + `export default {\n${indent}${defaultExportRows.join(`,\n${indent}`)}\n};\n`,
map: {mappings: ''}
};
}
};
}
3 changes: 3 additions & 0 deletions test/samples/array/config.json
@@ -0,0 +1,3 @@
[
1, 2, 3
]
3 changes: 3 additions & 0 deletions test/samples/array/main.js
@@ -0,0 +1,3 @@
import config from './config.json';

assert.deepEqual(config, [1, 2, 3]);
5 changes: 0 additions & 5 deletions test/samples/ast/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions test/samples/custom-indent/input.json

This file was deleted.

4 changes: 0 additions & 4 deletions test/samples/custom-indent/output.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/samples/form/customIndent.js
@@ -0,0 +1,10 @@
export var validKey = true;
export var nested = {"subKey":"ok"};
export var array = [1,"2"];
export default {
validKey: validKey,
"invalid-key": 1,
nested: nested,
array: array,
"function": "not used"
};
10 changes: 10 additions & 0 deletions test/samples/form/default.js
@@ -0,0 +1,10 @@
export var validKey = true;
export var nested = {"subKey":"ok"};
export var array = [1,"2"];
export default {
validKey: validKey,
"invalid-key": 1,
nested: nested,
array: array,
"function": "not used"
};
9 changes: 9 additions & 0 deletions test/samples/form/input.json
@@ -0,0 +1,9 @@
{
"validKey": true,
"invalid-key": 1,
"nested": {
"subKey": "ok"
},
"array": [1, "2"],
"function": "not used"
}
10 changes: 10 additions & 0 deletions test/samples/form/preferConst.js
@@ -0,0 +1,10 @@
export const validKey = true;
export const nested = {"subKey":"ok"};
export const array = [1,"2"];
export default {
validKey: validKey,
"invalid-key": 1,
nested: nested,
array: array,
"function": "not used"
};
1 change: 1 addition & 0 deletions test/samples/literal/config.json
@@ -0,0 +1 @@
true
3 changes: 3 additions & 0 deletions test/samples/literal/main.js
@@ -0,0 +1,3 @@
import config from './config.json';

assert.equal(config, true);

0 comments on commit d3f1293

Please sign in to comment.