craco-alias
Advanced tools
Comparing version 1.0.5 to 2.0.0
{ | ||
"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"] | ||
} | ||
} | ||
} |
{ | ||
"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"] | ||
} | ||
} | ||
} |
{ | ||
"name": "craco-alias", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "A craco plugin for automatic aliases generation", | ||
@@ -5,0 +5,0 @@ "main": "plugin/index.js", |
@@ -6,3 +6,3 @@ const fs = require('fs') | ||
const extractAliasesFromConfig = ({ configPath, appPath }) => { | ||
const extractAliasesFromConfig = ({ configPath, absoluteBaseUrl }) => { | ||
const configFileContents = fs.readFileSync(configPath) | ||
@@ -21,3 +21,3 @@ const config = JSON.parse(configFileContents) | ||
return normalizeAliases({ | ||
basePath: path.join(appPath, compilerOptions.baseUrl), | ||
absoluteBaseUrl, | ||
aliases: standardAliases | ||
@@ -31,7 +31,10 @@ }) | ||
const { appPath } = paths | ||
const { baseUrl } = options | ||
const absoluteBaseUrl = path.join(appPath, baseUrl) | ||
if (options.source === 'jsconfig') | ||
return extractAliasesFromConfig({ | ||
configPath: paths.appJsConfig, | ||
appPath | ||
absoluteBaseUrl | ||
}) | ||
@@ -42,3 +45,3 @@ | ||
configPath: options.tsConfigPath, | ||
appPath | ||
absoluteBaseUrl | ||
}) | ||
@@ -48,3 +51,3 @@ | ||
return normalizeAliases({ | ||
basePath: appPath, | ||
absoluteBaseUrl, | ||
aliases: options.aliases | ||
@@ -51,0 +54,0 @@ }) |
@@ -25,3 +25,5 @@ const path = require('path') | ||
'@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' | ||
} | ||
@@ -51,3 +53,5 @@ }, | ||
'@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') | ||
} | ||
@@ -54,0 +58,0 @@ |
const path = require('path') | ||
const normalizeAliases = ({ basePath, aliases }) => { | ||
const normalizeAliases = ({ absoluteBaseUrl, aliases }) => { | ||
const result = {} | ||
@@ -11,3 +11,3 @@ | ||
// make alias path absolute | ||
result[aliasName] = path.join(basePath, cleanAlias) | ||
result[aliasName] = path.join(absoluteBaseUrl, cleanAlias) | ||
} | ||
@@ -14,0 +14,0 @@ |
@@ -11,3 +11,3 @@ const path = require('path') | ||
normalizeAliases({ | ||
basePath: appPath, | ||
absoluteBaseUrl: path.join(appPath, '.'), | ||
aliases: { | ||
@@ -18,3 +18,5 @@ '@file': './src/file.js', | ||
'@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' | ||
} | ||
@@ -27,2 +29,23 @@ }) | ||
'@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({ | ||
'@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') | ||
@@ -29,0 +52,0 @@ }) |
@@ -7,2 +7,3 @@ const exitWithError = require('./exit-with-error') | ||
* @property {'jsconfig' | 'tsconfig' | 'options'} source | ||
* @property {string} baseUrl | ||
* @property {Object.<string, string>} aliases | ||
@@ -20,10 +21,17 @@ * @property {string} [tsConfigPath] | ||
source: 'options', | ||
baseUrl: './', | ||
aliases: {} | ||
} | ||
const { source = 'options', tsConfigPath, aliases = {} } = originalOptions | ||
const { | ||
source = 'options', | ||
baseUrl = './', | ||
tsConfigPath, | ||
aliases = {} | ||
} = originalOptions | ||
if (source === 'jsconfig') | ||
return { | ||
source | ||
source, | ||
baseUrl | ||
} | ||
@@ -34,2 +42,3 @@ | ||
source, | ||
baseUrl, | ||
tsConfigPath | ||
@@ -40,2 +49,3 @@ } | ||
source, | ||
baseUrl, | ||
aliases | ||
@@ -42,0 +52,0 @@ } |
@@ -7,2 +7,3 @@ const normalize = require('./normalize-plugin-options') | ||
source: 'options', | ||
baseUrl: './', | ||
aliases: {} | ||
@@ -13,2 +14,3 @@ }) | ||
source: 'options', | ||
baseUrl: './', | ||
aliases: {} | ||
@@ -22,6 +24,8 @@ }) | ||
source: 'jsconfig', | ||
baseUrl: './', | ||
aliases: {} | ||
}) | ||
).toEqual({ | ||
source: 'jsconfig' | ||
source: 'jsconfig', | ||
baseUrl: './' | ||
}) | ||
@@ -39,2 +43,3 @@ }) | ||
source: 'tsconfig', | ||
baseUrl: './', | ||
tsConfigPath: 'tsconfig.paths.json' | ||
@@ -48,4 +53,5 @@ }) | ||
source: 'options', | ||
baseUrl: './src', | ||
aliases: { | ||
'@file': 'src/file.js' | ||
'@file': './file.js' | ||
} | ||
@@ -55,4 +61,5 @@ }) | ||
source: 'options', | ||
baseUrl: './src', | ||
aliases: { | ||
'@file': 'src/file.js' | ||
'@file': './file.js' | ||
} | ||
@@ -59,0 +66,0 @@ }) |
@@ -19,7 +19,2 @@ const checkConfigContents = ({ | ||
if (!config.compilerOptions.baseUrl) | ||
return handleError( | ||
`Property "compilerOptions.baseUrl" is missing in ${configFileName}` | ||
) | ||
if (!config.compilerOptions.paths) | ||
@@ -26,0 +21,0 @@ return handleError( |
@@ -38,10 +38,2 @@ const check = require('./check-config-contents') | ||
handyMockedCheck({ | ||
compilerOptions: {} | ||
}) | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
`Property "compilerOptions.baseUrl" is missing in ${configFileName}` | ||
) | ||
handyMockedCheck({ | ||
compilerOptions: { | ||
@@ -48,0 +40,0 @@ baseUrl: 'src' |
const normalizePluginOptions = require('../normalize-plugin-options') | ||
const checkOptions = ({ pluginOptions, handleError }) => { | ||
if (pluginOptions && typeof pluginOptions !== 'object') | ||
return handleError('You have provided an invalid options') | ||
if (typeof pluginOptions === 'undefined') { | ||
return handleError('Plugin options should be specified') | ||
} | ||
if (typeof pluginOptions !== 'object') { | ||
return handleError('Plugin options should be an object') | ||
} | ||
const options = normalizePluginOptions(pluginOptions) | ||
@@ -31,6 +36,11 @@ | ||
if (options.source === 'options') { | ||
if (typeof options.baseUrl !== 'string') { | ||
return handleError( | ||
'The "baseUrl" option should be a string' | ||
) | ||
} | ||
if (typeof options.aliases !== 'object' || options.aliases === null) | ||
return handleError( | ||
'The "source" option is set to "options",' + | ||
' but you have provided an invalid aliases' | ||
'The "aliases" option should be an object' | ||
) | ||
@@ -37,0 +47,0 @@ } |
@@ -15,2 +15,10 @@ const path = require('path') | ||
mockedCheck({ | ||
pluginOptions: undefined | ||
}) | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
'Plugin options should be specified' | ||
) | ||
mockedCheck({ | ||
pluginOptions: 123 | ||
@@ -20,3 +28,3 @@ }) | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
'You have provided an invalid options' | ||
'Plugin options should be an object' | ||
) | ||
@@ -32,5 +40,11 @@ }) | ||
const availableSources = ['jsconfig', 'tsconfig', 'options'] | ||
const availableSourcesString = availableSources | ||
.map(s => `"${s}"`) | ||
.join(', ') | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
'You have provided an invalid aliases source.' + | ||
' Available sources are: "jsconfig", "tsconfig", "options"' | ||
` Available sources are: ${availableSourcesString}` | ||
) | ||
@@ -52,2 +66,15 @@ }) | ||
test('should check "baseUrl" when source is "options"', () => { | ||
mockedCheck({ | ||
pluginOptions: { | ||
source: 'options', | ||
baseUrl: 345345 | ||
} | ||
}) | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
'The "baseUrl" option should be a string' | ||
) | ||
}) | ||
test('should check "aliases" when source is "options"', () => { | ||
@@ -62,6 +89,5 @@ mockedCheck({ | ||
expect(handleErrorMock).toHaveBeenLastCalledWith( | ||
'The "source" option is set to "options",' + | ||
' but you have provided an invalid aliases' | ||
'The "aliases" option should be an object' | ||
) | ||
}) | ||
}) |
# craco-alias | ||
[![npm](https://img.shields.io/npm/v/craco-alias.svg)](https://www.npmjs.com/package/craco-alias) | ||
[![Travis (.com)](https://img.shields.io/travis/com/risenforces/craco-alias)](https://travis-ci.com/risenforces/craco-alias) | ||
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 | ||
@@ -18,3 +15,3 @@ | ||
1. Install [craco](https://github.com/sharegate/craco) | ||
1. Install [craco](https://github.com/gsoft-inc/craco/blob/master/packages/craco/README.md#installation) | ||
@@ -37,4 +34,3 @@ 2. Install `craco-alias`: | ||
options: { | ||
// craco-alias options | ||
// please see below | ||
// see in examples section | ||
} | ||
@@ -54,2 +50,6 @@ } | ||
- `baseUrl`: | ||
A base url for aliases. (`./src` for example) | ||
Defaults to `./` (project root directory) | ||
- `aliases`: | ||
@@ -81,5 +81,8 @@ An object with aliases names and paths | ||
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" | ||
} | ||
@@ -107,3 +110,6 @@ } | ||
options: { | ||
source: "jsconfig" | ||
source: "jsconfig", | ||
// baseUrl SHOULD be specified | ||
// plugin does not take it from jsconfig | ||
baseUrl: "./src" | ||
} | ||
@@ -115,3 +121,3 @@ } | ||
> 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. | ||
@@ -146,2 +152,3 @@ ```js | ||
"compilerOptions": { | ||
// baseUrl is optional for plugin, but some IDEs require it | ||
"baseUrl": "src", | ||
@@ -181,2 +188,5 @@ "paths": { | ||
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 | ||
@@ -183,0 +193,0 @@ tsConfigPath: "./tsconfig.extend.json" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25932
27
607
193