1 | import fs from 'fs'
|
2 | import tmp from 'tmp'
|
3 | import path from 'path'
|
4 | import { expect } from 'chai'
|
5 | import { transform, transformFileSync } from 'babel-core'
|
6 |
|
7 | tmp.setGracefulCleanup()
|
8 |
|
9 | describe('babel-plugin-inline-json-imports', () => {
|
10 | it('inlines simple JSON imports', () => {
|
11 | const t = configureTransform()
|
12 | const result = t(`
|
13 | import json from '../test/fixtures/example.json'
|
14 |
|
15 | console.log(json)
|
16 | `)
|
17 |
|
18 | expect(normalize(result.code)).to.equal(normalize(`
|
19 | const json = { example: true }
|
20 |
|
21 | console.log(json)
|
22 | `))
|
23 | })
|
24 |
|
25 |
|
26 |
|
27 | it('inlines simple systemjs JSON imports', () => {
|
28 | const t = configureTransform()
|
29 | const result = t(`
|
30 | import json from '../test/fixtures/example.json!json'
|
31 |
|
32 | console.log(json)
|
33 | `)
|
34 |
|
35 | expect(normalize(result.code)).to.equal(normalize(`
|
36 | const json = { example: true }
|
37 |
|
38 | console.log(json)
|
39 | `))
|
40 | })
|
41 |
|
42 | it('supports destructuring of the JSON imports', () => {
|
43 | const t = configureTransform()
|
44 | const result = t(`
|
45 | import {example} from '../test/fixtures/example.json'
|
46 |
|
47 | console.log(example)
|
48 | `)
|
49 |
|
50 | expect(normalize(result.code)).to.equal(normalize(`
|
51 | const { example: example } = { example: true }
|
52 |
|
53 | console.log(example)
|
54 | `))
|
55 | })
|
56 |
|
57 | it('does not inline other kinds of imports', () => {
|
58 | const t = configureTransform()
|
59 | const result = t(`
|
60 | import babel from 'babel-core'
|
61 | import css from './styles.css'
|
62 | import json from '../test/fixtures/example.json'
|
63 |
|
64 | console.log(json)
|
65 | `)
|
66 |
|
67 | expect(normalize(result.code)).to.equal(normalize(`
|
68 | import babel from 'babel-core'
|
69 | import css from './styles.css'
|
70 | const json = { example: true }
|
71 |
|
72 | console.log(json)
|
73 | `))
|
74 | })
|
75 |
|
76 | it('respects the file location of the imports', () => {
|
77 | const contents = `
|
78 | import json from '../fixtures/example.json'
|
79 |
|
80 | console.log(json)
|
81 | `
|
82 | const file = tmp.fileSync({ postfix: '.js', dir: './test/tmp' })
|
83 | fs.writeFileSync(file.name, contents)
|
84 |
|
85 | const t = configureTransform({}, true)
|
86 | const result = t(file.name)
|
87 |
|
88 | expect(normalize(result.code)).to.equal(normalize(`
|
89 | const json = { example: true }
|
90 |
|
91 | console.log(json)
|
92 | `))
|
93 | })
|
94 |
|
95 | function configureTransform(options = {}, isFile) {
|
96 | return function configuredTransform(string) {
|
97 | const transformOptions = {
|
98 | babelrc: false,
|
99 | presets: [],
|
100 | plugins: [path.resolve('./src'), options],
|
101 | }
|
102 |
|
103 | if (isFile) {
|
104 | return transformFileSync(string, transformOptions)
|
105 | } else {
|
106 | return transform(string.trim(), transformOptions)
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | function normalize(code) {
|
112 | return transform(squashWhitespace(code)).code
|
113 | }
|
114 |
|
115 | function squashWhitespace(code) {
|
116 | return code.trim().replace(/\s+/g, '\n')
|
117 | }
|
118 | })
|