【发布时间】:2019-10-20 07:47:23
【问题描述】:
我正在使用带有 typescript 的 webpack,其配置在 typescript 中。指令here
在我的根目录下我有webpack.config.base.ts
import webpack from "webpack";
const getConfig = ( appDir: string, distDir: string, env: any, args: any ): webpack.Configuration => {
const config: webpack.Configuration = {
entry: {
},
output: {
},
resolve: {
plugins: [
],
extensions: [".ts", ".tsx", ".js"]
},
devtool: "source-map",
module: {
rules: [
]
},
plugins: [
]
};
return config;
};
module.exports = getConfig;
然后我有两个项目,每个项目都有自己的webpack.config.ts
import webpack from "webpack";
import * as path from 'path';
const getConfig = require("../../webpack.config.base");
const appDir = __dirname;
const distDir = path.resolve(__dirname, "../AppOne.Web/wwwroot");
const getConfigFactory = (env: any, args: any): webpack.Configuration => getConfig(appDir, distDir, env, args);
module.exports = getConfigFactory;
这一切都很好。 Here's这个工厂getConfig = () => {}风格的完整示例。
我的问题是当我尝试改变来改变时
const getConfig = require("../../webpack.config.base");
到 es6 导入。这甚至是 VS 代码作为建议提供的。
当我应用此更改时,我得到
这是我的tsconfig.json 我已经启用了[allowSyntheticDefaultImports][5]。建议here。
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"moduleResolution": "node",
"jsx": "react",
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"target": "es5",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"typeRoots": [
"./node_modules/@types/",
"./types/"
],
"baseUrl": ".",
"paths": { }
},
"include": [
"./src/**/*.ts",
"./src/**/*.tsx"
]
}
但我还是添加了export default getConfig;……然后又添加了npm run build。它仍然失败。
const getConfigFactory = (env: any, args: any): webpack.Configuration => getConfig(appDir, distDir, env, args);
^
TypeError: webpack_config_base_1.default is not a function
我在把头撞到桌子之前的最后一次尝试是改变
import getConfig from "../../webpack.config.base";
import * as base from "../../webpack.config.base";
删除webpack.config.base.ts中的export default getConfig;,将const getConfig直接导出为export const getConfig。但那时module.exports = getConfig 的意义何在。更不用说它也没有血腥的工作(和以前一样的问题)
const getConfigFactory = (env: any, args: any): webpack.Configuration => base.getConfig(appDir, distDir, env, args);
^
TypeError: base.getConfig is not a function
我在这里缺少什么?为什么我不能简单地将const getConfig = require("../../webpack.config.base"); 替换为import getConfig from "../../webpack.config.base"
附言。
这是我的"scripts" 用于运行它
"build:appone": "webpack --mode=development --config ./src/AppOne.App/webpack.config.ts",
"build:apptwo": "webpack --mode=development --config ./src/AppTwo.App/webpack.config.ts",
【问题讨论】:
标签: typescript webpack import