-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide options to transform module path (#25)
- Loading branch information
1 parent
90ea0c1
commit db59d16
Showing
6 changed files
with
161 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/__tests__/__snapshots__/macro-transform-import.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`macros README:4 configure \`importAll\` to transform import path before generating imports: README:4 configure \`importAll\` to transform import path before generating imports 1`] = ` | ||
import importAll from 'import-all.macro' | ||
const a = importAll.sync('./files/*.js') | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
import * as _src__tests__FixturesA from 'src/__tests__/files/a' | ||
import * as _src__tests__FixturesB from 'src/__tests__/files/b' | ||
import * as _src__tests__FixturesC from 'src/__tests__/files/c' | ||
import * as _src__tests__FixturesD from 'src/__tests__/files/d' | ||
const a = { | ||
'src/__tests__/files/a': _src__tests__FixturesA, | ||
'src/__tests__/files/b': _src__tests__FixturesB, | ||
'src/__tests__/files/c': _src__tests__FixturesC, | ||
'src/__tests__/files/d': _src__tests__FixturesD, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import path from 'path' | ||
import pluginTester from 'babel-plugin-tester' | ||
import prettier from 'prettier' | ||
import {prettier as prettierConfig} from 'kcd-scripts/config' | ||
|
||
const projectRoot = path.join(__dirname, '../../../').replace(/\\/g, '/') | ||
|
||
expect.addSnapshotSerializer({ | ||
print(val) { | ||
return val | ||
.split(projectRoot) | ||
.join('<PROJECT_ROOT>/') | ||
.replace(/fixtures/g, 'files') | ||
.replace(/..\/macro/, 'import-all.macro') | ||
}, | ||
test(val) { | ||
return typeof val === 'string' | ||
}, | ||
}) | ||
|
||
export function createMacroTests(pluginTesterOptions) { | ||
pluginTester({ | ||
snapshot: true, | ||
formatResult(result) { | ||
return prettier.format(result, {...prettierConfig, parser: 'babel'}) | ||
}, | ||
...pluginTesterOptions, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import path from 'path' | ||
import plugin from 'babel-plugin-macros' | ||
|
||
import {createMacroTests} from './helpers/create-macro-test' | ||
|
||
createMacroTests({ | ||
plugin: (babel, options) => { | ||
return plugin(babel, { | ||
importAll: { | ||
transformModulePath(modulePath, importingPath) { | ||
const projectRoot = path.join(__dirname, '../../') | ||
const modulePathWithoutExt = modulePath.replace(/\.js$/, '') | ||
const absolutePath = path.resolve( | ||
path.dirname(importingPath), | ||
modulePathWithoutExt, | ||
) | ||
const pathRelativeToRoot = path.relative(projectRoot, absolutePath) | ||
return pathRelativeToRoot | ||
}, | ||
}, | ||
...options, | ||
}) | ||
}, | ||
babelOptions: { | ||
filename: __filename, | ||
}, | ||
tests: { | ||
'README:4 configure `importAll` to transform import path before generating imports': ` | ||
import importAll from '../macro' | ||
const a = importAll.sync('./fixtures/*.js') | ||
`, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters