2017-08-11 104 views
1

我知道使用webpack和webpack-dev-server構建的區別在於後者輸出和服務於內存,而前者將輸出創建爲磁盤並且不會啓動快遞服務。加載webpack-dev-server的問題加載

我的問題是,我的webpack配置工作正常,如果我通過webpack運行它,然後運行webpack-dev-server,但後者看着它,當發生更改時,它們不會反映在輸出中因爲它應該隻影響內存中的內容)。但是,如果我刪除了磁盤上的構建文件,並且不首先與webpack綁定,我無法使webpack-dev-server加載Angular應用程序(頁面空白並顯示「無法獲取/」)。

半工作方法

此作品(但並不理想,因爲我建立這一切兩次):

  • 創建指定的輸出和devserver選項的WebPack配置。
  • 運行npm命令「npm run webpack -- --config appConfigs/webpack.dev.js --progress --profile && npm run webpack-dev-server -- --config appConfigs/webpack.dev.js --open --progress --profile --inline
  • 它建立到輸出目錄,然後webpack-dev-server啓動這些文件,它運行順暢。

僅使用的WebPack-DEV-服務器

這看起來不正確。 webpack-dev-server的全部重點是從內存中提供服務,所以它根本不需要從輸出文件讀取數據。如果我按照以下步驟進行操作,則不起作用:

  • 刪除以前生成的輸出文件夾。
  • 運行故宮命令「npm run webpack-dev-server -- --config appConfigs/webpack.dev.js --open --progress --profile --inline
  • 當瀏覽器加載起來,用錯誤招呼「無法GET /」

這裏是的WebPack-dev的服務器輸出與問候到內容位於: 項目運行在http://localhost:9000/ 的WebPack輸出從/ 內容不從的WebPack是由C送達:\某某\ DIST

所以,我希望,當它加載了,它會找到的index.html在http://localhost:9000,但我取而代之的是「無法GET /」。

現在,另一個SO帖子指出,由於沒有編寫任何文件,我需要使用像HtmlWebpackPlugin這樣的插件來確保它已正確創建並可用於webpack,但似乎沒有解決問題。

webpack.dev.config內容

這裏是我的webpack.dev.config(注意這個文件工作正常,如果我的WebPack通常捆綁,如果我用的WebPack捆綁提供精美,然後運行的WebPack-dev亡服務器)。

const webpack = require('webpack'); 
const helpers = require('./helpers'); 

const DefinePlugin = require('webpack/lib/DefinePlugin'); 
const HtmlWebpackPlugin = require('html-webpack-plugin'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); 

/** 
* Webpack constants 
*/ 
const ENV = process.env.ENV = process.env.NODE_END = 'development'; 
const HOST = 'localhost'; 
const PORT = 3000; 
const PUBLIC = process.env.PUBLIC || undefined; 
const HMR = helpers.hasProcessFlag('hot'); 
const METADATA = { 
    host: HOST, 
    port: PORT, 
    public: PUBLIC, 
    ENV: ENV, 
    HMR: HMR 
}; 

module.exports = { 
    devtool: 'cheap-module-source-map', 

    performance: { 
     hints: false 
    }, 

    entry: { 
     'polyfills': helpers.root('src', 'polyfills.browser.ts'), 
     'app': helpers.root('src', 'main.browser.ts') 
    }, 

    output: { 
     path: helpers.root('dist'), 
     filename: 'js/[name].bundle.js', 
     chunkFilename: 'js/[id].chunk.js', 
     sourceMapFilename: '[file].map', 
     publicPath: '/' 
    }, 

    devServer: { 
     historyApiFallback: true, 
     contentBase: helpers.root('dist'), 
     port: 9000 
    }, 

    resolve: { 
     extensions: ['.ts', '.js', '.json', '.css', '.scss', '.html'] 
    }, 

    module: { 
     rules: [ 
      { 
       test: /\.ts$/, 
       use: [ 
         { 
          loader: 'awesome-typescript-loader', 
          options: { 
           configFileName: 'tsconfig.webpack.json' 
          } 
         }, 
         'angular-router-loader', 
         'angular2-template-loader', 
         { 
          loader: 'tslint-loader', 
          options: { 
           conigFile: 'tslint.json' 
          } 
         }, 
         'source-map-loader' 
       ], 
       exclude: [/\.(spec|e2e)\.ts$/] 
      }, 
      { 
       test: /\.(png|jpg|gif|woff|woff2|ttf|svg|eot)$/, 
       loader: 'file-loader?name=assets/[name]-[hash:6].[ext]' 
      }, 
      { 
       test: /\.json$/, 
       loader: 'json-loader' 
      }, 
      { 
       test: /favicon.ico$/, 
       loader: 'file-loader?name=/[name].[ext]' 
      }, 
      { 
       test: /\.scss$/, 
       loaders: ["style-loader", "css-loader", "sass-loader"] 
      }, 
      { 
       test: /\.html$/, 
       loader: ['html-loader'], 
      } 
     ], 
     exprContextCritical: false 
    }, 
    plugins: [ 
     new DefinePlugin({ 
      'ENV': JSON.stringify(METADATA.ENV), 
      'HMR': METADATA.HMR, //unused here 
      'process.env': { 
       'ENV': JSON.stringify(METADATA.ENV), 
       'NODE_ENV': JSON.stringify(METADATA.ENV), 
       'HMR': METADATA.HMR //unused here 
      } 
     }), 
     new LoaderOptionsPlugin({ 
      debug: true, 
      options: { 
      } 
     }), 
     new webpack.ContextReplacementPlugin(
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/, 
      helpers.root('src'), 
      {} 
     ), 
     new webpack.optimize.CommonsChunkPlugin({ 
      name: ['app', 'polyfills'], 
      minChunks: Infinity 
     }), 
     new HtmlWebpackPlugin({ 
      inject: 'body', 
      template: './src/index.html' 
     }) 
    ] 
}; 

