UNPKG

react-imported-component

Version:
109 lines (108 loc) 6.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var path_1 = require("path"); var scanForImports_1 = require("../scanForImports"); var shared_1 = require("../shared"); describe('scanForImports', function () { var rel = (0, path_1.dirname)(__dirname); var root = '.'; var rootRel = '.' + process.cwd(); var sourceFile = rel + "/a"; it('should map simple import', function () { var imports = {}; (0, scanForImports_1.remapImports)([{ file: rel + "/a", content: 'blabla;import("./b.js"); blabla;' }], rel, rel, shared_1.getRelative, imports, function () { return true; }); expect(Object.values(imports)).toEqual(["[() => import('./b.js'), '', './b.js', false] /* from ./a */"]); }); it('handles imports in jsdoc', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: sourceFile, content: "\n /**\n * @type {import('wrong-import')}\n */\n import(/* comment:valuable */ \"./a.js\");\n import(\"./b.js\");\n // import('another-wrong-import');// FIXME: temporary removed\n ", }, ], rel, rel, shared_1.getRelative, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* comment:valuable */'./a.js'), '', './a.js', false] /* from ./a */", "[() => import('./b.js'), '', './b.js', false] /* from ./a */", ]); }); it('should map client-side import', function () { var imports = {}; (0, scanForImports_1.remapImports)([{ file: sourceFile, content: 'blabla;import(/* client-side */"./a.js"); blabla;' }], rel, rel, shared_1.getRelative, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* client-side */'./a.js'), '', './a.js', true] /* from ./a */", ]); }); it('should map simple import with a comment', function () { var imports = {}; (0, scanForImports_1.remapImports)([{ file: sourceFile, content: 'blabla;import(/* comment:42 */"./a.js"); blabla;' }], rel, rel, shared_1.getRelative, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* comment:42 */'./a.js'), '', './a.js', false] /* from ./a */", ]); }); it('should map complex import', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: sourceFile, content: 'blabla;import(/* webpack: "123" */"./a.js"); blabla; import(/* webpack: 123 */ \'./b.js\');', }, ], rel, rel, shared_1.getRelative, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* webpack: \"123\" */'./a.js'), '', './a.js', false] /* from ./a */", "[() => import(/* webpack: 123 */'./b.js'), '', './b.js', false] /* from ./a */", ]); }); it('should match chunk name', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: 'a', content: 'blabla;import(/* webpackChunkName: "chunk-a" */"./a.js"); blabla; import(/* webpack: 123 */ \'./b.js\');', }, ], root, root, function (a, b) { return a + b; }, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* webpackChunkName: \"chunk-a\" */'" + rootRel + "/a.js'), 'chunk-a', '" + rootRel + "/a.js', false] /* from .a */", "[() => import(/* webpack: 123 */'" + rootRel + "/b.js'), '', '" + rootRel + "/b.js', false] /* from .a */", ]); }); it('should override chunk name', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: 'a', content: 'blabla;import(/* webpackChunkName: "chunk-a" */"./a.js"); blabla; import(/* webpackChunkName: "chunk-b" */"./b.js"); import(/* webpackChunkName: "chunk-c" */"./c.js");', }, ], root, root, function (a, b) { return a + b; }, imports, function (imported) { return imported.indexOf('c.js') < 0; }, function (imported, _, options) { return (imported.indexOf('a.js') > 0 ? "test-" + options.chunkName + "-test" : 'bundle-b'); }); expect(Object.values(imports)).toEqual([ "[() => import(/* webpackChunkName: \"chunk-a\" */'" + rootRel + "/a.js'), 'test-chunk-a-test', '" + rootRel + "/a.js', false] /* from .a */", "[() => import(/* webpackChunkName: \"chunk-b\" */'" + rootRel + "/b.js'), 'bundle-b', '" + rootRel + "/b.js', false] /* from .a */", ]); }); it('should match support multiline imports', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: 'a', content: "\n blabla;import(\n /* webpackChunkName: \"chunk-a\" */\n \"./a.js\"\n );\n something else\n import(\n // ts-ignore\n \"./b.js\"\n );\n ", }, ], root, root, function (a, b) { return a + b; }, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* webpackChunkName: \"chunk-a\" */'" + rootRel + "/a.js'), 'chunk-a', '" + rootRel + "/a.js', false] /* from .a */", "[() => import('" + rootRel + "/b.js'), '', '" + rootRel + "/b.js', false] /* from .a */", ]); }); it('should remove webpackPrefetch and webpackPreload', function () { var imports = {}; (0, scanForImports_1.remapImports)([ { file: 'a', content: 'blabla;import(/* webpackPrefetch: true *//* webpack: "123" */"./a.js"); blabla; import(/* webpackPreload: true */ \'./b.js\');', }, ], root, root, function (a, b) { return a + b; }, imports, function () { return true; }); expect(Object.values(imports)).toEqual([ "[() => import(/* *//* webpack: \"123\" */'" + rootRel + "/a.js'), '', '" + rootRel + "/a.js', false] /* from .a */", "[() => import(/* */'" + rootRel + "/b.js'), '', '" + rootRel + "/b.js', false] /* from .a */", ]); }); });