Skip to content

Commit

Permalink
Fix(Cross Import): Cannot use import statement outside a module
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Mar 7, 2024
1 parent 04ceb10 commit af07cd8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/cross-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"sucrase": "^3.35.0"
},
"devDependencies": {
"@master/css": "2.0.0-rc.24",
"pretty-bytes": "^6.1.0"
}
}
4 changes: 2 additions & 2 deletions packages/cross-import/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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), {
Expand Down
9 changes: 9 additions & 0 deletions packages/cross-import/tests/external.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
4 changes: 2 additions & 2 deletions packages/cross-import/tests/external.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import fs from 'fs'
import MasterCSS from '@master/css'

export default fs
export default MasterCSS
7 changes: 6 additions & 1 deletion packages/cross-import/tests/real.mjs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down
7 changes: 0 additions & 7 deletions packages/cross-import/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af07cd8

Please sign in to comment.