(部分)從的WebPack-DEV-服務器輸出

有限公司爲簡潔

10% building modules 2/2 modules 0 active 
Project is running at http://localhost:9000/ 
webpack output is served from/
Content not from webpack is served from C:\xyz\dist 
404s will fallback to /index.html 
... 
Hash: 8ccd65a6efa15f3c1590 
Version: webpack 3.5.1 
Time: 29663ms 
        Asset  Size Chunks     Chunk Names 
      js/app.bundle.js 4.6 MB  0 [emitted] [big] app 
    js/polyfills.bundle.js 577 kB  1 [emitted] [big] polyfills 
     js/app.bundle.js.map 4.97 MB  0 [emitted]   app 
js/polyfills.bundle.js.map 691 kB  1 [emitted]   polyfills 
       index.html 1.14 kB   [emitted] 
[560] (webpack)-dev-server/client?http://localhost:9000 5.83 kB {1} [built] 
     [] -> factory:77ms building:65ms = 142ms 
[747] multi (webpack)-dev-server/client?http://localhost:9000 ./src/polyfills.browser.ts 40 bytes {1} [built] 
     factory:0ms building:3ms = 3ms 
[756] ./node_modules/loglevel/lib/loglevel.js 6.74 kB {1} [built] 
     [] -> factory:6700ms building:254ms = 6954ms 
[757] (webpack)-dev-server/client/socket.js 856 bytes {1} [built] 
     [] -> factory:34ms building:757ms = 791ms 
[789] (webpack)-dev-server/client/overlay.js 3.6 kB {1} [built] 
     [] -> factory:36ms building:743ms = 779ms 
[794] (webpack)/hot nonrecursive ^\.\/log$ 170 bytes {1} [built] 
     [] -> factory:31ms building:14ms = 45ms 
[796] (webpack)/hot/emitter.js 77 bytes {1} [built] 
     [] -> factory:6257ms building:24ms = 6281ms 
[798] ./src/polyfills.browser.ts 1.16 kB {1} [built] 
     [] -> factory:188ms building:6063ms = 6251ms 
[799] ./node_modules/core-js/es6/regexp.js 346 bytes {1} [built] 
     [] -> factory:551ms building:50ms = 601ms 
[806] ./node_modules/core-js/es6/map.js 208 bytes {1} [built] 
     [] -> factory:552ms building:55ms dependencies:4419ms = 5026ms 
[812] ./node_modules/core-js/es6/set.js 208 bytes {1} [built] 
     [] -> factory:552ms building:53ms dependencies:4416ms = 5021ms 
[813] ./node_modules/core-js/es6/weak-map.js 176 bytes {1} [built] 
     [] -> factory:553ms building:56ms dependencies:4415ms = 5024ms 
[864] multi (webpack)-dev-server/client?http://localhost:9000 ./src/main.browser.ts 40 bytes {0} [built] 
     factory:0ms building:2ms dependencies:78ms = 80ms 
[865] ./src/main.browser.ts 899 bytes {0} [built] 
     [] -> factory:452ms building:5896ms = 6348ms 
[1436] ./src/app/environment.ts 1.01 kB {0} [built] 
     [] -> factory:61ms building:106ms = 167ms 
    + 1422 hidden modules 
Child html-webpack-plugin for "index.html": 
    1 asset 
     [0] ./node_modules/html-webpack-plugin/lib/loader.js!./src/index.html 1.18 kB {0} [built] 
      factory:476ms building:5898ms = 6374ms 
webpack: Compiled successfully. 

所以,它看起來像它的工作原理,但我無法瀏覽到任何與得到「無法獲取{無論我試圖去}「

在此期間,我可以運行半工作方式,但它會捆綁它通過webpack,輸出到目錄,然後啓動webpack-dev-serv呃來自該目錄中的文件,這不像它應該做的那樣(這需要兩倍的時間,因爲它捆綁了兩次)。

我錯過了什麼?

回答

0

刪除了node_modules,並重新安裝了使用紗線的所有東西,它的工作方式與廣告完全相同。不知道有什麼不同,但沒有進一步的問題。