mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-12 07:17:13 +00:00
feat: configurable aliases (#5655)
This commit is contained in:
parent
ffadb3177f
commit
8576b4fcfd
@ -43,6 +43,7 @@ export default () => ({
|
|||||||
},
|
},
|
||||||
extensions: [],
|
extensions: [],
|
||||||
styleExtensions: ['css', 'pcss', 'postcss', 'styl', 'stylus', 'scss', 'sass', 'less'],
|
styleExtensions: ['css', 'pcss', 'postcss', 'styl', 'stylus', 'scss', 'sass', 'less'],
|
||||||
|
alias: {},
|
||||||
|
|
||||||
// Ignores
|
// Ignores
|
||||||
ignorePrefix: '-',
|
ignorePrefix: '-',
|
||||||
|
@ -99,6 +99,18 @@ export function getNuxtConfig(_options) {
|
|||||||
// Resolve buildDir
|
// Resolve buildDir
|
||||||
options.buildDir = path.resolve(options.rootDir, options.buildDir)
|
options.buildDir = path.resolve(options.rootDir, options.buildDir)
|
||||||
|
|
||||||
|
// Aliases
|
||||||
|
const { rootDir, srcDir, dir: { assets: assetsDir, static: staticDir } } = options
|
||||||
|
options.alias = {
|
||||||
|
'~~': rootDir,
|
||||||
|
'@@': rootDir,
|
||||||
|
'~': srcDir,
|
||||||
|
'@': srcDir,
|
||||||
|
[assetsDir]: path.join(srcDir, assetsDir),
|
||||||
|
[staticDir]: path.join(srcDir, staticDir),
|
||||||
|
...options.alias
|
||||||
|
}
|
||||||
|
|
||||||
// Default value for _nuxtConfigFile
|
// Default value for _nuxtConfigFile
|
||||||
if (!options._nuxtConfigFile) {
|
if (!options._nuxtConfigFile) {
|
||||||
options._nuxtConfigFile = path.resolve(options.rootDir, `${defaultNuxtConfigFile}.js`)
|
options._nuxtConfigFile = path.resolve(options.rootDir, `${defaultNuxtConfigFile}.js`)
|
||||||
|
@ -8,6 +8,14 @@ Object {
|
|||||||
"_nuxtConfigFiles": Array [
|
"_nuxtConfigFiles": Array [
|
||||||
"/var/nuxt/test/nuxt.config.js",
|
"/var/nuxt/test/nuxt.config.js",
|
||||||
],
|
],
|
||||||
|
"alias": Object {
|
||||||
|
"@": "/var/nuxt/test",
|
||||||
|
"@@": "/var/nuxt/test",
|
||||||
|
"assets": "/var/nuxt/test/assets",
|
||||||
|
"static": "/var/nuxt/test/static",
|
||||||
|
"~": "/var/nuxt/test",
|
||||||
|
"~~": "/var/nuxt/test",
|
||||||
|
},
|
||||||
"appTemplatePath": "/var/nuxt/test/.nuxt/views/app.template.html",
|
"appTemplatePath": "/var/nuxt/test/.nuxt/views/app.template.html",
|
||||||
"build": Object {
|
"build": Object {
|
||||||
"_publicPath": "/_nuxt/",
|
"_publicPath": "/_nuxt/",
|
||||||
|
@ -4,6 +4,7 @@ exports[`config should return default nuxt configurations 1`] = `
|
|||||||
Object {
|
Object {
|
||||||
"ErrorPage": null,
|
"ErrorPage": null,
|
||||||
"_nuxtConfigFile": undefined,
|
"_nuxtConfigFile": undefined,
|
||||||
|
"alias": Object {},
|
||||||
"build": Object {
|
"build": Object {
|
||||||
"analyze": false,
|
"analyze": false,
|
||||||
"babel": Object {
|
"babel": Object {
|
||||||
@ -336,6 +337,7 @@ exports[`config should return nuxt configurations with custom env 1`] = `
|
|||||||
Object {
|
Object {
|
||||||
"ErrorPage": null,
|
"ErrorPage": null,
|
||||||
"_nuxtConfigFile": undefined,
|
"_nuxtConfigFile": undefined,
|
||||||
|
"alias": Object {},
|
||||||
"build": Object {
|
"build": Object {
|
||||||
"analyze": false,
|
"analyze": false,
|
||||||
"babel": Object {
|
"babel": Object {
|
||||||
|
@ -23,11 +23,13 @@ describe('config: options', () => {
|
|||||||
test('should return default nuxt config', () => {
|
test('should return default nuxt config', () => {
|
||||||
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
|
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
|
||||||
jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
|
jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
|
||||||
|
jest.spyOn(path, 'join').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
|
||||||
|
|
||||||
expect(getNuxtConfig({})).toMatchSnapshot()
|
expect(getNuxtConfig({})).toMatchSnapshot()
|
||||||
|
|
||||||
process.cwd.mockRestore()
|
process.cwd.mockRestore()
|
||||||
path.resolve.mockRestore()
|
path.resolve.mockRestore()
|
||||||
|
path.join.mockRestore()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should prevent duplicate calls with same options', () => {
|
test('should prevent duplicate calls with same options', () => {
|
||||||
|
@ -196,16 +196,7 @@ export default class WebpackBaseConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
alias() {
|
alias() {
|
||||||
const { srcDir, rootDir, dir: { assets: assetsDir, static: staticDir } } = this.buildContext.options
|
return { ...this.buildContext.options.alias }
|
||||||
|
|
||||||
return {
|
|
||||||
'~': path.join(srcDir),
|
|
||||||
'~~': path.join(rootDir),
|
|
||||||
'@': path.join(srcDir),
|
|
||||||
'@@': path.join(rootDir),
|
|
||||||
[assetsDir]: path.join(srcDir, assetsDir),
|
|
||||||
[staticDir]: path.join(srcDir, staticDir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rules() {
|
rules() {
|
||||||
|
@ -46,19 +46,14 @@ export default class PostcssConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get defaultConfig() {
|
get defaultConfig() {
|
||||||
const { dev, srcDir, rootDir, modulesDir } = this.buildContext.options
|
const { dev, alias, srcDir, rootDir, modulesDir } = this.buildContext.options
|
||||||
return {
|
return {
|
||||||
sourceMap: this.buildContext.buildOptions.cssSourceMap,
|
sourceMap: this.buildContext.buildOptions.cssSourceMap,
|
||||||
plugins: {
|
plugins: {
|
||||||
// https://github.com/postcss/postcss-import
|
// https://github.com/postcss/postcss-import
|
||||||
'postcss-import': {
|
'postcss-import': {
|
||||||
resolve: createResolver({
|
resolve: createResolver({
|
||||||
alias: {
|
alias: { ...alias },
|
||||||
'~': path.join(srcDir),
|
|
||||||
'~~': path.join(rootDir),
|
|
||||||
'@': path.join(srcDir),
|
|
||||||
'@@': path.join(rootDir)
|
|
||||||
},
|
|
||||||
modules: [ srcDir, rootDir, ...modulesDir ]
|
modules: [ srcDir, rootDir, ...modulesDir ]
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user