Skip to content

Commit

Permalink
Fix(Cross Import): Incorrect module file name and requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Apr 25, 2024
1 parent af07cd8 commit b4ac571
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 76 deletions.
4 changes: 2 additions & 2 deletions packages/cross-import/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
31 changes: 19 additions & 12 deletions packages/cross-import/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { transform } from 'sucrase'
import { Module } from 'module'
import { Transform, transform } from 'sucrase'
import { Module, createRequire } from 'module'
import { readFileSync } from 'fs'
import { runInThisContext } from 'vm'
import { dirname } from 'path'
import { dirname, join, parse } from 'path'

export default function crossImport(modulePath: string): any {
if (!modulePath) return
Expand All @@ -21,20 +21,27 @@ export default function crossImport(modulePath: string): any {
console.error(error)
console.log('[cross-import] fall back to sucrase runtime transform:', modulePath)
}
const { code: moduleCode } = transform(readFileSync(modulePath, 'utf8').toString(), {
transforms: ['imports', 'typescript'],
const parsedModulePath = parse(modulePath)
const sourceCode = readFileSync(modulePath, 'utf8').toString()
const transforms: Transform[] = ['imports']
if (parsedModulePath.ext === '.ts') {
transforms.unshift('typescript')
}
const { code: moduleCode } = transform(sourceCode, {
transforms,
filePath: modulePath
})
const mod = new Module(__filename)
mod.filename = __dirname
mod.require = require
mod.path = dirname(__filename)
const filename = join(parsedModulePath.dir, parsedModulePath.name + '.js')
const mod = new Module(filename, module)
mod.filename = filename
mod.path = dirname(filename)
mod.require = createRequire(filename)
// Compile wrapped script
const compiledModule = runInThisContext(Module.wrap(moduleCode), {
filename: __filename,
lineOffset: 0,
displayErrors: false,
filename
})
compiledModule(mod.exports, mod.require, mod, mod.filename, dirname(mod.filename))
mod.loaded = true
return mod.exports
}
}
5 changes: 5 additions & 0 deletions packages/explore-config/fixtures/card.css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
styles: {
card: 'inline-flex'
}
}
5 changes: 5 additions & 0 deletions packages/explore-config/fixtures/card.css.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
styles: {
card: 'inline-flex'
}
}
7 changes: 7 additions & 0 deletions packages/explore-config/fixtures/card.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Config } from '@master/css'

export default {
styles: {
card: 'inline-flex'
}
} as Config
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
// extends: [
// require('./card.css')
// ],
colors: {
primary: '#ff0'
}
Expand Down
10 changes: 10 additions & 0 deletions packages/explore-config/fixtures/master.css.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import card from './card.css.mjs'

export default {
// extends: [
// card
// ],
colors: {
primary: '#ff0'
}
}
10 changes: 10 additions & 0 deletions packages/explore-config/fixtures/master.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import card from './card.css'

export default {
// extends: [
// card
// ],
colors: {
primary: '#ff0'
}
}
8 changes: 7 additions & 1 deletion packages/explore-config/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/** @type {import('jest').Config} */
export default {
preset: '@techor/jest',
transformIgnorePatterns: ['node_modules/(?!callsites)/']
transform: {
'^.+\\.(t|j)sx?$': [
'@swc/jest'
],
},
transformIgnorePatterns: ['node_modules/(?!callsites)/'],
extensionsToTreatAsEsm: []
}
4 changes: 2 additions & 2 deletions packages/explore-config/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
5 changes: 0 additions & 5 deletions packages/explore-config/tests/master.css.mjs

This file was deleted.

5 changes: 0 additions & 5 deletions packages/explore-config/tests/master.css.ts

This file was deleted.

44 changes: 33 additions & 11 deletions packages/explore-config/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
import exploreConfig from '../src'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

it('read .js config', () => {
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['js'] })
expect(config).toEqual({ colors: { primary: '#ff0' } })
const config = exploreConfig('../fixtures/master.css', { cwd: __dirname, extensions: ['js'] })
expect(config).toEqual({
// extends: [
// {
// styles: {
// card: 'inline-flex'
// }
// }
// ],
colors: { primary: '#ff0' }
})
})

it('read .ts config', () => {
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['ts'] })
expect(config).toEqual({ colors: { primary: '#ff0' } })
const config = exploreConfig('../fixtures/master.css', { cwd: __dirname, extensions: ['ts'] })
expect(config).toEqual({
// extends: [
// {
// styles: {
// card: 'inline-flex'
// }
// }
// ],
colors: { primary: '#ff0' }
})
})

it('read .mjs config', () => {
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['mjs'] })
expect(config).toEqual({ colors: { primary: '#ff0' } })
const config = exploreConfig('../fixtures/master.css', { cwd: __dirname, extensions: ['mjs'] })
expect(config).toEqual({
// extends: [
// {
// styles: {
// card: 'inline-flex'
// }
// }
// ],
colors: { primary: '#ff0' }
})
})

it('cannot read config', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/extend/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/fs/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/glob/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/is-plain-obj/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/log/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/npm/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/semantic-release-config/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commonOptions from '../../rollup.config.mjs'
import createConfig from '../../rollup.config.mjs'

/** @type {import('rollup').RollupOptions} */
export default {
...commonOptions
...createConfig()
}
4 changes: 2 additions & 2 deletions packages/techor/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const config: Config = {
export default config

export interface Config {
build: BuildOptions
version: VersionOptions
build?: BuildOptions
version?: VersionOptions
}

export interface BuildCommonOptions {
Expand Down
48 changes: 26 additions & 22 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,30 @@ const DEFAULT_EXTENSIONS = [
'.json',
]

/** @type {import('rollup').RollupOptions} */
export default {
input: 'src/index.ts',
output: [
{ file: 'dist/index.cjs', format: 'cjs' },
{ file: 'dist/index.mjs', format: 'esm' }
],
plugins: [
swc(),
commonjs({ extensions: ['.js', '.ts'] }),
nodeResolve({
extensions: DEFAULT_EXTENSIONS,
exportConditions: ['node', 'import', 'require', 'default']
}),
esmShim(),
raw('**/*.hbs')
],
external: [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.optionalDependencies || {})
]
export default function defineConfig(options = {}) {
return {
input: 'src/index.ts',
output: [
{ file: 'dist/index.cjs', format: 'cjs' },
{ file: 'dist/index.mjs', format: 'esm' }
],
plugins: [
swc(),
commonjs({
extensions: ['.js', '.ts'],
...options.commonjs
}),
nodeResolve({
extensions: DEFAULT_EXTENSIONS,
exportConditions: ['node', 'import', 'require', 'default']
}),
esmShim(),
raw('**/*.hbs')
],
external: [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.optionalDependencies || {})
]
}
}

0 comments on commit b4ac571

Please sign in to comment.