代码之家  ›  专栏  ›  技术社区  ›  ttmt

创建基本的Webpack设置以输出CSS

  •  0
  • ttmt  · 技术社区  · 6 年前

    我正在尝试创建一个超级简单的Webpack,它设置为从sass创建css。

    我有文件:

    index.html文件
    入口.js
    包.json
    web包.config.js
    CSS
    -附录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>
    

    条目.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' ]
                }
            ]
        }
    };
    

    附录SCSS:

    *{
      color: red;
    }
    

    如果我跑步 npm start 我得到错误 Error: Cannot find module 'webpack'

    如何设置此项以从app.scss文件输出CSS

    更新

    我安装了Webpack,现在出现以下错误

    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`).
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   PlayMa256    6 年前
    module.exports = {
        entry: "./entry.js",
        output: {
            path: __dirname,
            filename: "bundle.js"
        },
        module: {
            rules: [
                {
                    test: /\.scss$/,
                    use: [ 'style-loader','css-loader','postcss-loader' ]
                }
            ]
        }
    };