为什么不在Windows上使用我的webpack配置?

时间:2016-08-30 10:10:25

标签: reactjs webpack webpack-style-loader

我的webpack配置可以在MacOS上运行,但它在Windows上显示错误。

webpack.config.js

var path = require("path");
var webpack = require("webpack");
var FileSystem = require("fs");
var argv = require('yargs').argv;
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-source-map',
    entry: {
      main: ['webpack/hot/dev-server','webpack-dev-server/client?http://localhost:8090',path.resolve(__dirname, 'app','index.jsx')],
      vendor:['react', 'redux', 'amazeui-react','react-redux' ,'react-router']
    },
    output: {
        path: path.resolve(__dirname, 'dist','app'),
        publicPath: '/dist/app/',
        filename: 'bundle.[hash].js',
        chunkFilename: '[id].[hash].chunk.js',
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin('vendor',  'vendor.js'),
        new ExtractTextPlugin('[name]-[chunkhash].css', {allChunks: true}),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('dev')
            }
        }),
        new webpack.DefinePlugin({
            'process.env.db_env': JSON.stringify(argv['db_env'] ? argv['db_env'] + '' : 'dev'),
        }),
        function() {
            this.plugin("done", function(statsData) {
                var stats = statsData.toJson();
                var bundlejs,maincss;
                var mains = stats.assetsByChunkName.main;
                console.log(mains);
                for (var i = 0; i < mains.length; i++) {
                  if (/^(bundle).+(js)$/.test(mains[i])) {
                    bundlejs = mains[i]
                  }
                  if (/^(main).+(css)$/.test(mains[i])) {
                    maincss = mains[i]
                  }
                }
                if (!stats.errors.length) {
                    var htmlFileName = "index.html";
                    var html = FileSystem.readFileSync(path.join('./resources/temp', htmlFileName), "utf8");
                    var htmlOutput = html.replace('bundle.js', bundlejs).replace('main.css', maincss);
                    FileSystem.writeFileSync(path.join('./', htmlFileName), htmlOutput);
                }
            });
        }
    ],
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: "style!css"
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style','css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!sass?outputStyle=expanded&sourceMap')
            },
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel', // 'babel-loader' is also a legal name to reference
                query: {
                    plugins: ['transform-runtime', 'add-module-exports', "transform-decorators-legacy"],
                    presets: ['react', 'es2015', 'stage-0']
                }
            },
            {
                test: /\.js$/,
                loaders: [ 'babel' ],
                exclude: /node_modules/,
                include: __dirname
            },
            { test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=8192' }
        ]
    },
    externals: {
        BMap:'BMap',
        BMapLib:'BMapLib'
    },
    resolve: {
        root:path.resolve(__dirname),
        modulesDirectories: [
            'app',
            'node_modules'
        ],
        extensions: ['', '.js', '.jsx','.scss','.css']
    },
    resolveLoader: {
        root:path.resolve(__dirname,"node_modules"),
    },
    devServer: {
        port: 8090,
        hot: true,
        host:"0.0.0.0",
        historyApiFallback: {
            index: 'index.html'
        }
    }

};

我的目录结构是:

+src
    +app
    +node_modules
    +webpack.config.js

在MacOS上一切正常,但是当我在Windows上运行它时,我收到一个错误:

 Module not found: Error: Cannot resolve module 'css' in somefile.

文件就像这样:

 const style = require('./BuildingDetailContainer.scss');

我确信我已经安装了css-loader sass-loader style-loader

这是我的package.json,我怀疑它是由这个文件引起的。也许是因为我没有将我的加载器添加到依赖项中。

