why webpack can't find loader - javascript

Here's my pacjage.json:
{
"name": "redux-todo",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "webpack-dev-server"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"webpack": "^1.13.2"
},
"dependencies": {
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^4.4.5",
"redux": "^3.5.2"
}
}
webpack.config.js:
var path = require('path');
module.exports = {
entry: './index.js',
output: {
path: './',
filename: 'app.js'
},
devServer: {
inline: true,
port: 3334
},
resolveLoader: { root: path.join(__dirname, "node_modules") },
module: {
loaders: [
{
test: /\.js$/,
exclude: '/node_modules',
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
and I have following project directory structure:
├── actions.js
├── components
├── containers
├── index.js
├── node_modules
├── package.json
├── reducers.js
├── test.js
└── webpack.config.js
the absolute path to project dir is /home/dmitriy/WebstormProjects/Redux-todo
so why when I run npm start it crashes with error:
ERROR in (webpack)/~/process/browser.js
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/usr/local/lib/node_modules/webpack/node_modules/process"
what's this /usr/local/lib/node_modules/webpack/node_modules/process path and why it says that it searches relative to it?
Googling this error I found that
IMPORTANT: The loaders here are resolved relative to the resource which they are applied to. This means they are not resolved relative the the configuration file. If you have loaders installed from npm and your node_modules folder is not in a parent folder of all source files, webpack cannot find the loader. You need to add the node_modules folder as absolute path to the resolveLoader.root option. (resolveLoader: { root: path.join(__dirname, "node_modules") })
should fix it, but as you can see I have it in my config and still seeing this error.
I'm on ubuntu 16.04 LTS, nodejs version is 4.2.6, npm 3.5.2

You are only excluding /node_modules as absolute path:
exclude: '/node_modules'
If you want to recursively exclude all node_modules try using:
exclude: /node_modules/
The difference is subtle but the former is using a string with an absolute path to the root node_modules and the latter a regular expression that matches any path with node_modules.
This should work without the resolveLoader configuration. So you can remove this field:
resolveLoader: { root: path.join(__dirname, "node_modules") },

actually I removed node_modules, tweaked package.json a little bit:
{
"name": "redux-todo",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "webpack-dev-server"
},
"dependencies": {
"babel": "^6.5.2",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^4.4.5",
"redux": "^3.5.2"
},
"devDependencies": {
"babel-core": "^6.13.2",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.1"
}
}
aaand it worked. Not sure why. Here's how webpack config looks like now:
var path = require('path');
module.exports = {
entry: './index.js',
output: {
path: './',
filename: 'app.js'
},
devServer: {
inline: true,
port: 3334
},
module: {
loaders: [
{
test: /\.js$/,
exclude: '/node_modules',
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
I have no clue what was that about ¯_(ツ)_/¯

For my project the loader is configured like this:
{
test: /\.js$/,
exclude: '/node_modules',
loader: "babel-loader"
}
And the .babelrc file contains this:
{
"presets": ["es2015","react"]
}
Watching the first config of webpack that you have posted, I noticed the option resolveLoader: { root: path.join(__dirname, "node_modules") }. This option isn't needed if you have all loader on correct path (node_modules directory on project root).

Did you mean to use babel-loader? If so, and if there is no babel-loader folder in node_modules, then npm install babel-loader#7.1.1 --save-dev should fix the issue and generate that folder.

Related

Webpack does not generate source maps

I'm new to webpack and am trying to make a webpack configuration that generates source maps so I can debug my JS and JSX code in the browser.
The build is actually successful so far, except that no source maps are generated. I've been researching for a long time and tried many solutions, including several ones from StackOverflow. But nothing works for me.
Below I am posting my current files in the hope that someone can help me. Thanks in advance for helping.
Root directory after build:
- webpack.config.js
- package.json
- package-lock.json
- node_modules
- dist
|- bundle.js
|- bundle.js.map
|- index.html
|- main.min.js
|- main.min.js.map
|- node_modules
- src
|- index.html
|- index.js
|- helper.jsx //My own jsx file
What my webpack app looks like in the browser:
webpack.config.js:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports =
{
entry: path.join(__dirname, "src", "index.js"),
mode: 'development',
devtool: 'source-map',
output:
{
path: path.resolve(__dirname, "dist"),
filename: "[name].min.js"
},
module:
{
rules:
[
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use:
{
loader: "babel-loader",
options:
{
presets:
[
'#babel/preset-env',
'#babel/preset-react'
]
}
}
},
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
{
test: /\.(png|jp(e*)g|gif)$/,
use: ['file-loader'],
},
]
},
plugins:
[
new HtmlWebpackPlugin
(
{
template: path.join(__dirname, "src", "index.html"),
hash: true
}
),
],
}
package.json:
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"dev": "webpack serve"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"#babel/core": "^7.20.12",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"babel-loader": "^9.1.2",
"html-webpack-plugin": "^5.5.0",
"source-map-loader": "^4.0.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
}
}

Running webpack as child process to generate source files

I'm new to webpack so sorry if this is something obvious. I'm trying to get the package antlr4-webpack-loader to work, generating some source code from a .g4 file. I think I'm most of the way there, as I have a javascript file with require in it, and the .bundle. file seems to contain the output of antlr4, however it also has this:
module.exports = __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module 'antlr4/index'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
contentScript.js starts as below:
import 'jquery';
import 'antlr4'; // This line doesn't cause the error
import '../anaplan/AnaplanFormula.g4'; // This line causes the MODULE_NOT_FOUND error
webpack.config.js as below:
var webpack = require("webpack"),
path = require("path"),
fileSystem = require("fs"),
env = require("./utils/env"),
CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin,
CopyWebpackPlugin = require("copy-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
WriteFilePlugin = require("write-file-webpack-plugin");
var options = {
mode: process.env.NODE_ENV || "development",
entry: {
contentScript: path.join(__dirname, "src", "js", "contentScript.js"),
background: path.join(__dirname, "src", "js", "background.js"),
},
output: {
path: path.join(__dirname, "build"),
filename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
loader: "style-loader!css-loader",
exclude: /node_modules/
},
{
test: new RegExp('.(jpg|jpeg|png|gif|eot|otf|svg|ttf|woff|woff2)$'),
loader: "file-loader?name=[name].[ext]",
exclude: /node_modules/
},
{
test: /\.html$/,
loader: "html-loader",
exclude: /node_modules/
},
{
test: /\.g4/,
exclude: /(node_modules)/,
use: {
loader:'antlr4-webpack-loader'
}
}
]
},
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules']
},
node: { module: "empty", net: "empty", fs: "empty" },
plugins: [
// clean the build folder
new CleanWebpackPlugin(),
// expose and write the allowed env vars on the compiled bundle
new webpack.EnvironmentPlugin(["NODE_ENV"]),
new CopyWebpackPlugin([{
from: "src/manifest.json",
transform: function (content, path) {
// generates the manifest file using the package.json informations
return Buffer.from(JSON.stringify({
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString())
}))
}
}]),
new WriteFilePlugin(),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
]
};
if (env.NODE_ENV === "development") {
options.devtool = "cheap-module-source-map";
}
module.exports = options;
From what I can tell the antlr4-webpack-loader plugin spawns a new webpack process for just the .g4 file and generates a bundle.js file from that as it's output, which then gets bundled into the 'parent' file. I can step through the code within antlr4-webpack-loader and that does appear to work (like I say, the output from it appears to be within my contentScript.bundle.js file. I can see it as something about externals: [ 'antlr4/index' ], which I guess is because the files it generates from the .g4 file require it, but the reference should get resolved by the script that required the g4.
For reference, package.json below which doesn't include the antlr4 package in devDependencies, however I get the same error when i include it in both devDependencies and dependencies:
{
"name": "anaplanextension",
"version": "1.0.0",
"description": "",
"main": "background.js",
"directories": {
"lib": "lib"
},
"dependencies": {
"antlr4": "^4.9.2",
"arrive": "^2.4.1",
"jquery": "^3.6.0"
},
"devDependencies": {
"antlr4-webpack-loader": "^0.1.1",
"clean-webpack-plugin": "3.0.0",
"copy-webpack-plugin": "5.0.5",
"css-loader": "3.2.0",
"file-loader": "4.3.0",
"fs-extra": "8.1.0",
"html-loader": "0.5.5",
"html-webpack-plugin": "3.2.0",
"style-loader": "1.0.0",
"webpack": "4.41.2",
"webpack-cli": "3.3.10",
"webpack-dev-server": "3.9.0",
"write-file-webpack-plugin": "4.5.1"
},
"scripts": {
"build": "node utils/build.js",
"start": "node utils/webserver.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/georgeduckett/AnaplanExtension.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/georgeduckett/AnaplanExtension/issues"
},
"homepage": "https://github.com/georgeduckett/AnaplanExtension#readme"
}
I recommend to go a different route here. That webpack loader has not been updated in the last 4 years, uses a pretty old ANTLR4 jar (current version is 4.9.2) and uses the Javascript runtime, which is known for certain problems.
Instead I recommend that you switch to the antlr4ts runtime and use antlr4ts-cli to generate your files from the grammar. Both are still marked as being alpha, but I have used these packages for years already (e.g. in my vscode extension vscode-antlr4).
With that in place you can remove the webpack loader and generate the parser/lexer files as part of your build process.

Webpack 4 and Uglify Plugin (TypeError: Cannot read property 'length' of undefined)

I'm having problems with Webpack 4 on a Linux machine. The build works fine in dev mode, but fail in production. It also seems to be working on a windows machine. I did try do downgrade webpack to an older version and nothing.
Nodejs:
v10.2.1
*TypeError: Cannot read property 'length' of undefined* at node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:59
this.workers = workers === true ? _os2.default.cpus().length - 1 : Math.min(Number(workers) || 0, _os2.default.cpus().length - 1);
packge.json
{
"name": "webpack-demo",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "webpack -p"
},
"devDependencies": {},
"dependencies": {
"#types/node": "^10.5.1",
"css-loader": "^0.28.11",
"global": "^4.3.2",
"node-sass": "^4.9.1",
"npm": "^6.1.0",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"ts-loader": "^4.4.2",
"typescript": "^2.9.2",
"uglifyjs-webpack-plugin": "1.0.0-beta.2",
"webpack": "^4.15.1",
"webpack-cli": "^3.0.8"
}
}
webpack.config.js
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var webpack = require('webpack');
module.exports = {
entry: './src/index.ts',
devtool: 'source-map',
mode: 'production',
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
exclude: /node_modules/
}
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js','.css','.scss']
},
plugins: [
new UglifyJsPlugin()
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
}
}
Setting mode to production in Webpack v4 should do enough optimisations, so there's no need to specifically require the Uglify plugin. Try remove uglifyjs-webpack-plugin and there's also no need for passing the -p flag for the build script.
If you want to customise the Uglify plugin, you can also do so in Webpack's optimization config, see https://webpack.js.org/configuration/optimization/
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
//...
optimization: {
minimizer: [
new UglifyJsPlugin({ /* your config */ })
]
}
};
Finally, I have a basic webpack v4 starter boilerplate with all the latest ecosystem on Github, take a look and see if it will help you or not

