我的完整webpack.config.js粘贴在下面以供参考。
如果我清除app目录中的所有*.js
文件,则webpack会抱怨以下错误:
../main.ts中的错误 找不到模块:错误:无法解析'C:\ Projects \ Github \ Panda \ Panda.CoreService \ wwwroot \ ts'中的'./app/app.module' @ ../main.ts 5:19-46 @ multi ../ main.ts
它正在查找正确的位置,错误中的相对路径看起来都是正确的。
如果另一方面,我执行tsc
,然后执行 webpack
,模块都可以找到并构建,没有任何问题。这几乎就像webpack甚至根本没有编译打字稿一样。我试过回滚,但似乎无法解决它。
目录结构
tsconfig.json
wwwroot\ts\main.ts
wwwroot\ts\app\app.module.ts
wwwroot\ts\webpack\webpack.config.js
webpack.config.js
const path = require("path");
const webpack = require("webpack");
const extractTextPlugin = require("extract-text-webpack-plugin");
const config = {
wwwroot: "../../../wwwroot",
css: "../../../wwwroot/css",
node: "../../../node_modules",
admin: "../../../wwwroot/sass/src/"
};
config.scssInclude = [
path.resolve(__dirname, config.node),
path.resolve(__dirname, "../../../wwwroot")
];
console.log("------------------------------------------------------------");
console.log(config);
console.log("------------------------------------------------------------");
const configuration = {
context: __dirname,
entry: {
"application": ["../main.ts"],
"vendor": [
"@angular/animations",
"@angular/common",
"@angular/common/http",
"@angular/compiler",
"@angular/core",
"@angular/forms",
"@angular/http",
"@angular/platform-browser",
"@angular/platform-browser-dynamic",
"@angular/router",
"ngx-logger",
"jquery",
"jquery-validation",
"jquery-validation-unobtrusive",
"bluebird",
"moment",
"ramda",
"lodash",
"reflect-metadata",
"zone.js",
"ng2-charts/ng2-charts",
"ngx-bootstrap/dropdown",
"ngx-bootstrap/tabs"
],
"style": ["../../sass/src/application.scss"]
//"site": ["../site/preload.js"],
//"bootstrap": ["../../sass/bootstrap.scss"],
//"print": ["../../sass/admin/print.scss"]
},
output: {
path: path.join(__dirname, "../"),
filename: "[name].js",
chunkFilename: "[name].js",
publicPath: "ts/"
},
devtool: "source-map",
plugins: [
new webpack.LoaderOptionsPlugin({
debug: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
filename: "vendor.js"
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"window.jquery": "jquery",
//Promise: "bluebird",
_: "lodash"
}),
new extractTextPlugin({
// disable: false,
disable: process.env.NODE_ENV != "production",
filename: "../../wwwroot/css/[name].css",
allChunks: true
})
],
resolve:
{
alias: {
typeahead: "typeahead.js"
}
},
module: {
rules: [
{
test: /\.ts$/,
use: ["ts-loader"],
exclude: /(node_modules)/
},
{
test: /\.scss|\.css$/,
use: extractTextPlugin.extract({
use: [
{
loader: "css-loader",
options: {
includePaths: config.scssInclude,
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
sourceMap: true,
sourceMapContents: true,
precision: 10,
includePaths: config.scssInclude,
outputStyle: "nested"
}
}
],
fallback: "style-loader"
})
},
{
test: /\.(eot|ttf|woff|woff2|svg)$/,
exclude: [/wwwroot(\\|\/)fonts/,/wwwroot(\\|\/)images/],
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "../fonts/",
publicPath: "/"
}
},
{
test: /\.(eot|ttf|woff|woff2|svg)$/,
include: /wwwroot(\\|\/)fonts/,
loader: "file-loader",
options: {
emitFile: false,
name: "[name].[ext]",
outputPath: "../fonts/",
publicPath: "/"
}
},
{
test: /\.(svg|png|jpeg|gif)$/,
include: /wwwroot(\\|\/)images/,
exclude: /node_modules/,
loader: "file-loader",
options: {
emitFile: false,
name: "[name].[ext]",
publicPath: "../images/"
}
},
{
test: /\.(svg|png|jpeg|gif)$/,
include: /sass(\\|\/)img/,
exclude: /node_modules/,
loader: "file-loader",
options: {
emitFile: true,
name: "[name].[ext]",
outputPath: "../images/template/",
publicPath: "/"
}
},
{
test: require.resolve("jquery"),
use: [
{ loader: "expose-loader", options: "jQuery" },
{ loader: "expose-loader", options: "$" }
]
}
]
}
};
console.log("------------------------------------------------------------");
console.log(configuration.module.rules);
console.log("------------------------------------------------------------");
module.exports = configuration;
main.ts
import { enableProdMode } from "@angular/core";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./app/app.module";
import { environment } from "./environments/environment";
/* additional scripts */
import "script-loader!./lib/jquery-resizable.js"
import "script-loader!./lib/tagsuggest.js"
import "./lib/tagsuggest.scss"
import "../fonts/proxima-nova.css";
import "../fonts/roboto.css";
// font awesome
import "font-awesome/scss/font-awesome.scss";
// simple line icons.
import "simple-line-icons/scss/simple-line-icons.scss";
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
答案 0 :(得分:1)
默认情况下,webpack根本无法识别*.ts
个文件。
我找到了解决方案here。
webpack实际上只是在编译时捆绑js
个文件,因为这是默认行为。
我需要在解析部分中加入*.ts.
。
resolve:
{
extensions: [".ts", ".js", ".json"],
},