From 4783a4029970b2786d57ab004ceda09267004810 Mon Sep 17 00:00:00 2001 From: liximomo Date: Fri, 21 Feb 2020 18:20:18 +0800 Subject: [PATCH] feat: resolve by target --- packages/shuvi/src/webpack/webpackManager.ts | 2 +- packages/toolpack/src/webpack/config/base.ts | 1 + packages/toolpack/src/webpack/config/browser.ts | 16 ++++++++++++++++ packages/toolpack/src/webpack/config/node.ts | 16 ++++++++++++++++ .../toolpack/src/webpack/config/parts/resolve.ts | 8 ++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/toolpack/src/webpack/config/parts/resolve.ts diff --git a/packages/shuvi/src/webpack/webpackManager.ts b/packages/shuvi/src/webpack/webpackManager.ts index 0428edf35..aa843b136 100644 --- a/packages/shuvi/src/webpack/webpackManager.ts +++ b/packages/shuvi/src/webpack/webpackManager.ts @@ -55,7 +55,7 @@ class WebpackManagerImpl { }); clientConfig.entry = getClientEntry(); // console.log("clientConfig"); - // console.dir(clientConfig.module?.rules); + // console.dir(clientConfig.resolve?.extensions); const serverConfig = createWepbackConfig(this._app, { name: WEBPACK_CONFIG_SERVER, diff --git a/packages/toolpack/src/webpack/config/base.ts b/packages/toolpack/src/webpack/config/base.ts index 46c625d4a..3dc2bf74c 100644 --- a/packages/toolpack/src/webpack/config/base.ts +++ b/packages/toolpack/src/webpack/config/base.ts @@ -22,6 +22,7 @@ export interface BaseOptions { srcDirs: string[]; mediaFilename: string; buildManifestFilename: string; + target?: string; publicPath?: string; env?: { [x: string]: string; diff --git a/packages/toolpack/src/webpack/config/browser.ts b/packages/toolpack/src/webpack/config/browser.ts index f1c225a58..4a714596d 100644 --- a/packages/toolpack/src/webpack/config/browser.ts +++ b/packages/toolpack/src/webpack/config/browser.ts @@ -4,6 +4,8 @@ import WebpackChain from "webpack-chain"; // import BuildManifestPlugin from "../plugins/build-manifest-plugin"; import { baseWebpackChain, BaseOptions } from "./base"; import { withStyle } from "./parts/style"; +import { resolvePreferTarget } from "./parts/resolve"; +import { getProjectInfo } from "../../utils/typeScript"; const BIG_LIBRARY_THRESHOLD = 160000; // byte @@ -14,9 +16,23 @@ export function createBrowserWebpackChain({ }: BrowserOptions): WebpackChain { const { dev } = baseOptions; const chain = baseWebpackChain(baseOptions); + const { useTypeScript } = getProjectInfo(baseOptions.projectRoot); chain.target("web"); chain.devtool(dev ? "cheap-module-source-map" : false); + const extensions = [ + ...(useTypeScript ? [".tsx", ".ts"] : []), + ".mjs", + ".js", + ".jsx", + ".json", + ".wasm" + ]; + chain.resolve.extensions.merge( + baseOptions.target + ? resolvePreferTarget(baseOptions.target, extensions) + : extensions + ); if (dev) { chain.plugin("private/hmr-plugin").use(webpack.HotModuleReplacementPlugin); } else { diff --git a/packages/toolpack/src/webpack/config/node.ts b/packages/toolpack/src/webpack/config/node.ts index c24adff2c..929af676e 100644 --- a/packages/toolpack/src/webpack/config/node.ts +++ b/packages/toolpack/src/webpack/config/node.ts @@ -2,6 +2,8 @@ import WebpackChain from "webpack-chain"; import { baseWebpackChain, BaseOptions } from "./base"; import { nodeExternals } from "./parts/external"; import { withStyle } from "./parts/style"; +import { resolvePreferTarget } from "./parts/resolve"; +import { getProjectInfo } from "../../utils/typeScript"; export interface NodeOptions extends BaseOptions {} @@ -9,9 +11,23 @@ export function createNodeWebpackChain({ ...baseOptions }: NodeOptions): WebpackChain { const chain = baseWebpackChain(baseOptions); + const { useTypeScript } = getProjectInfo(baseOptions.projectRoot); chain.target("node"); chain.devtool(false); + const extensions = [ + ...(useTypeScript ? [".tsx", ".ts"] : []), + ".js", + ".mjs", + ".jsx", + ".json", + ".wasm" + ]; + chain.resolve.extensions.merge( + baseOptions.target + ? resolvePreferTarget(baseOptions.target, extensions) + : extensions + ); chain.output.libraryTarget("commonjs2"); chain.optimization.minimize(false); chain.externals(nodeExternals({ projectRoot: baseOptions.projectRoot })); diff --git a/packages/toolpack/src/webpack/config/parts/resolve.ts b/packages/toolpack/src/webpack/config/parts/resolve.ts new file mode 100644 index 000000000..a3a0e3761 --- /dev/null +++ b/packages/toolpack/src/webpack/config/parts/resolve.ts @@ -0,0 +1,8 @@ +export function resolvePreferTarget(target: string, extensions: string[]) { + return extensions.reduce((res, ext) => { + res.push(`.${target}${ext}`); + res.push(ext); + + return res; + }, [] as string[]); +}