Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
add support for packages aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
risenforces committed Jan 8, 2020
1 parent c81ebc9 commit 015e434
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 57 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

A [craco](https://github.com/sharegate/craco) plugin for automatic aliases generation for Webpack and Jest.

> :warning: **The plugin does not fully support a module alises**
## List of Contents

- [Installation](#installation)
Expand All @@ -32,8 +30,7 @@ A [craco](https://github.com/sharegate/craco) plugin for automatic aliases gener
{
plugin: CracoAlias,
options: {
// craco-alias options
// please see below
// see in examples section
}
}
]
Expand All @@ -48,6 +45,10 @@ A [craco](https://github.com/sharegate/craco) plugin for automatic aliases gener
One of `"options"`, `"jsconfig"`, `"tsconfig"`
Defaults to `"options"`

- `baseUrl`:
A base url for aliases. (`./src` for example)
Defaults to `./` (project root directory)

- `aliases`:
An object with aliases names and paths
Defaults to `{}`
Expand All @@ -74,9 +75,12 @@ module.exports = {
plugin: CracoAlias,
options: {
source: "options",
baseUrl: "./",
aliases: {
"@file": "src/file.js",
"@dir": "src/some/dir"
"@file": "./src/file.js",
"@dir": "./src/some/dir",
// you can alias packages too
"@material-ui": "./node_modules/@material-ui-ie10"
}
}
}
Expand All @@ -99,14 +103,17 @@ module.exports = {
{
plugin: CracoAlias,
options: {
source: "jsconfig"
source: "jsconfig",
// baseUrl SHOULD be specified
// plugin does not take it from jsconfig
baseUrl: "./src"
}
}
]
};
```

> Note: your jsconfig should always have baseUrl and paths properties
> **Note:** your jsconfig should always have `compilerOptions.paths` property. `baseUrl` is optional for plugin, but some IDEs and editors require it for intellisense.
```js
/* jsconfig.json */
Expand Down Expand Up @@ -136,6 +143,7 @@ module.exports = {
```js
{
"compilerOptions": {
// baseUrl is optional for plugin, but some IDEs require it
"baseUrl": "src",
"paths": {
"@file-alias": ["./your/file.tsx"],
Expand Down Expand Up @@ -170,6 +178,9 @@ module.exports = {
plugin: CracoAlias,
options: {
source: "tsconfig",
// baseUrl SHOULD be specified
// plugin does not take it from tsconfig
baseUrl: "./src",
// tsConfigPath should point to the file where "baseUrl" and "paths" are specified
tsConfigPath: "./tsconfig.extend.json"
}
Expand Down
13 changes: 7 additions & 6 deletions mocks/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@file": ["./file.js"],
"@file2": ["file2.js"],
"@dir/*": ["./dir/*"],
"@dir2/*": ["././dir2/*"],
"$dir3/*": ["dir3/*", "dir3"]
"@file": ["./src/file.js"],
"@file2": ["src/file2.js"],
"@dir/*": ["./src/dir/*"],
"@dir2/*": ["././src/dir2/*"],
"$dir3/*": ["src/dir3/*", "src/dir3"],
"my-package": ["./node_modules/some-package", "./node_modules/some-package/*"],
"@material-ui": ["node_modules/@material-ui/ie-10/ie-10.js"]
}
}
}
13 changes: 7 additions & 6 deletions mocks/tsconfig.paths.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@file": ["./file.js"],
"@file2": ["file2.js"],
"@dir/*": ["./dir/*"],
"@dir2/*": ["././dir2/*"],
"$dir3/*": ["dir3/*", "dir3"]
"@file": ["./src/file.js"],
"@file2": ["src/file2.js"],
"@dir/*": ["./src/dir/*"],
"@dir2/*": ["././src/dir2/*"],
"$dir3/*": ["src/dir3/*", "src/dir3"],
"my-package": ["./node_modules/some-package/*", "./node_modules/some-package"],
"@material-ui": ["node_modules/@material-ui/ie-10/ie-10.js"]
}
}
}
13 changes: 8 additions & 5 deletions plugin/extract-aliases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const normalizePluginOptions = require('../normalize-plugin-options')
const normalizeAliases = require('./normalize-aliases')

const extractAliasesFromConfig = ({ configPath, appPath }) => {
const extractAliasesFromConfig = ({ configPath, absoluteBaseUrl }) => {
const configFileContents = fs.readFileSync(configPath)
const config = JSON.parse(configFileContents)

Expand All @@ -17,7 +17,7 @@ const extractAliasesFromConfig = ({ configPath, appPath }) => {
}

return normalizeAliases({
basePath: path.join(appPath, compilerOptions.baseUrl),
absoluteBaseUrl,
aliases: standardAliases
})
}
Expand All @@ -26,22 +26,25 @@ const extractAliases = ({ pluginOptions, context: { paths } }) => {
const options = normalizePluginOptions(pluginOptions)

const { appPath } = paths
const { baseUrl } = options

const absoluteBaseUrl = path.join(appPath, baseUrl)

if (options.source === 'jsconfig')
return extractAliasesFromConfig({
configPath: paths.appJsConfig,
appPath
absoluteBaseUrl
})

if (options.source === 'tsconfig')
return extractAliasesFromConfig({
configPath: options.tsConfigPath,
appPath
absoluteBaseUrl
})

if (options.source === 'options')
return normalizeAliases({
basePath: appPath,
absoluteBaseUrl,
aliases: options.aliases
})
}
Expand Down
8 changes: 6 additions & 2 deletions plugin/extract-aliases/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('extract-aliases', () => {
'@file2': 'src/file2.js',
'@dir': './src/dir',
'@dir2': '././src/dir2/',
$dir3: 'src/dir3'
$dir3: 'src/dir3',
'my-package': './node_modules/some-package',
'@material-ui': 'node_modules/@material-ui/ie-10/ie-10.js'
}
},
context
Expand All @@ -47,7 +49,9 @@ describe('extract-aliases', () => {
'@file2': path.join(appPath, './src/file2.js'),
'@dir': path.join(appPath, './src/dir'),
'@dir2': path.join(appPath, './src/dir2'),
$dir3: path.join(appPath, './src/dir3')
$dir3: path.join(appPath, './src/dir3'),
'my-package': path.join(appPath, './node_modules/some-package'),
'@material-ui': path.join(appPath, './node_modules/@material-ui/ie-10/ie-10.js')
}

test('should correctly extract aliases from options', () => {
Expand Down
4 changes: 2 additions & 2 deletions plugin/extract-aliases/normalize-aliases.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const path = require('path')

const normalizeAliases = ({ basePath, aliases }) => {
const normalizeAliases = ({ absoluteBaseUrl, aliases }) => {
const result = {}

for (let aliasName in aliases) {
// remove trailing slash
const cleanAlias = aliases[aliasName].replace(/\/$/, '')

// make alias path absolute
result[aliasName] = path.join(basePath, cleanAlias)
result[aliasName] = path.join(absoluteBaseUrl, cleanAlias)
}

return result
Expand Down
27 changes: 25 additions & 2 deletions plugin/extract-aliases/normalize-aliases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,36 @@ describe('normalize-aliases', () => {
test('should correctly normalize aliases', () => {
expect(
normalizeAliases({
basePath: appPath,
absoluteBaseUrl: path.join(appPath, '.'),
aliases: {
'@file': './src/file.js',
'@file2': 'src/file2.js',
'@dir': './src/dir',
'@dir2': '././src/dir2/',
$dir3: 'src/dir3'
$dir3: 'src/dir3',
'my-package': './node_modules/some-package',
'@material-ui': 'node_modules/@material-ui/ie-10/ie-10.js'
}
})
).toEqual({
'@file': path.join(appPath, './src/file.js'),
'@file2': path.join(appPath, './src/file2.js'),
'@dir': path.join(appPath, './src/dir'),
'@dir2': path.join(appPath, './src/dir2'),
$dir3: path.join(appPath, './src/dir3'),
'my-package': path.join(appPath, './node_modules/some-package'),
'@material-ui': path.join(appPath, './node_modules/@material-ui/ie-10/ie-10.js')
})

expect(
normalizeAliases({
absoluteBaseUrl: path.join(appPath, './src'),
aliases: {
'@file': './file.js',
'@file2': 'file2.js',
'@dir': './dir',
'@dir2': '././dir2/',
$dir3: 'dir3'
}
})
).toEqual({
Expand Down
14 changes: 12 additions & 2 deletions plugin/normalize-plugin-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const exitWithError = require('./exit-with-error')
* @typedef NormalizedConfig
* @type {object}
* @property {'jsconfig' | 'tsconfig' | 'options'} source
* @property {string} baseUrl
* @property {Object.<string, string>} aliases
* @property {string} [tsConfigPath]
*/
Expand All @@ -16,24 +17,33 @@ const normalizePluginOptions = originalOptions => {
if (!originalOptions)
return {
source: 'options',
baseUrl: './',
aliases: {}
}

const { source = 'options', tsConfigPath, aliases = {} } = originalOptions
const {
source = 'options',
baseUrl = './',
tsConfigPath,
aliases = {}
} = originalOptions

if (source === 'jsconfig')
return {
source
source,
baseUrl
}

if (source === 'tsconfig')
return {
source,
baseUrl,
tsConfigPath
}

return {
source,
baseUrl,
aliases
}
}
Expand Down
13 changes: 10 additions & 3 deletions plugin/normalize-plugin-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ describe('normalize-plugin-options', () => {
test('should return default config', () => {
expect(normalize(undefined)).toEqual({
source: 'options',
baseUrl: './',
aliases: {}
})

expect(normalize({})).toEqual({
source: 'options',
baseUrl: './',
aliases: {}
})
})
Expand All @@ -17,10 +19,12 @@ describe('normalize-plugin-options', () => {
expect(
normalize({
source: 'jsconfig',
baseUrl: './',
aliases: {}
})
).toEqual({
source: 'jsconfig'
source: 'jsconfig',
baseUrl: './'
})
})

Expand All @@ -33,6 +37,7 @@ describe('normalize-plugin-options', () => {
})
).toEqual({
source: 'tsconfig',
baseUrl: './',
tsConfigPath: 'tsconfig.paths.json'
})
})
Expand All @@ -41,14 +46,16 @@ describe('normalize-plugin-options', () => {
expect(
normalize({
source: 'options',
baseUrl: './src',
aliases: {
'@file': 'src/file.js'
'@file': './file.js'
}
})
).toEqual({
source: 'options',
baseUrl: './src',
aliases: {
'@file': 'src/file.js'
'@file': './file.js'
}
})
})
Expand Down
5 changes: 0 additions & 5 deletions plugin/pre-check/check-config-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const checkConfigContents = ({
`Property "compilerOptions" is missing in ${configFileName}`
)

if (!config.compilerOptions.baseUrl)
return handleError(
`Property "compilerOptions.baseUrl" is missing in ${configFileName}`
)

if (!config.compilerOptions.paths)
return handleError(
`Property "compilerOptions.paths" is missing in ${configFileName}`
Expand Down
8 changes: 0 additions & 8 deletions plugin/pre-check/check-config-contents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ describe('check-config-contents', () => {
`Property "compilerOptions" is missing in ${configFileName}`
)

handyMockedCheck({
compilerOptions: {}
})

expect(handleErrorMock).toHaveBeenLastCalledWith(
`Property "compilerOptions.baseUrl" is missing in ${configFileName}`
)

handyMockedCheck({
compilerOptions: {
baseUrl: 'src'
Expand Down
Loading

0 comments on commit 015e434

Please sign in to comment.