Not able to extract css using Webpack2 - webpack-2

I am trying to extract all the css into one file, I am using webpack2 and i have extract-text-webpack-plugin#2.0.0-rc.0, the following is my code
webpack
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: './app/main.ts',
output: {
path: './dist',
filename: 'app.bundle.js'
},
module: {
rules: [
{ test: /\.ts$/, exclude: /node_modules/, use: ['ts-loader'] },
{ test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader', publicPath: "./dist" }) }
]
},
resolve: {
extensions: ['.js', '.ts']
},
plugins: [
new HtmlWebpackPlugin({
template: './index.html',
minify: {
collapseWhitespace: true
},
}),
new ExtractTextPlugin({
filename: 'style.css',
disabled: false,
allChunks: true
})
]
};
package.json
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"css-loader": "^0.28.0",
"extract-text-webpack-plugin": "^2.0.0-rc.0",
"html-webpack-plugin": "^2.28.0",
"style-loader": "^0.16.1",
"ts-loader": "^2.0.3",
"tslint": "^3.15.1",
"typescript": "~2.0.10",
"typings": "^2.1.1",
"webpack": "2.2.1"
},
"repository": {}
}
main.ts
require('../app/content/custom.css');
import 'core-js';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import { enableProdMode } from '#angular/core'
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
when i try to run the webpack, i get the following error
I was following this tutorial for building webpack webpack tutorial

First you can safely remove the const webpack = require('webpack') from your webpack.config.js, it's not needed :)
Could you try to without publicPath: './dist' && use: ['css-loader'] instead of use: 'css-loader' in ExtractTextPlugin.extract() ?

Related

My web app built with react doesn't run on safari browser

I've built and pushed my minified code to a site to test on various devices and it works on windows desktop, various android devices that I've tested with but when I go to iOS devices my code doesn't run and displays like this. (I realize now that every browser on iOS is basically safari and that this is a safari issue)
Here's the link for the isolated code if you want to load it up on your own
https://app.gohighlevel.com/v2/preview/6fAe9cXYdS11V8TSUgIL?notrack=true
I thought maybe I need to change my babelrc to include more devices but I feel like it has something to do with my overall webpack build config I'm just at a loss as to why.
Here are what my configs look like
webpack.config.common.js
const paths = require('./paths');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const imageInlineSizeLimit = parseInt(
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
);
module.exports = {
entry: ['core-js/stable', 'regenerator-runtime/runtime', paths.appIndexJs],
output: {
filename: '[name].[contenthash].js',
path: paths.appBuild,
publicPath: './'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', 'scss']
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebPackPlugin({
filename: 'index.html',
inject: true,
template: paths.appHtml
}),
new ESLintPlugin({
formatter: eslintFormatter,
eslintPath: 'eslint',
resolvePluginsRelativeTo: __dirname
})
],
module: {
rules: [
{
test: /\.(js|ts)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: imageInlineSizeLimit,
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
loader: require.resolve('file-loader'),
exclude: [/\.(js|mjs|jsx|ts|tsx|scss)$/, /\.html$/, /\.json$/],
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
}
]
}
}
webpack.config.prod.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
const TerserPlugin = require('terser-webpack-plugin');
//const postcssNormalize = require('postcss-normalize');
const commonConfig= require('./webpack.config.common');
const { merge } = require('webpack-merge');
module.exports = merge(commonConfig, {
mode: 'production',
devtool: 'source-map',
output: {
publicPath: './'
},
module: {
rules: [
{
test:/\.(scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2,
modules: {
getLocalIdent: getCSSModuleLocalIdent
}
}
},
'resolve-url-loader',
'sass-loader'
],
sideEffects: true
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css'
})
],
optimization: {
minimizer: [
new OptimizeCssAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true
}
}
}),
new TerserPlugin({
terserOptions: {
sourceMap: true
},
parallel: true
})
],
}
})
babelrc
{
"presets": [
"#babel/preset-typescript",
[
"#babel/preset-env",
{
"targets": {
"browsers": [
"> 0.1%",
"ios >= 6"
]
},
"useBuiltIns": "usage",
"corejs": 3
}
],
"#babel/preset-react"
],
"plugins": [
"react-hot-loader/babel",
"#babel/plugin-proposal-class-properties",
"#babel/plugin-transform-runtime"
]
}
package.json
//....package.json details
"devDependencies": {
"#babel/core": "^7.20.2",
"#babel/plugin-transform-runtime": "^7.19.6",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"#babel/preset-typescript": "^7.18.6",
"#types/react": "^18.0.25",
"#types/react-dom": "^18.0.9",
"babel-loader": "^9.1.0",
"babel-plugin-import": "^1.13.5",
"clean-webpack-plugin": "^4.0.0",
"core-js": "^3.26.1",
"css-loader": "^6.7.2",
"eslint": "^8.28.0",
"eslint-config-react-app": "^7.0.1",
"eslint-loader": "^4.0.2",
"eslint-webpack-plugin": "^3.2.0",
"file-loader": "^6.2.0",
"gulp": "^4.0.2",
"gulp-inline-source": "^4.0.0",
"gulp-replace": "^1.1.3",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.7.0",
"optimize-css-assets-webpack-plugin": "^6.0.1",
"react-dev-utils": "^12.0.1",
"react-hot-loader": "^4.13.1",
"regenerator-runtime": "^0.13.11",
"resolve-url-loader": "^5.0.0",
"sass-loader": "^13.2.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.6",
"url-loader": "^4.1.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0",
"webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"axios": "^1.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.56.1",
"swr": "^1.3.0"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
"> 0.1%",
"ios >= 6"
]
}
So Like I said in my comment the issue ended up being my terser minifier. The version I posted is the latest version as of this post and apparently will not work on safari once minified as when I built the code without the minfier it worked fine on safari.
The version of Terser plugin I rolled back to is "terser-webpack-plugin": "4.2.3".
There maybe other version that works but this is the version that worked for me and fixed my issue

