【问题标题】:Why isn't Webpack excluding a folder I specified?为什么 Webpack 不排除我指定的文件夹?
【发布时间】:2016-08-31 11:32:13
【问题描述】:

我将服务器和客户端代码保存在同一个存储库中,尽管我使用 Webpack 仅构建客户端:

如果我删除 src/server 文件夹,我的项目会正常构建。但是当它在那里时,我会收到所有这些 Webpack Typescript 重复定义错误,例如:

[1m[31mERROR in /home/rje/projects/ekaya/typings/main/ambient/react-dom/index.d.ts
(70,5): error TS2300: Duplicate identifier 'export='.

这是由于 Webpack 试图在我的服务器文件夹中构建包含以下内容的文件之一:

/// <reference path="../../../../typings/main.d.ts" />

如何让 Webpack 完全忽略服务器文件夹?

我已经在我的 webpack.config.js 中尝试过:

var rootPath = __dirname; 
var srcPath = path.join(rootPath, 'src/client');
var distPath = path.join(rootPath, 'dist/client');
var serverPath = path.join(rootPath, 'src/serve
...
loaders:
        [
            {test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath]},
            {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },

如果有帮助,这里是完整的 webpack 配置:

//https://webpack.github.io/docs/configuration.html

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');
var rootPath = __dirname; // e.g.  ~/projects/ekaya
var srcPath = path.join(rootPath, 'src/client');
var distPath = path.join(rootPath, 'dist/client');
var serverPath = path.join(rootPath, 'src/server');

module.exports =
{
    bail: true,
    cache: false,
    context: rootPath,
    debug: true,
    devtool: 'source-map', //inline-source-map, https://webpack.github.io/docs/configuration.html#devtool
    target: 'web', //node, web
    devServer:
    {
        contentBase: distPath,
        historyApiFallback: true,
        outputPath: path.join(distPath, 'devServer')
    },
    entry:
    {
        app: path.join(srcPath, 'app/home.jsx'),
        lib: ['react', 'react-router', 'react-dom', 'jquery', 'lodash', 'history']
    },
    output:
    {
        path: distPath,
        publicPath: '',
        filename: '[name].js',
        pathInfo: true
    },
    resolve:
    {
        root: srcPath,
        extensions: ['', '.js', '.jsx', '.ts', '.tsx'],
        modulesDirectories: ['node_modules', srcPath, 'typings']
    },
    module:
    {
        loaders:
        [
            {test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath]},
            {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.scss$/, loaders: ['style', 'css', 'sass']},
            {test: /\.png$/, loader: 'file-loader'},
            {test: /\.jpg$/, loader: 'file-loader'},
            {test: /\.jpeg$/, loader: 'file-loader'},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'},
            {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"}
        ]
    },
    plugins:
    [
        new CopyWebpackPlugin
        ([
            { from: path.join(srcPath, 'images'), to: 'images' }
        ]),
        new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js'),
        new HtmlWebpackPlugin
        ({
            inject: true,
            template: path.join(srcPath, 'index.html')
        }),
        new webpack.NoErrorsPlugin()
    ]
};

【问题讨论】:

  • 你解决过这个问题吗?
  • 不,我想我最终必须将所有内容移到单独的文件夹中 - 即将 webpback 配置放入客户端文件夹
  • 谢谢。在尝试克服构建问题时,我注意到包含/排除似乎受正则表达式的影响比受绝对路径字符串的影响更大。这可能是我做错了什么,但下次可能值得尝试

标签: typescript webpack definitelytyped


【解决方案1】:

您是否尝试过排除 tsconfig.json 中的文件夹?

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    ...
  },
  "exclude": [
    "src/server",
    "node_modules"
  ]
}

【讨论】:

  • 有 40 个错误,现在我得到了 2000 个,所以它显然不起作用,有人有解决方案吗?
  • Saaame 在这里。 Exclude 具有排除的所有定义,但 webpack/ts-loader 仍在尝试编译 node_modules 中的内容。
猜你喜欢
  • 1970-01-01
  • 2017-10-12
  • 2020-05-05
  • 2017-04-20
  • 2018-01-24
  • 2017-11-06
  • 2020-07-22
  • 2011-04-19
  • 2022-06-14
相关资源
最近更新 更多