{
  "name": "souban-website",
  "version": "1.0.0",
  "description": "souban website",
  "author": "souban team",
  "license": "UNLICENSED",
  "private": true,
  "engines": {
    "node": ">=5.0.0",
    "npm": "^3.0.0"
  },
  "scripts": {
    "clean": "rm -rf dist",
    "start": "npm run webpack-dev",
    "deploy": "npm run test && npm run clean && npm run compile",
    "webpack": "rm -rf dist && NODE_ENV=production webpack  --display-error-details --colors --progress -p --config webpack.prod.config.js",
    "webpack-dev": "webpack-dev-server --progress --colors --hot --inline",
    "webpack-release": "rm -rf dist && NODE_ENV=production webpack  --display-error-details --colors --progress -p --config webpack.rele.config.js --db_env production",
    "webpack-dev-release": "rm -rf dist && NODE_ENV=production webpack  --display-error-details --colors --progress -p --config webpack.rele.config.js --db_env dev"
  },
  "dependencies": {
    "amazeui-react": "^1.0.1",
    "babel-runtime": "^6.6.1",
    "color": "^0.11.3",
    "colormin": "^1.1.2",
    "css-loader": "^0.24.0",
    "extract-text-webpack-plugin": "^1.0.1",
    "moment": "^2.12.0",
    "node-sass": "^3.8.0",
    "postcss-colormin": "^2.2.0",
    "react": "^0.14.7",
    "react-count-to": "^0.4.0",
    "react-dom": "^0.14.6",
    "react-motion": "^0.4.2",
    "react-redux": "^4.0.6",
    "react-router": "^2.4.0",
    "redux": "^3.0.6",
    "sass-loader": "^4.0.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.12.12"
  },
  "devDependencies": {
    "autobind-decorator": "^1.3.3",
    "babel-core": "^6.4.5",
    "babel-loader": "^6.2.1",
    "babel-plugin-add-module-exports": "^0.1.4",
    "babel-plugin-react-transform": "^2.0.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-runtime": "^6.6.0",
    "babel-polyfill": "^6.3.14",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "babel-preset-stage-0": "^6.5.0",
    "css-loader": "^0.23.1",
    "file-loader": "^0.8.5",
    "isomorphic-fetch": "^2.2.1",
    "jsx-loader": "^0.13.2",
    "lodash": "^4.3.0",
    "node-sass": "^3.6.0",
    "radium": "^0.16.6",
    "react-addons-css-transition-group": "^0.14.6",
    "react-cookie": "^0.4.3",
    "react-dom": "^0.14.5",
    "react-hot-loader": "^1.3.0",
    "react-modal": "^0.6.1",
    "react-motion": "^0.4.1",
    "redux-logger": "^2.3.1",
    "redux-persist": "^1.5.5",
    "redux-persist-crosstab": "^1.0.1",
    "redux-thunk": "^1.0.3",
    "sass-loader": "^3.2.0",
    "scroll-behavior": "^0.3.0",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.7",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0",
    "yargs": "^4.7.1"
  }
}

1 个答案:

答案 0 :(得分:0)

我在Windows上工作,让事情顺利运行是一场噩梦,一切都在Mac上工作正常,但在Windows上没有,所以如果这只是Windows和Sass的错误,你可能需要手动添加路径sass loader。

Windows上的技巧就在这里:

  • 显示隐藏的文件和文件夹。
  • 检查'user / appData'文件夹,路径应为:C:\ Users \ user \ AppData \ Roaming \ npm
  • 将环境变量添加到Windows:NODE_PATH并将其指向nodeModules C:\ Users \ user \ AppData \ Roaming \ npm \ nodeModules
  • 运行npm install -g
  • 关闭并重新打开终端。

我正在使用sass loader 3.0,在我的webpack.config中它是这样的:

const path = require("path");
const srcPath = path.join(__dirname, 'src');
const sassLoaders = [
  "css-loader",
  "autoprefixer-loader?browsers=last 2 version",
  "sass-loader?indentedSyntax=sass&includePaths[]=" + path.resolve(__dirname, "./src"),
];

我的package.json用于加载:

"autoprefixer-loader": "3.1.0" 
"sass-loader": "^3.0.0",
"style-loader": "0.12.4"

我的决心使用path.sep在Windows中我必须这样做:

 resolve: {
    extensions: ["", ".js", ".scss"],
    modulesDirectories: ["src", "node_modules"],
    root: [__dirname + path.sep + 'scripts'],
  }

作为一个抬头,对于我这样使用的装载机,我注意到你的模块中没有sass加载器:

 {test: /\.scss$/, loaders: ["style", "css", "sass"]},
 {test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},