Get css files when compiling using #nrwl/web:package

I need to get css files for the build version of a library using #nrwl/web:package, the library that I'm creating doesn't have an index.html, it's just plain TS, but when I try to compile using web:package, CSS files are not shown, in the build for this library I have:
{
"build": {
"builder": "#nrwl/web:package",
"options": {
"outputPath": "libs/data-table/dist",
"tsConfig": "libs/data-table/tsconfig.lib.json",
"project": "libs/data-table/package.json",
"entryFile": "libs/data-table/src/index.tsx",
"external": [ "react", "react-dom" ],
"styles": [ "libs/data-table/src/lib/index.css", "libs/data-table/src/lib/responsive.scss" ],
"webpackConfig": "libs/data-table/webpack.config.js",
"assets": [
{
"glob": "README.md",
"input": ".",
"output": "."
}
]
},
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
}
}
And in webpack.config.js I have:
const getConfig = require('#nrwl/react/plugins/babel');
const cssModuleRegex = /\.module\.css$/;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = (config) => {
config = getConfig(config);
config.module.rules.forEach((rule, idx) => {
// Find rule tests for CSS.
// Then make sure it excludes .module.css files.
if (rule.test.test('foo.css')) {
rule.exclude = rule.exclude
? Array.isArray(rule.exclude)
? [...rule.exclude, cssModuleRegex]
: [rule.exclude, cssModuleRegex]
: cssModuleRegex
}
});
// Add new rule to handle .module.css files by using css-loader
// with modules on.
config.module.rules.push({
test: /\.module\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-modules-typescript-loader' },
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1
}
}
]
});
config.module.rules.push({
test: /\.module\.(scss|sass)$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1
}
}
]
});
return config;
}
But for some reason it seems that is not read.

How to add scss loader in webpacker angular rails?

I'm trying to study rails with angular but I stock with implementing scss-loader. I already search this in google. I already add what they are trying to do, but unfortunately nothing happen and I always got this error
Invalid CSS after "": expected 1 selector or at-rule, was "var content = requi".
I add scss.d.ts
declare module "*.scss" {
const content: string
export default content
}
and here is my environment.js
const { environment } = require('#rails/webpacker')
const typescript = require('./loaders/typescript')
environment.loaders.prepend('typescript', typescript)
environment.loaders.prepend('html', {
test: /\.html$/,
use: [{
loader: 'html-loader',
options: {
minimize: true,
removeAttributeQuotes: false,
caseSensitive: true,
customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
customAttrAssign: [ /\)?\]?=/ ]
}
}]
})
environment.loaders.prepend('style', {
test: /\.(scss|sass|css)$/,
use: [{
loader: "to-string-loader"
}, {
loader: "css-loader"
}, {
loader: "resolve-url-loader"
}, {
loader: "sass-loader"
}]
})
module.exports = environment
even add a stylesheet_pack_tag
<%= stylesheet_pack_tag 'application' %>
<div>
<default-selector></default-selector>
</div>
<%= javascript_pack_tag 'application' %>
I got error. Does my version of my sass-loader matters here?
{
"name": "acbook_angular",
"private": true,
"dependencies": {
"#angular/common": "^7.2.8",
"#angular/compiler": "^7.2.8",
"#angular/core": "^7.2.8",
"#angular/platform-browser": "^7.2.8",
"#angular/platform-browser-dynamic": "^7.2.8",
"#angular/router": "^7.2.10",
"#rails/webpacker": "^4.0.2",
"core-js": "^2.6.5",
"html-loader": "^0.5.5",
"rxjs": "^6.4.0",
"ts-loader": "^5.3.3",
"typescript": "^3.3.3333",
"zone.js": "^0.8.29"
},
"devDependencies": {
"css-loader": "^2.1.1",
"resolve-url-loader": "^3.0.1",
"sass-loader": "^7.1.0",
"to-string-loader": "^1.1.5",
"webpack-dev-server": "^3.2.1",
"webpack-merge": "^4.2.1"
}
}
I already solve it by replacing my environment
environment.loaders.prepend('style', {
test: /\.(scss|sass|css)$/,
use: [{
loader: "to-string-loader"
}, {
loader: "css-loader"
}, {
loader: "resolve-url-loader"
}, {
loader: "sass-loader"
}]
})
to this
environment.loaders.insert('sass', {
test: /\.scss$/,
use: [
"to-string-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS
]
});
and thanks for this link