How can I build/deploy an angular 2 project to production?

I have created a site using Angular 2. I started making my site using the Angular 2 QuickStart Guide as base and it's working like it should if I use the command npm start. Now that the site is finished I need to build/deploy (don't know the correct definition) to production so the site can be access to the client. The question is: how to I build this project for production? (without the need to run npm install)
The best thing I could found was to to try ng build -prod, but it says that my project is not a cli project. How to a generate the independent files to open just the index.htmlpage and access the site? Is it possible?
Update:
Maybe I was not clear: what I'm looking for is a way to get all the TypeScripts files and build it in a pure HTML/JavaScript/CSS site ready to display. No compression or minify needed at the moment. Is it possible? If not, what are other solutions (preferably independent on the host)?
So from what I understand from using Angular2 the last few weeks is what you're essentially creating is a client-side single page app so all you need to deliver is the HTML page with references to the respective JavaScript files.
I've been investigating WebPack for packaging an application (and still in the middle of getting it right), however, WebPack will combine all the compiled typescript files into a single (or multiple, still working on this part) Javascript files while also injecting the reference to the javascript files into your index.html page.
My current project compiles the typescript files (using gulp) using a web pack config to configure the output javascript file (main.ts -> main.js):
gulp.task("build-tsc", function() {
return gulp.src("entry.js")
.pipe(webpack( require("../../../webpack.config.js")))
.pipe(gulp.dest(config.dist));
});
My web pack.config.js looks like (the webpack module ignores the fact I don't have an entry.js):
var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');
module.exports = {
entry: {
"main": "./src/app/main.ts",
"share": "./src/app/share_main.ts",
"polyfills": "./src/app/polyfills.ts"
},
output: {
path: __dirname,
filename: "[name].js"
},
module: {
loaders: [
{ test: /\.css$/, loader: "style!css" },
{ test: /\.ts$/, loader: 'ts-loader' }
]
},
resolve: {
extensions: ['', '.js', '.ts']
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['main', 'share', 'polyfills']
}),
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
};
My original codebase is:
src/
index.html
assets/
styles/
main.scss
app/
app.component.ts
app.routes.ts
app.module.ts
share_main.ts
polyfills.ts
dashboard/
dashboard.component.ts
dashboard.html
navbar/
navbar.component.ts
navbar.html
So what I end up with (post build) is a new dist/ directory with the files in place for use:
dist/
index.html
main.js
share.js
polyfill.js
app/
dashboard/
dashboard.html
navbar/
navbar.html
styles/
main.css
Right now I'm using browserSync for now to serve the files for dev purposes, but you theoretically should be able to serve them up as static content via NGINX, Apache or proxy them through NGINX/Apache to lite-server.
The idea behind WebPack is that you give it an entry point (in this case main.ts) and it crawls the import statements ensuring that anything it encounters is added to the resulting Javascript files then adds them to the index.html file automatically.
Like I mentioned I've got a working app but I'm trying to understand the "multiple entry points" better but the above should work just the same.
I'm really bad at dealing with webpack and wasn't able to Anthony Ikeda solution or any other solution I found on web. If anyone get stuck in this like me, my solution was to use Angular Cli. I create a new project with angular cli, create all the pages, components and services with angular cli and copied the code from the old no-cli project to this new cli project. With a cli project, I used the ng build command to build the project and get the files to deploy.
I'm using this solution and it works perfectly for me (only html + css + js files) ... only minification give me some problems.
My webpack.config.js:
/// <binding />
var environment = (process.env.NODE_ENV || "development").trim();
if (environment === "development") {
module.exports = require('./webpack.dev.js');
} else {
module.exports = require('./webpack.prod.js');
}
My webpack.dev.js
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var CleanWebpackPlugin = require('clean-webpack-plugin');
var path = require('path');
module.exports = {
entry: {
"polyfills": "./polyfills.ts",
"vendor": "./vendor.ts",
"app": "./app/main.ts",
},
resolve: {
extensions: ['', '.ts', '.js', '.json', '.css', '.scss', '.html']
},
output: {
path: "./app_build",
filename: "js/[name]-[hash:8].bundle.js"
},
devtool: 'source-map',
module: {
loaders: [
{
loader: "babel-loader",
// Skip any files outside of your project's `src` directory
//include: [
// "app_build",
//],
exclude: [
path.resolve(__dirname, "node_modules")
],
// Only run `.js` and `.jsx` files through Babel
test: /\.js/,
// Options to configure babel with
query: {
plugins: ['transform-runtime', 'babel-plugin-transform-async-to-generator'],
presets: ['es2015', 'stage-0'],
}
},
{
test: /\.ts$/,
loader: "ts"
},
{
test: /\.html$/,
loader: "html"
},
//{
// test: /\.(png|jpg|gif|ico|woff|woff2|ttf|svg|eot)$/,
// loader: "file?name=assets/[name]-[hash:6].[ext]",
//},
{
test: /\.(png|jpg|gif|ico)$/,
//include: path.resolve(__dirname, "assets/img"),
loader: 'file?name=/assets/img/[name]-[hash:6].[ext]'
},
{
test: /\.(woff|woff2|eot|ttf|svg)$/,
// exclude: /node_modules/,
loader: 'file?name=/assets/fonts/[name].[ext]'
},
// Load css files which are required in vendor.ts
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!sass')
},
]
},
plugins: [
new ExtractTextPlugin("css/[name]-[hash:8].bundle.css", { allChunks: true }),
new webpack.optimize.CommonsChunkPlugin({
name: ["app", "vendor", "polyfills"]
}),
new CleanWebpackPlugin(
[
"./app_build/js/",
"./app_build/css/",
"./app_build/assets/",
"./app_build/index.html"
]
),
// inject in index.html
new HtmlWebpackPlugin({
template: "./index.html",
inject: "body"
}),
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
jquery: 'jquery'
})
],
devServer: {
//contentBase: path.resolve(__dirname, "app_build/"),
historyApiFallback: true,
stats: "minimal"
}
};
My webpack.prod.js:
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var CleanWebpackPlugin = require('clean-webpack-plugin');
var path = require('path');
var BabiliPlugin = require("babili-webpack-plugin");
module.exports = {
entry: {
"polyfills": "./polyfills.ts",
"vendor": "./vendor.ts",
"app": "./app/main.ts"
},
resolve: {
extensions: ['', '.ts', '.js', '.json', '.css', '.scss', '.html']
},
output: {
path: "./app_build",
filename: "js/[name]-[hash:8].bundle.min.js"
},
module: {
loaders: [
{
loader: "babel-loader",
// Skip any files outside of your project's `src` directory
//include: [
// "app_build",
//],
exclude: [
path.resolve(__dirname, "node_modules")
],
// Only run `.js` and `.jsx` files through Babel
test: /\.js/,
// Options to configure babel with
query: {
plugins: ['transform-runtime', 'babel-plugin-transform-async-to-generator'],
presets: ['es2015', 'stage-0'],
}
},
{
test: /\.ts$/,
loader: "ts"
},
{
test: /\.html$/,
loader: "html"
},
//{
// test: /\.(png|jpg|gif|ico|woff|woff2|ttf|svg|eot)$/,
// loader: "file?name=assets/[name]-[hash:6].[ext]",
//},
{
test: /\.(png|jpg|gif|ico)$/,
//include: path.resolve(__dirname, "assets/img"),
loader: 'file?name=assets/img/[name]-[hash:6].[ext]'
},
{
test: /\.(woff|woff2|eot|ttf|svg)$/,
// exclude: /node_modules/,
loader: 'file?name=/assets/fonts/[name].[ext]'
},
// Load css files which are required in vendor.ts
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!sass')
},
]
},
plugins: [
new ExtractTextPlugin("css/[name]-[hash:8].bundle.css", { allChunks: true }),
new webpack.optimize.CommonsChunkPlugin({
name: ["app", "vendor", "polyfills"]
}),
new CleanWebpackPlugin(
[
"./app_build/js/",
"./app_build/css/",
"./app_build/assets/",
"./app_build/index.html"
]
),
// inject in index.html
new HtmlWebpackPlugin({
template: "./index.html",
inject: "body"
}),
new BabiliPlugin({ comments: false }),
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
jquery: 'jquery'
})
],
devServer: {
historyApiFallback: true,
stats: "minimal"
}
};
and then my package.json (with command to build for prod):
{
"name": "dipendentistatali",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"start": "tsc && npm install && npm run build",
"watch": "SET NODE_ENV=development && webpack --watch --color",
"test": "webpack-dev-server --inline --hot",
"build": "SET NODE_ENV=development && webpack -d --color",
"buildProduction": "SET NODE_ENV=production && webpack -d --color",
"lint": "tslint ./app/**/*.ts -t verbose",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"keywords": [],
"author": "",
"dependencies": {
"#angular/common": "2.1.1",
"#angular/compiler": "2.1.1",
"#angular/core": "2.1.1",
"#angular/forms": "2.1.1",
"#angular/http": "2.1.1",
"#angular/platform-browser": "2.1.1",
"#angular/platform-browser-dynamic": "2.1.1",
"#angular/router": "3.0.0",
"#angular/upgrade": "2.0.0",
"#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.15",
"angular2-cool-http": "^1.2.3",
"angular2-toaster": "^1.0.1",
"babel-plugin-transform-async-to-generator": "^6.16.0",
"bootstrap": "^3.3.6",
"core-js": "^2.4.1",
"jquery": "2.2.4",
"ng2-bs3-modal": "^0.10.4",
"ng2-modal": "0.0.22",
"ng2-resource-rest": "^1.5.3",
"ng2-slim-loading-bar": "^2.0.4",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"zone.js": "^0.6.23"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.18.0",
"clean-webpack-plugin": "0.1.10",
"concurrently": "^2.2.0",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"html-loader": "^0.4.3",
"html-webpack-plugin": "^2.15.0",
"lite-server": "^2.2.2",
"lodash": "^4.11.1",
"node-sass": "^3.13.0",
"raw-loader": "^0.5.1",
"rimraf": "^2.5.2",
"sass-loader": "^3.2.3",
"style-loader": "^0.13.1",
"ts-loader": "^0.8.1",
"tslint": "^3.7.4",
"typescript": "^2.0.2",
"typings": "^1.3.2",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2",
"webpack-merge": "^0.9.0",
"webpack-stream": "^3.2.0"
}
}
So if you launch npm start build it build the prod version in a directory that i called app_build/
Hope it can help you

