使用Webpack为Phoenix配置Materialize-loader

时间:2019-05-02 04:25:44

标签: webpack phoenix-framework materialize

我一直在尝试通过materialize-loader npm插件使用Webpack和Materialize设置新的Phoenix 1.4.2应用程序。

使用此设置,materialize-css正在运行。我可以创建一个物化下拉按钮,该按钮会复制从物化文档中的示例粘贴的内容,但是按预期,单击它不会显示“下拉列表”。我认为导入实现javascript的方式存在问题。此外,页面还在控制台中显示此错误:

TypeError: M.AutoInit is not a function

assets / package.json

{
  "repository": {},
  "license": "MIT",
  "scripts": {
    "deploy": "webpack --mode production",
    "watch": "webpack --mode development --watch"
  },
  "dependencies": {
    "phoenix": "file:../deps/phoenix",
    "phoenix_html": "file:../deps/phoenix_html"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "babel-loader": "^8.0.0",
    "copy-webpack-plugin": "^4.5.0",
    "css-loader": "^2.1.1",
    "file-loader": "^3.0.1",
    "materialize-css": "^1.0.0-rc.2",
    "materialize-loader": "^3.0.1",
    "mini-css-extract-plugin": "^0.4.0",
    "node-sass": "^4.11.0",
    "optimize-css-assets-webpack-plugin": "^4.0.0",
    "sass-loader": "^7.1.0",
    "uglifyjs-webpack-plugin": "^1.2.4",
    "url-loader": "^1.1.2",
    "webpack": "4.4.0",
    "webpack-cli": "^2.0.10"
  }
}

app.scss

@import "./node_modules/materialize-css/sass/materialize.scss";

$primary-color: color("blue-grey", "lighten-2");
$primary-color-light: lighten($primary-color, 15%);
$primary-color-dark: darken($primary-color, 15%);

body { background-color: $primary-color;}

app.js     从“ ../css/app.scss”导入CSS

import "phoenix_html"
const M = require("materialize-loader");

document.addEventListener('DOMContentLoaded', function() {
    M.AutoInit();
  });

webpack.config.js

const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = (env, options) => ({
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  entry: {
      './js/app.js': ['./js/app.js'].concat(glob.sync('./vendor/**/*.js'))
  },
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../priv/static/js')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.scss$/,
        use: [
           MiniCssExtractPlugin.loader,
           {
             loader: 'css-loader',
             options: {}
           },
           {
             loader: 'sass-loader',
             options: {}
           }
        ]
      },
      { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff' },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader' }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({ filename: '../css/app.css' }),
    new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
  ]
});

0 个答案:

没有答案