Auto Refresh Page (React With Ruby On Rails)

client folder where the react code sitsI am Using react with ruby on rails, I have created a separate folder named client for react code on the root. but whenever I changed something in the react i have to go to the page and refresh it manually, is there any way where I can automatically refresh the page whenever I can make any change in the code?
If you are using create-react-app then it should be available through that package. If you are building your own environment with Webpack, you can use Hot Module Replacement. https://webpack.js.org/guides/hot-module-replacement/
Edit:
Here are some example files from a project I made containing relevant code.
Webpack
const webpack = require('webpack');
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
resolve(__dirname, "src") + "/index.jsx"
],
output: {
filename: 'app.bundle.js',
path: resolve(__dirname, 'build'),
publicPath: '/'
},
resolve: {
extensions: ['.js', '.jsx']
},
devtool: '#source-map',
devServer: {
hot: true,
contentBase: resolve(__dirname, 'build'),
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
enforce: "pre",
loader: "eslint-loader",
exclude: /node_modules/,
options: {
emitWarning: true,
configFile: "./.eslintrc.json"
}
},
{
test: /\.(png|gif|jp(e*)g|svg)$/,
use: {
loader: 'url-loader',
options: {
limit: 8000,
name: 'images/[hash]-[name].[ext]'
}
}
},
{
test: /\.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/,
options: {
presets: [
["es2015", {"modules": false}],
"react"
],
plugins: [
"react-hot-loader/babel",
"styled-jsx/babel"
]
}
},
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
template:'template.ejs',
appMountId: 'react-app-root',
title: 'Project Name',
filename: resolve(__dirname, "build", "index.html"),
}),
]
};
Mainly, the entry, devServer, module.plugins and plugins at the bottom have the hot reloading code.
package.json
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server",
"lint": "eslint src/index.jsx src/**/*.jsx; exit 0",
"lint-fix": "eslint src/index.jsx src/**/*.jsx --fix; exit 0"
},
"repository": {
"type": "git",
"url": "url"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "url"
},
"homepage": "relevant-url",
"dependencies": {
"eslint": "^5.0.1",
"eslint-plugin-react": "^7.10.0",
"file-loader": "^1.1.6",
"npm": "^6.1.0",
"prop-types": "^15.5.10",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router-dom": "^4.0.0",
"styled-jsx": "^2.2.7",
"url-loader": "^0.6.2"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"eslint-loader": "^2.0.0",
"html-webpack-plugin": "^2.29.0",
"react-hot-loader": "^3.0.0-beta.7",
"webpack": "^3.4.0",
"webpack-dev-server": "^2.5.0"
}
}
The hot reloading packages in the devDependencies
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { AppContainer } from 'react-hot-loader';
import { HashRouter } from 'react-router-dom';
const render = (Component) => {
ReactDOM.render(
<AppContainer>
<HashRouter>
<Component/>
</HashRouter>
</AppContainer>,
document.getElementById('react-app-root')
);
};
render(App);
/*eslint-disable */
if (module.hot) {
module.hot.accept('./components/App', () => {
render(App);
});
}
/*eslint-enable */
Notice the AppContainer component imported from react-hot-loader that wraps your app in ReactDOM.render. Also, sometimes eslint can interfere with hot reloading which is why it is disabled and then enabled again at the bottom of the file.
I hope this helps.

