在surviveJS电子书之后,我正在尝试为我的ReactJS项目设置webpack配置。
运行我的启动脚本后,我收到以下错误:
> ./app/index.js中的错误模块解析失败: /Users/shooshte/PersonalProjects/surviveJS/node_modules/eslint-loader/index.js!/Users/shooshte/PersonalProjects/surviveJS/app/index.js 意外的令牌(5:0)您可能需要一个合适的加载器来处理 这个文件类型。 | | ReactDOM.render(| Hello world,|
document.getElementById(' app')| ); @多 (的WebPack)-dev - 服务器/客户端?http://localhost:8080 webpack / hot / only-dev-server babel-polyfill ./app
这是我的index.js:
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div>Hello world</div>,
document.getElementById('app')
);
我的webpack配置:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackTemplate = require('html-webpack-template');
const merge = require('webpack-merge');
const parts = require('./webpack.parts');
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build')
};
const common = merge(
{
entry: {
app: ['babel-polyfill', PATHS.app]
},
resolve: {
extensions: ['.js', '.jsx']
},
output: {
path: PATHS.build,
filename: '[name].js'
},
plugins: [
new HtmlWebpackPlugin({
template: HtmlWebpackTemplate,
title: 'ReactJS Boilerplate',
appMountID: 'app',
mobile: true,
inject: false
})
]
},
parts.loadFonts(PATHS.app),
parts.loadImages(PATHS.app),
parts.lintCSS(PATHS.app),
parts.lintJavaScript(PATHS.app)
);
module.exports = function(env) {
if(env === 'production') {
return merge(
common,
{
output: {
chunkFilename: 'scripts/[chunkhash].js',
filename: '[name].[chunkhash].js',
// Tweak this to match your GitHub project name
publicPath: '/surviveJS/'
},
plugins: [
new webpack.HashedModuleIdsPlugin()
]
},
parts.setFreeVariable(
'process.env.NODE_ENV',
'production'
),
parts.loadJavaScript(PATHS.app),
parts.minifyJavaScript(PATHS.app),
parts.extractBundles([
{
name: 'vendor',
entries: ['react']
},
{
name: 'manifest'
}
]),
parts.clean(PATHS.build),
parts.generateSourcemaps('source-map'),
parts.extractSASS(),
parts.purifyCSS(PATHS.app)
);
}
return merge(
common,
{
performance: {
hints: false
},
plugins: [
new webpack.NamedModulesPlugin()
]
},
parts.generateSourcemaps('eval-source-map'),
parts.loadSASS(),
parts.devServer({
host: process.env.HOST,
port: process.env.PORT
})
);
};
零件模块的相关部分:
exports.devServer = function(options) {
return {
devServer: {
historyApiFallback: true,
hot: true,
hotOnly: true,
stats: 'errors-only',
host: options.host, // Defaults to `localhost`
port: options.port // Defaults to 8080
},
plugins: [
new webpack.HotModuleReplacementPlugin({
})
]
};
};
exports.lintJavaScript = function(paths) {
return {
module: {
rules: [
{
test: /\.js$/,
include: paths,
use: 'eslint-loader',
enforce: 'pre'
}
]
}
};
};
exports.loadJavaScript = function(paths) {
return {
module: {
rules: [
{
test: /\.(js|jsx)$/,
include: paths,
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
]
}
};
};
这已经太大了,但我不知道我的webpack配置的哪个部分失败了。如果您需要有关样板的更多信息,请查看gitHub repo。
答案 0 :(得分:0)
发现问题,
我只是在生产环境中而不是在任何地方加载javascript。将loadJavascript移动到常见修复了我的问题:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackTemplate = require('html-webpack-template');
const merge = require('webpack-merge');
const parts = require('./webpack.parts');
const PATHS = {
app: path.join(__dirname, 'app'),
build: path.join(__dirname, 'build')
};
const common = merge(
{
entry: {
app: ['babel-polyfill', PATHS.app]
},
resolve: {
extensions: ['.js', '.jsx']
},
output: {
path: PATHS.build,
filename: '[name].js'
},
plugins: [
new HtmlWebpackPlugin({
template: HtmlWebpackTemplate,
title: 'ReactJS Boilerplate',
appMountID: 'app',
mobile: true,
inject: false
})
]
},
parts.loadJavaScript(PATHS.app),
parts.loadFonts(PATHS.app),
parts.loadImages(PATHS.app),
parts.lintCSS(PATHS.app),
parts.lintJavaScript(PATHS.app)
);
module.exports = function(env) {
if(env === 'production') {
return merge(
common,
{
output: {
chunkFilename: 'scripts/[chunkhash].js',
filename: '[name].[chunkhash].js',
// Tweak this to match your GitHub project name
publicPath: '/surviveJS/'
},
plugins: [
new webpack.HashedModuleIdsPlugin()
]
},
parts.setFreeVariable(
'process.env.NODE_ENV',
'production'
),
parts.minifyJavaScript(PATHS.app),
parts.extractBundles([
{
name: 'vendor',
entries: ['react']
},
{
name: 'manifest'
}
]),
parts.clean(PATHS.build),
parts.generateSourcemaps('source-map'),
parts.extractSASS(),
parts.purifyCSS(PATHS.app)
);
}
return merge(
common,
{
performance: {
hints: false
},
plugins: [
new webpack.NamedModulesPlugin()
]
},
parts.generateSourcemaps('eval-source-map'),
parts.loadSASS(),
parts.devServer({
host: process.env.HOST,
port: process.env.PORT
})
);
};