node.js - "npm run"卡在 ts-loader (webpack)

标签 node.js typescript webpack visual-studio-code ts-loader

我正在 Mac OS X 10.11.3 上使用 webpack 构建一个由静态 HTML 和其他 Assets 组成的网络应用程序。该应用程序与另一台服务器上的 API 对话。

我在使用 webpack 构建我的应用程序时遇到了问题。构建过程似乎在 ts-loader 执行时或附近挂起。我正在这样运行我的构建:

npm run go --loglevel verbose

从我的 package.json 执行这个命令:

./node_modules/.bin/webpack-dev-server --display-reasons --display-chunks --watch

终端窗口中的输出以

结尾
ts-loader: Using typescript@1.7.5 and /Users/mn/Documents/source/J/appstore/store-front/app/ts/tsconfig.json

我已经尝试删除 node_modules 文件夹并重新安装它们;我试过卸载 webpack 并重新安装;我尝试将我的 webpack.config.js 恢复到我知道有效的版本;但它只是卡在这里!

我的 webpack.config.js 看起来像这样:

var webpack = require('webpack'),
    ReloadPlugin = require('webpack-reload-plugin'),
    path = require('path'),
    ChunkManifestPlugin = require('chunk-manifest-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    WebpackNotifierPlugin = require('webpack-notifier'),
    ExtractTextPlugin = require("extract-text-webpack-plugin");

/**
 * optimist has been depracted. Find an alternative? minimist?  
 */
var argv = require('optimist')
    .alias('r', 'release').default('r', false)
    .argv;

/**
 * Useful variables
 */
var cwd = process.cwd();
var DEBUG = !argv.release;
var isDevServer = process.argv.join('').indexOf('webpack-dev-server') > -1;
var version = require(path.resolve(cwd, 'package.json')).version;
var reloadHost = "0.0.0.0";
var npmRoot = __dirname + "/node_modules";
var appDir = __dirname + "/app";

var entry = ["./app/ts/bootstrap"]

if (isDevServer) {
    entry.unshift("webpack-dev-server/client?http://" + reloadHost + ":8080");
}

function makeConfig(options) {
    return {
        cache: true,
        debug: true,
        verbose: true,
        displayErrorDetails: true,
        displayReasons: true,
        displayChunks: true,
        context: __dirname,
        entry: {
            app: entry,
            vendor: './app/ts/vendor.ts'
        },
        stats: {
            colors: true,
            reasons: DEBUG
        },
        devtool: 'source-map',
        recordsPath: path.resolve('.webpack.json'),
        devServer: {
            inline: true,
            colors: true,
            contentBase: path.resolve(cwd, "build"),
            publicPath: "/"
        },
        output: {
            path: path.resolve(cwd, "build"),
            filename: "[name].js",
            publicPath: "/",
            chunkFilename: "[id].bundle.js",

            // Hot Module Replacement settings:
            hotUpdateMainFilename: "updates/[hash].update.json",
            hotUpdateChunkFilename: "updates/[hash].[id].update.js"
        },
        plugins: [
            new webpack.IgnorePlugin(/spec\.js$/),
            new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.js', minChunks: Infinity }),
            new webpack.optimize.CommonsChunkPlugin({ name: 'common', filename: 'common.js', minChunks: 2, chunks: ['app', 'vendor'] }),
            new ExtractTextPlugin("styles.css"),
            new webpack.DefinePlugin({
                VERSION: JSON.stringify(version),
                ENV: JSON.stringify(options.env)
            }),
            new HtmlWebpackPlugin({
                template: path.join(appDir, "index.html"),
            }),
            new ReloadPlugin(isDevServer ? 'localhost' : ''),
            new WebpackNotifierPlugin({
                title: "Jisc AppStore App"
            }),
        ],
        resolveLoader: {
            root: path.join(__dirname, 'node_modules'),
            modulesDirectories: ['node_modules'],
            fallback: path.join(__dirname, "node_modules")
        },
        resolve: {
            root: [path.resolve(cwd)],
            modulesDirectories: [
                'node_modules', 'app', 'app/ts', '.'
            ],
            extensions: ["", ".ts", ".js", ".json", ".css"],
            alias: {
                'app': 'app',
                'scripts': npmRoot
            }
        },
        module: {
            preLoaders: [
                { test: /\.ts$/, loader: "tslint" }
            ],

            loaders: [
                { test: /\.(png|jp?g|gif)$/, loaders: ["url", "image"] },
                { test: /\.json$/, loader: 'json' },
                { test: /^(?!.*\.min\.css$).*\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader?sourceMap") },
                { test: /\.scss$/, loaders: ['style', 'css?sourceMap', 'sass?sourceMap'] },
å                { test: /\.html$/, loader: "html" },
                { test: /\.ts$/, loader: 'ts', exclude: [/test/, /node_modules/] },
                { test: /vendor\/.*\.(css|js)/, loader: 'file-loader?name=[path][name].[ext]', exclude: [/node_modules/] },
                { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader?limit=10000&minetype=application/font-woff" },
                { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
            ],
            noParse: [
                /\.min\.js/,
                /vendor[\/\\].*?\.(js|css)$/
            ]
        },
        tslint: {
            emitErrors: false,
            failOnHint: false
        }
    }
}

var config = makeConfig(argv)

console.log(require('util').inspect(config, { depth: 10 }))
module.exports = config;

我的 tsconfig.json 看起来像这样:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noEmitHelpers": false,
        "sourceMap": true
    },
    "filesGlob": [
        "./app/**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "compileOnSave": false,
    "buildOnSave": false
}

任何人都可以建议可能会发生什么吗?我似乎无法从 webpack 开发服务器或 npm 构建生成任何日志。

最佳答案

在对 ts-loader 进行数小时的逆向工程后,我终于找到了导致这种“卡住”(看起来如此)的原因:

我正在构建一个网络抓取工具,并且在之前成功部署和现在失败/卡住部署之间的散列目录结构中积累了大约 40Gb 的缓存数据。

原来,因为我忘记在我的 tsconfig.json 的“排除”选项中包含根缓存目录,ts-loader 正在遍历缓存目录中的所有子文件夹。所以,它实际上并没有挂起,它只是在浏览数百万个文件。

当我将缓存目录添加到排除文件选项时,一切恢复正常。

希望这能帮助您解决问题。如果您想了解 typescript 的运行情况,我建议您在 visitDirectory-function in typescript.js 中试验一些 console.logs。这就是最终帮助我解决这个问题的原因。

干杯 山姆

关于node.js - "npm run"卡在 ts-loader (webpack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35552100/

相关文章:

node.js - 在服务器和客户端上渲染 Backbone.js 应用程序

javascript - 包未在项目中正确导入

html - 无法访问元素 margin-left

javascript - 将 Bower 与 Webpack 结合使用 - React

Webpack: "there are multiple modules with names that only differ in casing"但引用的模块是相同的

javascript - Webpack 中具有多个 vendor bundle 的多个入口点

javascript - $push value to array with mongoose 只有当键存在时

node.js - npm 本地路径 - 是否可以从子文件夹导入?

node.js - req.body 未返回提交的数据

angular - getRootNav() 和 navCtrl() 方法之间的区别