My React + Webpack + Rails + Bootstrap suddently erroring

About a week ago my app was deploying fine to production, however now I am seeing
Uncaught Error: Cannot find module "./lib/bootstrap.loader!./no-op.js"
var path = require('path');
var webpack = require('webpack');
var config = module.exports = {
entry: [
'bootstrap-loader',
'./app/frontend/javascripts/main.js'
],
stats: {
// Configure the console output
colors: true,
modules: true,
reasons: true
},
progress: true,
keepalive: true,
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader', // 'babel-loader' is also a legal name to reference
query: {
presets: ['es2015', 'react']
}
}, {
test: /\.css$/,
loader: 'style!css'
},
{ test: /bootstrap-sass\/assets\/javascripts\//, loader: 'imports?jQuery=jquery' },
{ test: /\.png$/, loader: "url-loader?limit=100000" },
{ test: /\.jpg$/, loader: "file-loader" },
{ test: /\.woff$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
{ test: /\.(woff2?|svg)$/, loader: 'url?limit=10000' },
{ test: /\.(ttf|eot)$/, loader: 'file' }
]
},
output: {
path: './app/assets/javascripts/',
filename: 'bundle.js',
publicPath: '/assets/',
devtoolModuleFilenameTemplate: '[resourcePath]',
devtoolFallbackModuleFilenameTemplate: '[resourcePath]?[hash]',
},
resolve: {
extensions: ['', '.js', '.jsx'],
modulesDirectories: [ 'node_modules', 'bower_components' ],
},
plugins: [
new webpack.ResolverPlugin([
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin('.bower.json', ['main'])
]),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
//new webpack.optimize.CommonsChunkPlugin('common-bundle.js'),
//new webpack.optimize.CommonsChunkPlugin('public-bundle.js')
]
};
My package.json
"devDependencies": {
"babel-core": "~6.4.0",
"babel-loader": "~6.2.1",
"babel-preset-es2015": "~6.3.13",
"babel-preset-react": "~6.3.13",
"exports-loader": "~0.6.2",
"expose-loader": "~0.6.0",
"grunt": "~0.4.5",
"grunt-babel": "~6.0.0",
"grunt-cli": "~0.1.13",
"grunt-contrib-watch": "~0.6.1",
"grunt-webpack": "~1.0.11",
"history": "~1.17.0",
"imports-loader": "~0.6.3",
"jquery": "~2.1.4",
"lodash": "~3.0.0",
"react": "~0.14.6",
"react-dom": "~0.14.6",
"webpack": "~1.12.10",
"webpack-dev-server": "~1.14.0"
},
"dependencies": {
"autoresponsive-react": "^1.1.10",
"bluebird": "~3.1.1",
"bootstrap": "~3.3.6",
"bootstrap-loader": "~1.0.3",
"bootstrap-sass": "~3.3.6",
"bootstrap-webpack": "0.0.5",
"chunk-manifest-webpack-plugin": "0.0.1",
"classnames": "^2.2.3",
"css-loader": "~0.23.1",
"extract-text-webpack-plugin": "~1.0.1",
"file-loader": "~0.8.5",
"grunt-react": "~0.12.3",
"less": "~2.5.3",
"less-loader": "~2.2.2",
"node-sass": "~3.4.2",
"react-bootstrap": "~0.28.1",
"react-dropzone": "~3.3.2",
"react-grid-layout": "^0.10.0-beta1",
"react-image-component": "^1.0.0",
"react-input-autosize": "^0.6.6",
"react-modal-dialog": "^2.0.0",
"react-responsive": "^1.1.0",
"react-router": "~2.0.0-rc4",
"react-router-component": "~0.29.0",
"react-select": "^0.9.1",
"react-spinjs": "^2.0.1",
"react-swipe": "^3.0.0",
"reflux": "~0.3.0",
"reflux-core": "^0.3.0",
"reflux-promise": "^1.0.2",
"resolve-url-loader": "~1.4.3",
"sass-loader": "~3.1.2",
"style-loader": "~0.13.0",
"superagent": "~1.6.1",
"superagent-bluebird-promise": "~3.0.0",
"url-loader": "~0.5.7"
}
}
When I deploy I am receiving this error. When I look at bootstrap-loader I don't know what is happening, if someone knows that can point me in the right direction I am most greatful.
Here is similar error https://github.com/shakacode/bootstrap-loader/issues/4
They resolve this by ensuring that latest version of bootstrap-loader is installed:
npm install --save bootstrap-loader#1.0.0-rc
or
npm install --save bootstrap-loader#latest

Resources