Webpack,sass被覆盖

时间:2016-11-18 22:24:05

标签: sass webpack webpack-style-loader

我首先要说的是我有一个奇怪的webpack构建。我有2个项目,主项目和共享项目(如公共库)。

每当我从主项目中引用我的共享项目时,它会得到一些奇怪的IE / react / webpack构建错误,当我尝试引用我的共享库时出现错误。为了解决这个问题,我将我的共享库引用别名到我的计算机上不同文件夹中的实际源。看起来像这样:

var alias.shared-library = 'c:\\shared-library\\index.js';

这解决了我奇怪的IE问题。但是,我的webpack生成版本中提出了另一个问题。在生产服务器上,共享库存在于node_modules文件夹中,所以我尝试过这样的事情:

var alias.shared-library = 'node_modules\\shared-library\\index.js';

它主要起作用,除了来自主项目的所有scss都被共享项目中的scss覆盖(输出css只包含来自共享库的scss而不是main)。我的完整设置看起来像这样(我删除了我认为不重要的部分):

var SHARED_PATH = path.resolve(__dirname, 'node_modules/shared-library');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCss = new ExtractTextPlugin('css.styles.css');
const extractScss = new ExtractTextPlugin('scss.styles.css');
const plugs = [];
plugs.push(
    new webpack.DefinePlugin({
        __DEVELOPMENT__: false
    })
);
plugs.push(
    new HtmlWebpackPlugin({
        template: './index.html',
        inject: 'body',
        chunks: ['app', 'vendor']
    })
);
plugs.push(new webpack.NoErrorsPlugin());
plugs.push(
    new CopyWebpackPlugin([
        { from: 'src/assets', to: 'assets' },
    ])
);
plugs.push(
    new SplitByPathPlugin([
        { name: 'vendor', path: [path.join(__dirname, 'node_modules')] },
    ])
);
plugs.push(extractCss);
plugs.push(extractScss);
plugs.push(
    new webpack.optimize.UglifyJsPlugin({
        sourceMap: true,
        compress: {
            warnings: false
        }
    })
);
var alias = {
    'shared-library': path.resolve(SHARED_PATH, '\\src\\index.js')
};
var loaderDirs = ['node_modules', 'webpack_loaders'];
var moduleDirs = ['node_modules', 'src'];
loaderDirs.push(SHARED_PATH + '\\node_modules');
moduleDirs.push(SHARED_PATH + '\\node_modules');
moduleDirs.push(SHARED_PATH + '\\src');

module.exports = {
    entry: {
        app: ['./src/index.js']
    },

    resolve: {
        extensions: ['', '.js', '.scss', '.css'],
        modulesDirectories: moduleDirs,
        alias
    },

    resolveLoader: {
        modulesDirectories: loaderDirs
    },

    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].[hash].js',
        publicPath: '/main/',
        sourceMapFilename: '[name].[hash].js.map',
        chunkFilename: '[id].chunk.js',
    },

    plugins: plugs,

    module: {
        loaders: [
            {
                test: /\.js?$/,
                loader: 'babel?sourceMaps=true',
                include: [/src/, path.resolve(SHARED_PATH, '\\src\\index.js')]
            },
            {
                test: /\.css$/,
                loader: extractCss.extract('style', 'css')
            },
            {
                test: /\.scss$/,
                loader: extractScss.extract('style', 'css!sass'),
                include: /src/    // Trying to exclude scss from shared-library, doesn't work though
            },
            loaders.json,
            loaders.image,
            loaders.svg,
            loaders.font,
            loaders.locale,
            loaders.html
        ]
    },

    postcss: (webpack) => {
        return [
            autoprefixer({
                browsers: ['last 2 versions'],
            }),
            postcssImport({
                addDependencyTo: webpack,
            })
        ];
    },

    externals: {
        'react/lib/ReactContext': 'window',
        'react/lib/ExecutionEnvironment': 'window',
        'react/addons': true
    }
};

