From ec2c8c6784aae680964a32ab7a5bc2b657e5cf98 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 1 Feb 2017 11:47:15 +0530 Subject: [PATCH] Fix .json import issue (#944) * Fix .json import error This is a regression we've added by #926 (tree-shaking-support) * Add a test case. --- server/build/loaders/emit-file-loader.js | 2 +- server/build/webpack.js | 7 ++++++- test/integration/basic/lib/data.json | 3 +++ test/integration/basic/pages/json.js | 5 +++++ test/integration/basic/test/rendering.js | 5 +++++ 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/integration/basic/lib/data.json create mode 100644 test/integration/basic/pages/json.js diff --git a/server/build/loaders/emit-file-loader.js b/server/build/loaders/emit-file-loader.js index e7138d43..1a76cd18 100644 --- a/server/build/loaders/emit-file-loader.js +++ b/server/build/loaders/emit-file-loader.js @@ -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) } diff --git a/server/build/webpack.js b/server/build/webpack.js index 3399356a..383c182a 100644 --- a/server/build/webpack.js +++ b/server/build/webpack.js @@ -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, diff --git a/test/integration/basic/lib/data.json b/test/integration/basic/lib/data.json new file mode 100644 index 00000000..42ee7df2 --- /dev/null +++ b/test/integration/basic/lib/data.json @@ -0,0 +1,3 @@ +{ + "name": "Zeit" +} \ No newline at end of file diff --git a/test/integration/basic/pages/json.js b/test/integration/basic/pages/json.js new file mode 100644 index 00000000..348a61d7 --- /dev/null +++ b/test/integration/basic/pages/json.js @@ -0,0 +1,5 @@ +import data from '../lib/data' + +export default () => ( +
{data.name}
+) diff --git a/test/integration/basic/test/rendering.js b/test/integration/basic/test/rendering.js index ad161ec3..a99a4c48 100644 --- a/test/integration/basic/test/rendering.js +++ b/test/integration/basic/test/rendering.js @@ -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/)