diff --git a/packages/cross-import/package.json b/packages/cross-import/package.json index 0e721ff..697df72 100644 --- a/packages/cross-import/package.json +++ b/packages/cross-import/package.json @@ -62,6 +62,7 @@ "sucrase": "^3.35.0" }, "devDependencies": { + "@master/css": "2.0.0-rc.24", "pretty-bytes": "^6.1.0" } } \ No newline at end of file diff --git a/packages/cross-import/src/index.ts b/packages/cross-import/src/index.ts index 33943dd..ab0378e 100644 --- a/packages/cross-import/src/index.ts +++ b/packages/cross-import/src/index.ts @@ -1,5 +1,5 @@ import { transform } from 'sucrase' -import { Module, createRequire } from 'module' +import { Module } from 'module' import { readFileSync } from 'fs' import { runInThisContext } from 'vm' import { dirname } from 'path' @@ -26,7 +26,7 @@ export default function crossImport(modulePath: string): any { }) const mod = new Module(__filename) mod.filename = __dirname - mod.require = createRequire(__filename) + mod.require = require mod.path = dirname(__filename) // Compile wrapped script const compiledModule = runInThisContext(Module.wrap(moduleCode), { diff --git a/packages/cross-import/tests/external.test.ts b/packages/cross-import/tests/external.test.ts new file mode 100644 index 0000000..e0ca07f --- /dev/null +++ b/packages/cross-import/tests/external.test.ts @@ -0,0 +1,9 @@ +import { resolve } from 'path' +import crossImport from '../src' + +it('read module with third-party deps', () => { + expect( + crossImport(resolve(__dirname, 'external.ts')).default + ) + .toBeDefined() +}) \ No newline at end of file diff --git a/packages/cross-import/tests/external.ts b/packages/cross-import/tests/external.ts index 1f67b97..b40bb8f 100644 --- a/packages/cross-import/tests/external.ts +++ b/packages/cross-import/tests/external.ts @@ -1,3 +1,3 @@ -import fs from 'fs' +import MasterCSS from '@master/css' -export default fs \ No newline at end of file +export default MasterCSS \ No newline at end of file diff --git a/packages/cross-import/tests/real.mjs b/packages/cross-import/tests/real.mjs index 766ed97..1b6c070 100644 --- a/packages/cross-import/tests/real.mjs +++ b/packages/cross-import/tests/real.mjs @@ -1,6 +1,6 @@ import crossImport from '../dist/index.mjs' import fs from 'fs' -import path from 'path' +import path, { resolve } from 'path' import { fileURLToPath } from 'url' const __filename = fileURLToPath(import.meta.url) @@ -17,6 +17,11 @@ if (module1.a !== 0) { throw new Error(`crossImport(${module1}) module1.a !== 0`) } +const externalModule = crossImport(resolve(__dirname, 'external.ts')) +if (!externalModule.default) { + throw new Error(`crossImport(${module1}) external is not defined`) +} + // fs.writeFileSync(configPath, 'module.exports = { b: 0 }') // const module2 = crossImport(configPath) // if (module2.b !== 0) { diff --git a/packages/cross-import/tests/test.ts b/packages/cross-import/tests/test.ts index 46b86fe..678f6ce 100644 --- a/packages/cross-import/tests/test.ts +++ b/packages/cross-import/tests/test.ts @@ -38,13 +38,6 @@ it('read module with export .css.ts', () => { }) }) -it('read module with third-party deps', () => { - expect( - crossImport(path.resolve(__dirname, 'external.ts')).default - ) - .toBeDefined() -}) - it('read non-existent file', () => { expect(() => { crossImport(path.resolve(__dirname, 'idontexist.ts')) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a281f75..50873d2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -130,6 +130,9 @@ importers: specifier: ^3.35.0 version: 3.35.0 devDependencies: + '@master/css': + specifier: 2.0.0-rc.24 + version: 2.0.0-rc.24 pretty-bytes: specifier: ^6.1.0 version: 6.1.1 @@ -1466,6 +1469,23 @@ packages: '@jridgewell/sourcemap-codec': 1.4.15 dev: false + /@master/colors@2.0.0-rc.2: + resolution: {integrity: sha512-0vbicbXvHvO7spAvEL4AMAvL+CUr6NTaufng8Oglh+ISsgtXfhkB82n3mS6kNgmwzgf8bOKMkNJRIDotjSMoqg==} + dev: true + + /@master/css@2.0.0-rc.24: + resolution: {integrity: sha512-whrgo1x1Yhw39CagySPOfSVHLglxaGguSeVCUgTqvw6DH3J0UNpPaMsu7hnhZHnVrrJ9g4rkucFt95w2OdZ6hQ==} + hasBin: true + dependencies: + '@master/colors': 2.0.0-rc.2 + '@master/normal.css': 2.0.0 + '@techor/extend': 3.0.17 + dev: true + + /@master/normal.css@2.0.0: + resolution: {integrity: sha512-zXvZwFTP9gvinLH06ElOlcPJRm7cgFVVxoL/uzFb5qiiXZq1TgsjYS57dEHoPirCWZnk987Xwvi9GQJ4AAgzxA==} + dev: true + /@nodelib/fs.scandir@2.1.5: resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} engines: {node: '>= 8'} @@ -2080,6 +2100,10 @@ packages: resolution: {integrity: sha512-myfUej5naTBWnqOCc/MdVOLVjXUXtIA+NpDrDBKJtLLg2shUjBu3cZmB/85RyitKc55+lUUyl7oRfLOvkr2hsw==} dev: false + /@techor/extend@3.0.17: + resolution: {integrity: sha512-i/QXjTDpsKRv/FsYXDbB41BfbKEw1DESHY3NVzRyvZgBmMavqbP+Gw87UahHMH/z6eju1zv6WJ1nPJMRvTU8yQ==} + dev: true + /@tootallnate/once@2.0.0: resolution: {integrity: sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==} engines: {node: '>= 10'}