From b16264d7a6d2cec930b47c442abb1de7f55bd906 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Sat, 5 Dec 2020 15:51:17 -0800 Subject: [PATCH] fix import proxies (JSON, CSS, etc) from npm packages (#1834) * fix import proxies from npm packages * add test --- snowpack/src/commands/dev.ts | 12 ++++-- test/build/import-json/import-json.test.js | 39 +++++++++++++++++++ test/build/import-json/package.json | 16 ++++++++ .../packages/json-test-pkg/file.json | 1 + .../packages/json-test-pkg/package.json | 4 ++ test/build/import-json/snowpack.config.json | 5 +++ test/build/import-json/src/file.json | 1 + test/build/import-json/src/index.js | 3 ++ yarn.lock | 3 ++ 9 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 test/build/import-json/import-json.test.js create mode 100644 test/build/import-json/package.json create mode 100644 test/build/import-json/packages/json-test-pkg/file.json create mode 100644 test/build/import-json/packages/json-test-pkg/package.json create mode 100644 test/build/import-json/snowpack.config.json create mode 100644 test/build/import-json/src/file.json create mode 100644 test/build/import-json/src/index.js diff --git a/snowpack/src/commands/dev.ts b/snowpack/src/commands/dev.ts index 7453fd24ed..9a9cfeb775 100644 --- a/snowpack/src/commands/dev.ts +++ b/snowpack/src/commands/dev.ts @@ -480,12 +480,16 @@ export async function startDevServer(commandOptions: CommandOptions): Promise { + beforeAll(() => { + setupBuildTest(__dirname); + files = readFiles( + [ + '_dist_/index.js', + '_dist_/file.json.proxy.js', + 'web_modules/json-test-pkg/file.json.proxy.js', + ], + { + cwd, + }, + ); + }); + + it('imports in source file are transformed correctly', () => { + expect(files['/_dist_/index.js'].replace(STRIP_WHITESPACE, '')).toEqual(`import testJsonData from './file.json.proxy.js'; +import testJsonPkgData from '../web_modules/json-test-pkg/file.json.proxy.js'; +console.log('loaded:', testJsonData, testJsonPkgData);`); + }); + + it('local json file is built as expected', () => { + expect(files['/_dist_/file.json.proxy.js'].replace(STRIP_WHITESPACE, '')).toEqual(`let json = {"test":true}; +export default json;`); + }); + + it('npm package json file is imported as expected', () => { + expect(files['/web_modules/json-test-pkg/file.json.proxy.js'].replace(STRIP_WHITESPACE, '')) + .toEqual(`let json = {"test-json-pkg":true}; +export default json;`); + }); +}); diff --git a/test/build/import-json/package.json b/test/build/import-json/package.json new file mode 100644 index 0000000000..d57930a600 --- /dev/null +++ b/test/build/import-json/package.json @@ -0,0 +1,16 @@ +{ + "private": true, + "version": "1.0.1", + "name": "@snowpack/test-import-json", + "description": "A test to make sure that JSON imports are working properly", + "scripts": { + "start": "snowpack dev", + "testbuild": "snowpack build" + }, + "dependencies": { + "json-test-pkg": "file:./packages/json-test-pkg" + }, + "devDependencies": { + "snowpack": "^2.14.3" + } +} diff --git a/test/build/import-json/packages/json-test-pkg/file.json b/test/build/import-json/packages/json-test-pkg/file.json new file mode 100644 index 0000000000..8f6e87f402 --- /dev/null +++ b/test/build/import-json/packages/json-test-pkg/file.json @@ -0,0 +1 @@ +{"test-json-pkg": true} \ No newline at end of file diff --git a/test/build/import-json/packages/json-test-pkg/package.json b/test/build/import-json/packages/json-test-pkg/package.json new file mode 100644 index 0000000000..81bf56199a --- /dev/null +++ b/test/build/import-json/packages/json-test-pkg/package.json @@ -0,0 +1,4 @@ +{ + "name": "test-json-pkg", + "version": "0.1.0" +} diff --git a/test/build/import-json/snowpack.config.json b/test/build/import-json/snowpack.config.json new file mode 100644 index 0000000000..2c1b129441 --- /dev/null +++ b/test/build/import-json/snowpack.config.json @@ -0,0 +1,5 @@ +{ + "mount": { + "./src": "/_dist_" + } +} diff --git a/test/build/import-json/src/file.json b/test/build/import-json/src/file.json new file mode 100644 index 0000000000..fb25aa1e11 --- /dev/null +++ b/test/build/import-json/src/file.json @@ -0,0 +1 @@ +{"test": true} \ No newline at end of file diff --git a/test/build/import-json/src/index.js b/test/build/import-json/src/index.js new file mode 100644 index 0000000000..5dbb721364 --- /dev/null +++ b/test/build/import-json/src/index.js @@ -0,0 +1,3 @@ +import testJsonData from './file.json'; +import testJsonPkgData from 'json-test-pkg/file.json'; +console.log('loaded:', testJsonData, testJsonPkgData); diff --git a/yarn.lock b/yarn.lock index 950f764509..d8b1592b88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8991,6 +8991,9 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1: resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus= +"json-test-pkg@file:./test/build/import-json/packages/json-test-pkg": + version "0.1.0" + json5@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"