ttmt
ttmt

Reputation: 4984

Create a basic webpack setup to output css

I'm trying to create a super simple webpack set up to create the css from sass

I have the files:

index.html
entry.js
package.json
webpack.config.js
css
-app.scss

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>

<script type="text/javascript" src="bundle.js" charset="utf-8"></script>
</body>

</html>

entry.js:

require('./css/app.scss');

package.json:

{
  "name": "ls",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer-loader": "^3.2.0",
    "css-loader": "^1.0.0",
    "node-sass": "^4.9.2",
    "postcss-loader": "^2.1.6",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.21.0",
    "svg-sprite-loader": "^3.8.0",
    "ts-loader": "^4.4.2",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  }
}

webpack.config.js:

module.exports = {
    entry: "./entry.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.scss$/,
                use: [ 'style-loader','css-loader','postcss-loader' ]
            }
        ]
    }
};

app.scss:

*{
  color: red;
}

If I run npm start I get the error Error: Cannot find module 'webpack'

How do I set this up to output the css from the app.scss file

UPDATE

I installed webpack and now I'm getting the following error

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module has an unknown property 'loaders'. These properties are valid:
   object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, defaultRules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence?, strictThisContextOnImports? }
   -> Options affecting the normal modules (`NormalModuleFactory`).

Upvotes: 0

Views: 49

Answers (1)

PlayMa256
PlayMa256

Reputation: 6831

module.exports = {
    entry: "./entry.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [ 'style-loader','css-loader','postcss-loader' ]
            }
        ]
    }
};

Upvotes: 1

Related Questions