diff --git a/src/index.ts b/src/index.ts index dcee08b..c6e280d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import path from 'path'; import fastGlob from 'fast-glob'; import { Plugin } from 'esbuild'; @@ -10,9 +11,12 @@ const EsbuildPluginImportGlob = (): Plugin => ({ } return { - path: args.path, + // make sure that imports are properly scoped to directories that are requested from + // otherwise results get overwritten + path: path.resolve(args.resolveDir, args.path), namespace: 'import-glob', pluginData: { + path: args.path, resolveDir: args.resolveDir, }, }; @@ -20,12 +24,12 @@ const EsbuildPluginImportGlob = (): Plugin => ({ build.onLoad({ filter: /.*/, namespace: 'import-glob' }, async (args) => { const files = ( - await fastGlob(args.path, { + await fastGlob(args.pluginData.path, { cwd: args.pluginData.resolveDir, }) ).sort(); - let importerCode = ` + const importerCode = ` ${files .map((module, index) => `import * as module${index} from '${module}'`) .join(';')} @@ -36,7 +40,7 @@ const EsbuildPluginImportGlob = (): Plugin => ({ export default modules; export const filenames = [${files - .map((module, index) => `'${module}'`) + .map((module) => `'${module}'`) .join(',')}] `; diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index b30a8cd..681b6f0 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -1,5 +1,43 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`test keeps proper scope of imports 1`] = ` +Object { + "default": Object { + "default": Array [ + Object { + "default": "a", + }, + Object { + "default": "b", + }, + ], + "filenames": Array [ + "./cases/a.js", + "./cases/b.js", + ], + }, +} +`; + +exports[`test keeps proper scope of imports 2`] = ` +Object { + "default": Object { + "default": Array [ + Object { + "default": "c", + }, + Object { + "default": "d", + }, + ], + "filenames": Array [ + "./cases/c.js", + "./cases/d.js", + ], + }, +} +`; + exports[`test should 1`] = ` Object { "default": Array [ diff --git a/tests/conflicts/conflictA/cases/a.js b/tests/conflicts/conflictA/cases/a.js new file mode 100644 index 0000000..0ed5c30 --- /dev/null +++ b/tests/conflicts/conflictA/cases/a.js @@ -0,0 +1 @@ +export default 'a'; diff --git a/tests/conflicts/conflictA/cases/b.js b/tests/conflicts/conflictA/cases/b.js new file mode 100644 index 0000000..a68ac28 --- /dev/null +++ b/tests/conflicts/conflictA/cases/b.js @@ -0,0 +1 @@ +export default 'b'; diff --git a/tests/conflicts/conflictA/index.js b/tests/conflicts/conflictA/index.js new file mode 100644 index 0000000..feb0178 --- /dev/null +++ b/tests/conflicts/conflictA/index.js @@ -0,0 +1,2 @@ +// this is the same as in conflictB +export * as default from './cases/*.js'; diff --git a/tests/conflicts/conflictB/cases/c.js b/tests/conflicts/conflictB/cases/c.js new file mode 100644 index 0000000..37a4d86 --- /dev/null +++ b/tests/conflicts/conflictB/cases/c.js @@ -0,0 +1 @@ +export default 'c'; diff --git a/tests/conflicts/conflictB/cases/d.js b/tests/conflicts/conflictB/cases/d.js new file mode 100644 index 0000000..d773ff7 --- /dev/null +++ b/tests/conflicts/conflictB/cases/d.js @@ -0,0 +1 @@ +export default 'd'; diff --git a/tests/conflicts/conflictB/index.js b/tests/conflicts/conflictB/index.js new file mode 100644 index 0000000..2862fbb --- /dev/null +++ b/tests/conflicts/conflictB/index.js @@ -0,0 +1,2 @@ +// this is the same as in conflictA +export * as default from './cases/*.js'; diff --git a/tests/conflicts/main.ts b/tests/conflicts/main.ts new file mode 100644 index 0000000..0a3f630 --- /dev/null +++ b/tests/conflicts/main.ts @@ -0,0 +1,5 @@ +// @ts-ignore +import * as conflictingSetA from './conflictA'; +import * as conflictingSetB from './conflictB'; + +console.log(conflictingSetA, conflictingSetB); diff --git a/tests/index.test.ts b/tests/index.test.ts index 89ff5ba..0892e37 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -4,8 +4,29 @@ import path from 'path'; describe('test', () => { it('should', async () => { + const logs = await runTestFile('service/main.ts'); + + const migrationModules = logs[0][0]; + expect(migrationModules).toMatchSnapshot(); + + const entitiesModules = logs[1][0]; + expect(entitiesModules).toMatchSnapshot(); + + const migration2Modules = logs[2][0]; + expect(migrationModules.default).toEqual(migration2Modules); + }); + + it('keeps proper scope of imports', async () => { + const logs = await runTestFile('conflicts/main.ts'); + + const [setA, setB] = logs[0]; + expect(setA).toMatchSnapshot(); + expect(setB).toMatchSnapshot(); + }); + + async function runTestFile(testFile) { const result = await build({ - entryPoints: [path.resolve(__dirname, 'service', 'main.ts')], + entryPoints: [path.resolve(__dirname, testFile)], write: false, plugins: [requireContext()], outfile: `tests/bundle.js`, @@ -16,13 +37,6 @@ describe('test', () => { eval(`(console) => ${result.outputFiles[0].text}`)({ log: fakeLogger }); - const migrationModules = fakeLogger.mock.calls[0][0]; - expect(migrationModules).toMatchSnapshot(); - - const entitesModules = fakeLogger.mock.calls[1][0]; - expect(entitesModules).toMatchSnapshot(); - - const migration2Modules = fakeLogger.mock.calls[2][0]; - expect(migrationModules.default).toEqual(migration2Modules); - }); + return fakeLogger.mock.calls; + } });