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

Commit

Permalink
Use dataToEsm in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 11, 2018
1 parent 70856b5 commit fb60b3e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 33 deletions.
16 changes: 2 additions & 14 deletions src/index.js
@@ -1,9 +1,8 @@
import {createFilter, makeLegalIdentifier} from 'rollup-pluginutils';
import {createFilter, dataToEsm} 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 @@ -17,19 +16,8 @@ export default function json(options = {}) {
return {code: `export default ${json};\n`, 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`,
code: dataToEsm(data, {preferConst: options.preferConst, indent}),
map: {mappings: ''}
};
}
Expand Down
5 changes: 3 additions & 2 deletions test/samples/form/customIndent.js
@@ -1,6 +1,7 @@
export var validKey = true;
export var nested = {"subKey":"ok"};
export var array = [1,"2"];
export var nested = { subKey:"ok" };
export var array = [ 1,
"2" ];
export default {
validKey: validKey,
"invalid-key": 1,
Expand Down
5 changes: 3 additions & 2 deletions test/samples/form/default.js
@@ -1,6 +1,7 @@
export var validKey = true;
export var nested = {"subKey":"ok"};
export var array = [1,"2"];
export var nested = {subKey:"ok" };
export var array = [1,
"2" ];
export default {
validKey: validKey,
"invalid-key": 1,
Expand Down
5 changes: 3 additions & 2 deletions test/samples/form/preferConst.js
@@ -1,6 +1,7 @@
export const validKey = true;
export const nested = {"subKey":"ok"};
export const array = [1,"2"];
export const nested = {subKey:"ok" };
export const array = [1,
"2" ];
export default {
validKey: validKey,
"invalid-key": 1,
Expand Down
26 changes: 13 additions & 13 deletions test/test.js
Expand Up @@ -16,8 +16,8 @@ describe('rollup-plugin-json', () => {
plugins: [json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
const fn = new Function('assert', code);
.then(generated => {
const fn = new Function('assert', generated.code);
fn(assert);
});
});
Expand All @@ -29,8 +29,8 @@ describe('rollup-plugin-json', () => {
plugins: [json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
const fn = new Function('assert', code);
.then(generated => {
const fn = new Function('assert', generated.code);
fn(assert);
});
});
Expand All @@ -42,8 +42,8 @@ describe('rollup-plugin-json', () => {
plugins: [json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
const fn = new Function('assert', code);
.then(generated => {
const fn = new Function('assert', generated.code);
fn(assert);
});
});
Expand All @@ -55,14 +55,14 @@ describe('rollup-plugin-json', () => {
plugins: [json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
.then(generated => {
const exports = {};
const fn = new Function('exports', code);
const fn = new Function('exports', generated.code);
fn(exports);

assert.equal(exports.version, '1.33.7');
assert.equal(
code.indexOf('this-should-be-excluded'),
generated.code.indexOf('this-should-be-excluded'),
-1,
'should exclude unused properties'
);
Expand All @@ -76,8 +76,8 @@ describe('rollup-plugin-json', () => {
plugins: [resolve({ extensions: ['.js', '.json'] }), json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
const fn = new Function('assert', code);
.then(generated => {
const fn = new Function('assert', generated.code);
fn(assert);
});
});
Expand All @@ -89,8 +89,8 @@ describe('rollup-plugin-json', () => {
plugins: [json()]
})
.then(bundle => bundle.generate({ format: 'cjs' }))
.then(({code}) => {
const fn = new Function('assert', code);
.then(generated => {
const fn = new Function('assert', generated.code);
fn(assert);
});
});
Expand Down

0 comments on commit fb60b3e

Please sign in to comment.