bundle.js missing from webpack build when using webpack-dev-server

I looked at similar but couldnt find a concerete answer that resolved my issue. I can't find the bundle.js file even though I am specifying where it should be outputted and everything works in the browser. I understand that the webpack-dev server is loading the files from memory and nothing is being written to disk, how I can get the file to be built and added to the dir specified in the output property in the config file?
Here is my package.json:
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "git#github.com:StephenGrider/ReduxSimpleStarter.git",
"scripts": {
"start": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js -- content-base build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"react-hot-loader": "^1.3.0",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"babel-preset-stage-1": "^6.1.18",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-redux": "^4.0.0",
"redux": "^3.0.4"
}
}
webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/index.js'
],
output: {
path: path.join(__dirname, 'assets'),
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel'
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
contentBase: './'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};
When using the dev server, the output is placed on it. So you won't actually see it amongst your files. From your index.html file you will want to load it in from the server.
For example, for my app I load in dev server, my vendor files, and then my own code.
<script src="http://localhost:8080/webpack-dev-server.js"></script>
<script src="http://localhost:8080/build/vendor.js"></script>
<script src="http://localhost:8080/build/app.js"></script>
And here is the relevant portion of my webpack config. There is some unnecessary legacy bits from when I was also loading it in from a static build bundle.
app: [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8080',
'./client/index.js'
]
},
output: {
path: __dirname + '/client/build',
publicPath: '/build/',
filename: '[name].js',
pathinfo: true
},
This Webpack plugin forces the server to write the bundle to disk.
Although I agree with Austin and lux, if you need to have the file in disk, call webpack directly.
Include below script in the webpack.config.js file
devServer: {
writeToDisk: true
}
You can also tell webpack to watch using a flag in the config. This will generate the bundle file
module.exports = {
watch: true,
};
Replace your scripts object of package.json file with the following one:
"scripts": {
"start": "npm run build",
"build": "webpack -p && ./node_modules/webpack-dev-server/bin/webpack-dev-server.js -- content-base build"
},
Then, run $ npm start

Categories

Resources