Skip to content

Commit

Permalink
add tsconfig paths support for importing vue libraries into nuxt apps
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckyMaler authored and ZachJW34 committed Sep 16, 2020
1 parent 6e7bf40 commit 8c37b2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@nrwl/cypress": "^10.0.0",
"@nrwl/jest": "^10.0.0",
"@nrwl/linter": "^10.0.0",
"rxjs": "6.5.5"
"rxjs": "6.5.5",
"tsconfig-paths-webpack-plugin": "3.2.0"
}
}
15 changes: 15 additions & 0 deletions libs/nuxt/src/builders/browser/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { from, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { BrowserBuilderSchema } from './schema';
import { getProjectRoot } from '../../utils';
import { modifyTypescriptAliases } from '../../webpack';

export function runBuilder(
options: BrowserBuilderSchema,
Expand All @@ -24,6 +25,20 @@ export function runBuilder(
buildDir: getSystemPath(
join(normalize(context.workspaceRoot), options.buildDir)
),
build: {
extend(config, ctx) {
modifyTypescriptAliases(config, projectRoot);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { default: nuxtConfig } = require(getSystemPath(
join(projectRoot, 'nuxt.config.js')
));

if (nuxtConfig.build && nuxtConfig.build.extend) {
nuxtConfig.build.extend(config, ctx);
}
},
},
},
});

Expand Down
15 changes: 15 additions & 0 deletions libs/nuxt/src/builders/server/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { map, switchMap } from 'rxjs/operators';
import { ServerBuilderSchema } from './schema';
import { BrowserBuilderSchema } from '../browser/schema';
import { getProjectRoot } from '../../utils';
import { modifyTypescriptAliases } from '../../webpack';

const serverBuilderOverriddenKeys = [];

Expand Down Expand Up @@ -48,6 +49,20 @@ export function runBuilder(
buildDir: getSystemPath(
join(normalize(context.workspaceRoot), browserOptions.buildDir)
),
build: {
extend(config, ctx) {
modifyTypescriptAliases(config, projectRoot);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { default: nuxtConfig } = require(getSystemPath(
join(projectRoot, 'nuxt.config.js')
));

if (nuxtConfig.build && nuxtConfig.build.extend) {
nuxtConfig.build.extend(config, ctx);
}
},
},
},
});

Expand Down
24 changes: 24 additions & 0 deletions libs/nuxt/src/webpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getSystemPath, join, Path } from '@angular-devkit/core';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function modifyTypescriptAliases(config: any, projectRoot: Path): void {
['~~', '@@', '~', '@', 'assets', 'static'].forEach(
(key) => delete config.resolve.alias[key]
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const options: any = {
configFile: getSystemPath(join(projectRoot, 'tsconfig.json')),
extensions: [...config.resolve.extensions, '.ts', '.tsx'],
};

if (config.resolve.mainFields) {
options.mainFields = config.resolve.mainFields;
}

config.resolve.plugins = [
...(config.resolve.plugins || []),
new TsconfigPathsPlugin(options),
];
}

0 comments on commit 8c37b2c

Please sign in to comment.