From 8576b4fcfdf0ab0a500b231907e3f171c5d34d82 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 6 May 2019 18:29:37 +0430 Subject: [PATCH] feat: configurable aliases (#5655) --- packages/config/src/config/_common.js | 1 + packages/config/src/options.js | 12 ++++++++++++ .../config/test/__snapshots__/options.test.js.snap | 8 ++++++++ .../test/config/__snapshots__/index.test.js.snap | 2 ++ packages/config/test/options.test.js | 2 ++ packages/webpack/src/config/base.js | 11 +---------- packages/webpack/src/utils/postcss.js | 9 ++------- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/config/src/config/_common.js b/packages/config/src/config/_common.js index 68988c4bd7..054fafd9c3 100644 --- a/packages/config/src/config/_common.js +++ b/packages/config/src/config/_common.js @@ -43,6 +43,7 @@ export default () => ({ }, extensions: [], styleExtensions: ['css', 'pcss', 'postcss', 'styl', 'stylus', 'scss', 'sass', 'less'], + alias: {}, // Ignores ignorePrefix: '-', diff --git a/packages/config/src/options.js b/packages/config/src/options.js index 7563be0d52..67df5c6664 100644 --- a/packages/config/src/options.js +++ b/packages/config/src/options.js @@ -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`) diff --git a/packages/config/test/__snapshots__/options.test.js.snap b/packages/config/test/__snapshots__/options.test.js.snap index e7bfc85efa..b35b63ec58 100644 --- a/packages/config/test/__snapshots__/options.test.js.snap +++ b/packages/config/test/__snapshots__/options.test.js.snap @@ -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/", diff --git a/packages/config/test/config/__snapshots__/index.test.js.snap b/packages/config/test/config/__snapshots__/index.test.js.snap index 1070f24265..85d4eecb79 100644 --- a/packages/config/test/config/__snapshots__/index.test.js.snap +++ b/packages/config/test/config/__snapshots__/index.test.js.snap @@ -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 { diff --git a/packages/config/test/options.test.js b/packages/config/test/options.test.js index 41cdef0d94..d89d5ff054 100644 --- a/packages/config/test/options.test.js +++ b/packages/config/test/options.test.js @@ -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', () => { diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js index f21ca23e44..4c6bffc790 100644 --- a/packages/webpack/src/config/base.js +++ b/packages/webpack/src/config/base.js @@ -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() { diff --git a/packages/webpack/src/utils/postcss.js b/packages/webpack/src/utils/postcss.js index 5b75b1705e..130761fcb6 100644 --- a/packages/webpack/src/utils/postcss.js +++ b/packages/webpack/src/utils/postcss.js @@ -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 ] }) },