justinribeiro / lite-youtube

The fastest little YouTube web component on this side of the internet. The shadow dom web component version of Paul's lite-youtube-embed.
https://www.npmjs.com/package/@justinribeiro/lite-youtube
MIT License
966 stars 70 forks source link

Module parse failed: Unexpected token ... #47

Closed lucrousseau closed 2 years ago

lucrousseau commented 2 years ago

Hi, i have this issue, maybe you can help ? Thanks!

ERROR in ./node_modules/@justinribeiro/lite-youtube/lite-youtube.js 224:23
Module parse failed: Unexpected token (224:23)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|         this.domRefImg.fallback.src = posterUrlJpeg;
|         this.domRefImg.fallback.setAttribute('aria-label', `${this.videoPlay}: ${this.videoTitle}`);
>         this.domRefImg?.fallback?.setAttribute('alt', `${this.videoPlay}: ${this.videoTitle}`);
|     }
|     initIntersectionObserver() {
 @ ./src/js/main.js 3:0-37
 @ ./src/index.js
package.json :

{
  "name": "nesto-2022",
  "version": "2.0.0",
  "description": "Custom WordPress Theme",
  "main": "index.js",
  "author": "Strong Developpement",
  "license": "UNLICENSED",
  "private": true,
  "devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/runtime": "^7.9.2",
    "autoprefixer": "^9.7.5",
    "babel-loader": "^8.1.0",
    "babel-plugin-import": "^1.13.3",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "browser-sync": "^2.26.7",
    "browser-sync-webpack-plugin": "^2.2.2",
    "clean-webpack-plugin": "^3.0.0",
    "compression-webpack-plugin": "6.1.1",
    "core-js": "^3.8.0",
    "css-loader": "^3.4.2",
    "file-loader": "^6.0.0",
    "glob": "^7.1.7",
    "html-loader": "^1.1.0",
    "image-minimizer-webpack-plugin": "^1.0.0",
    "imagemin-gifsicle": "^7.0.0",
    "imagemin-jpegtran": "^6.0.0",
    "imagemin-optipng": "^8.0.0",
    "imagemin-pngquant": "^8.0.0",
    "imagemin-svgo": "^7.1.0",
    "imagemin-webp": "^6.0.0",
    "imagemin-webpack-plugin": "^2.4.2",
    "mini-css-extract-plugin": "^0.9.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "postcss-loader": "^3.0.0",
    "sass": "^1.26.3",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.1.3",
    "terser-webpack-plugin": "^2.3.5",
    "url-loader": "^4.0.0",
    "webpack": "^4.42.0",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^3.10.3",
    "webpack-notifier": "^1.8.0"
  },
  "scripts": {
    "start": "yarn dev",
    "dev": "webpack --mode development --progress --watch",
    "build": "webpack --mode production"
  },
  "dependencies": {
    "@justinribeiro/lite-youtube": "^1.3.1"
  },
  "browserslist": [
    "defaults"
  ]
}

webpack.config.js

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const ImageminPlugin = require('image-minimizer-webpack-plugin');
const autoprefixer = require('autoprefixer');
const WebpackNotifierPlugin = require('webpack-notifier');

module.exports = (env, argv) => {
  let devmode = argv.mode === 'development';
  let babelPlugins = [
    '@babel/plugin-transform-runtime',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties'
  ];

  if(!devmode) babelPlugins.push(['transform-remove-console', { 'exclude': [ 'error', 'warn', 'info' ] }]);

  return {
    mode: argv.mode,
    entry: './src',
    output: {
      filename: '[name].js',
      chunkFilename: `[name].chunk.js`,
      path: path.resolve(__dirname, 'dist'),
      publicPath : '/wp-content/themes/nesto2022/dist/'
    },
    devtool: devmode ? 'source-map' : false,
    performance : {
      hints : devmode ? false : "warning"
    },        
    plugins: [
      new CleanWebpackPlugin(),
      new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[name].chunk.css'
      }),
      new ImageminPlugin({
        minimizerOptions: {
          plugins: [
            ['gifsicle', { interlaced: true }],
            ['jpegtran', { progressive: true }],
            ['optipng', { optimizationLevel: 3 }],
            [
              'svgo',
              {
                plugins: [
                  { optimizationLevel: 3 },
                  { progessive: true },
                  { interlaced: true },
                  { removeViewBox: false },
                  { removeUselessStrokeAndFill: true },
                  { cleanupIDs: true },
                  { minifyStyles: true },
                  { inlineStyles: true }
                ]
              }
            ]
          ]
        }
      }),
      new BrowserSyncPlugin( {
        open: 'external',
        host: 'devnesto.com',
        proxy: 'devnesto.com',
        port: 3000,
        files: [
          'src/js/',
          'images/**/*',
          './**/*.css',
          './**/*.php'
        ],
        },
        // plugin options
        {
          // prevent BrowserSync from reloading the page
          // and let Webpack Dev Server take care of this
          reload: false,
          injectCss: true
        }
      ),
      new WebpackNotifierPlugin(),
    ],
    module: {
      rules: [ 
        {
          test: /\.js$/,
          exclude: /(node_modules)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                [ '@babel/preset-env', {
                    useBuiltIns: 'entry',
                    corejs: 3,
                    debug: devmode
                } ]
              ],
              'plugins': babelPlugins
            }
          } 
        },
        {
          test: /\.(sa|sc|c)ss$/,      
          use: [
              MiniCssExtractPlugin.loader,
              'css-loader', {
                loader: 'postcss-loader',
                options: {
                  ident: 'postcss',
                  plugins: () => [autoprefixer()]
                }
              },
              'sass-loader'
          ]
        },
        {
          test: /\.(jpe?g|png|gif|svg)$/i,
          exclude: /fonts/,
          use: [
            {
              loader: 'url-loader',
              options: {
                  limit: 5120,
                  name: 'images/[name].[ext]'
              }  
            }
          ]
        },
        {
          test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
          exclude: /images/,
          use: [
            {
              loader: 'url-loader',
              options: {
                  limit: 5120,
                  name: 'fonts/[name].[ext]'
              }  
            }
          ]
        }
      ]
    },
    optimization: {
      minimizer: [
        new TerserPlugin(),
        new OptimizeCSSAssetsPlugin()
      ]
    }
  }
};
justinribeiro commented 2 years ago

Answer is in your webpack output:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

Specifically, webpack 4.x for has problems with optional chaining (the line it's pointing to in your output) because of the acorn dependency. You can either use the workaround in this thread or move to webpack 5. https://github.com/webpack/webpack/issues/10227