初始化构建React+Ts项目时出现:Module build failed (from ./node_modules/css-loader/dist/cjs.js): CssSyntaxError

2019/06/27 11:07
阅读数 1.5K

具体错误

ERROR in ./src/index.tsx
Module build failed (from ./node_modules/css-loader/dist/cjs.js):
CssSyntaxError
(2:1) Unknown word
var content = require("!!./index.tsx");
if(typeof content === 'string') content = [[module.id, content, '']]

webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
function resolve(dir) {
  return path.join(__dirname, dir);
}
module.exports = {
  entry: path.join(__dirname, "../src/index.tsx"),
  output: {
    filename: "bundle.js",
    path: path.join(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: ["babel-loader"],
        include: path.join(__dirname, "src")
      },
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: ["ts-loader", "babel-loader", "css-loader", "style-loader"]
      },
      {
        test: /\.(s*)css$/, // 正则匹配文件路径
        exclude: /node_modules/,
        use: ["css-loader", "style-loader"]
      }
    ]
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js", ".jsx", ".css", ".scss"]
  },
  // ...
};

解决方法

在添加ts编译模块的时候仅留下'ts-loader',即

  {
  test: /\.tsx?$/,
  exclude: /node_modules/,
  use: ["ts-loader"]
},

具体原因

待补充

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部