我已尝试过类似排除/包含在我的加载器上的不同排列。我认为ExtractTextPlugin上的allChunks选项可能有效:

const extractScss = new ExtractTextPlugin({filename: 'scss.styles.css', allChunks: true});

但它只是给了我这个错误:

    TypeError: path.replace is not a function
    at Template.replacePathVariables (c:\r2\RCA\rca-ui\node_modules\webpack\lib\
TemplatedPathPlugin.js:70:4)
    at Template.applyPlugins [as applyPluginsWaterfall] (c:\r2\RCA\rca-ui\node_m
odules\webpack\node_modules\tapable\lib\Tapable.js:37:47)
    at Compilation.getPath (c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compilatio
n.js:882:27)
    at ExtractTextPlugin.<anonymous> (c:\r2\RCA\rca-ui\node_modules\extract-text
-webpack-plugin\index.js:300:29)
    at Array.forEach (native)
    at ExtractTextPlugin.<anonymous> (c:\r2\RCA\rca-ui\node_modules\extract-text
-webpack-plugin\index.js:289:20)
    at Compilation.next (c:\r2\RCA\rca-ui\node_modules\webpack\node_modules\tapa
ble\lib\Tapable.js:69:14)
    at ExtractTextPlugin.<anonymous> (c:\r2\RCA\rca-ui\node_modules\extract-text
-webpack-plugin\index.js:309:4)
    at Compilation.applyPluginsAsync (c:\r2\RCA\rca-ui\node_modules\webpack\node
_modules\tapable\lib\Tapable.js:71:13)
    at Compilation.<anonymous> (c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compil
ation.js:563:8)
    at Compilation.next (c:\r2\RCA\rca-ui\node_modules\webpack\node_modules\tapa
ble\lib\Tapable.js:67:11)
    at ExtractTextPlugin.<anonymous> (c:\r2\RCA\rca-ui\node_modules\extract-text
-webpack-plugin\index.js:285:5)
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modules\as
ync\lib\async.js:52:16
    at done (c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modu
les\async\lib\async.js:246:17)
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modules\as
ync\lib\async.js:44:16
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\index.js:269:6
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modules\as
ync\lib\async.js:52:16
    at done (c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modu
les\async\lib\async.js:246:17)
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\node_modules\as
ync\lib\async.js:44:16
    at c:\r2\RCA\rca-ui\node_modules\extract-text-webpack-plugin\index.js:259:9
    at c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compilation.js:469:4
    at Array.forEach (native)
    at callback (c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compilation.js:468:14
)
    at Compilation.<anonymous> (c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compil
ation.js:489:4)
    at c:\r2\RCA\rca-ui\node_modules\webpack\lib\Compilation.js:332:10
    at c:\r2\RCA\rca-ui\node_modules\webpack\node_modules\async\lib\async.js:52:
16
    at Object.async.forEachOf.async.eachOf (c:\r2\RCA\rca-ui\node_modules\webpac
k\node_modules\async\lib\async.js:236:30)
    at Object.async.forEach.async.each (c:\r2\RCA\rca-ui\node_modules\webpack\no
de_modules\async\lib\async.js:209:22)
    at Compilation.addModuleDependencies (c:\r2\RCA\rca-ui\node_modules\webpack\
lib\Compilation.js:185:8)
    at Compilation.processModuleDependencies (c:\r2\RCA\rca-ui\node_modules\webp
ack\lib\Compilation.js:170:7)

有关解决此问题的正确方法的任何想法? allChunks选项会帮助我吗?我需要收紧我的包含/排除吗?还有什么我想念的吗?我应该放弃并尝试以另一种方式修复IE问题吗?有没有办法可以调试正在发生的事情?这是我正在使用的库的版本:

"extract-text-webpack-plugin": "1.0.1",
"style-loader": "0.13.0",
"sass-loader": "3.1.2",
"webpack": "1.13.2"

由于

0 个答案:

没有答案