Skip to content

Commit

Permalink
Fix .json import issue (#944)
Browse files Browse the repository at this point in the history
* Fix .json import error
This is a regression we've added by #926 (tree-shaking-support)

* Add a test case.
  • Loading branch information
arunoda authored and nkzawa committed Feb 1, 2017
1 parent 5790986 commit ec2c8c6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server/build/loaders/emit-file-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function (content, sourceMap) {
}

if (query.transform) {
const transformed = query.transform({ content, sourceMap })
const transformed = query.transform({ content, sourceMap, interpolatedName })
return emit(transformed.content, transformed.sourceMap)
}

Expand Down
7 changes: 6 additions & 1 deletion server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ export default async function createCompiler (dir, { dev = false, quiet = false
// By default, our babel config does not transpile ES2015 module syntax because
// webpack knows how to handle them. (That's how it can do tree-shaking)
// But Node.js doesn't know how to handle them. So, we have to transpile them here.
transform ({ content, sourceMap }) {
transform ({ content, sourceMap, interpolatedName }) {
// Only handle .js files
if (!(/\.js$/.test(interpolatedName))) {
return { content, sourceMap }
}

const transpiled = babelCore.transform(content, {
presets: ['es2015'],
sourceMaps: dev ? 'both' : false,
Expand Down
3 changes: 3 additions & 0 deletions test/integration/basic/lib/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Zeit"
}
5 changes: 5 additions & 0 deletions test/integration/basic/pages/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from '../lib/data'

export default () => (
<div>{data.name}</div>
)
5 changes: 5 additions & 0 deletions test/integration/basic/test/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export default function ({ app }, suiteName, render) {
expect($('pre').text().includes(expectedErrorMessage)).toBeTruthy()
})

test('allows to import .json files', async () => {
const html = await render('/json')
expect(html.includes('Zeit')).toBeTruthy()
})

test('error', async () => {
const $ = await get$('/error')
expect($('pre').text()).toMatch(/This is an expected error/)
Expand Down

0 comments on commit ec2c8c6

Please sign in to comment.