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