programing

webpack.config.babel에서 '가져오기'를 사용하는 중 오류 발생.js

prostudy 2022. 5. 3. 20:55
반응형

webpack.config.babel에서 '가져오기'를 사용하는 중 오류 발생.js

(function (exports, require, module, __filename, __dirname) { import path from 'path'
                                                              ^^^^^^
SyntaxError: Unexpected token import

이 오류는 사용할 때 발생한다.webpack-dev-server --hot.

읽지를 못해서 이런 일이 생기는 것 같다.import또는 웹 팩이 지원하지 않음import나는 사용하려고 노력했다.babel-register하지만 효과가 없어이 문제를 풀 다른 방법이 없겠어요?아래 코드를 참조하십시오.

webpack.config.babel.js

import path from 'path'
import webpack from 'webpack'
import HtmlPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'

const vueLoaders = {
  html: 'pug-loader',
  css: ExtractTextPlugin.extract({
    use: 'css-loader',
    fallback: 'vue-style-loader'
  }),
  scss: 'vue-style-loader!css-loader!sass-loader',
  sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}

export default {
  entry: './client/main.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },

  plugins: [
    new ExtractTextPlugin('bundle.css'),
    new HtmlPlugin({
      title: 'sex',
      template: 'client/assets/index.pug'
    })
  ],

  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: 'css-loader', fallback: 'style-loader'
        })
      }, {
        test: /\.s[a|c]ss$/,
        use: ExtractTextPlugin.extract({
          use: ['css-loader', 'sass-loader'], fallback: 'style-loader'
        })
      }, {
        test: /\.pug$/,
        loader: 'pug-loader'
      }, {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }, {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: { loaders: vueLoaders }
      }, {
        test: /\.(png|jpe?g|gif|svg|ttf|woff2?|eot)$/,
        loader: 'file-loader',
        options: { name: '[name].[ext]?[hash]' }
      }
    ]
  },

  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    }
  },

  devServer: {
    host: '0.0.0.0',
    historyApiFallback: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: '"production"' }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: { warnings: false }
    }),
    new webpack.LoaderOptionsPlugin({ minimize: true })
  ])
}

babel-register당신이 부르는 babel로 당신이 필요로 하는 모듈만 변환한다.require("babel-register");모듈 자체가 아니라.

중간 단계를 사용하여 웹 팩 구성에서 이 작업을 수행할 수 있다.

webpack.config.js

require('babel-register');
module.exports = require('./webpack.config.babel.js');

webpack.config.babel.js

import path from 'path'
import webpack from 'webpack'
import HtmlPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'

const vueLoaders = {
  html: 'pug-loader',
  css: ExtractTextPlugin.extract({
  ...

노드가 ES6를 지원하지 않음import당장의 구문공통 사용JSrequire한편의 구문

const path = require('path')
const webpack = require('webpack')
const HtmlPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')

webpack.config.js를 생성했으며 실행하려고 할 때webpack너는 실수를 초월하고 있다.웹 팩 구성 파일이 있어야 하는 이유babelified사용하기 전에

1) 이름 바꾸기webpack.config.jswebpack.config.babel.js.

2) 이제 새 파일 만들기webpack.config.js다음의 두 줄만으로

require('babel-register');
module.exports = require('./webpack.config.babel.js');

3) a를 창조해.babelrc당신의 것과 병렬로 철하다.webpack.config.js다음 내용으로 철하다우리는 바벨에게 어떤 사전 설정을 사용해야 하는지 명시적으로 말할 필요가 있다.

{
  "presets": ["latest",'react', 'es2015','stage-2']
}

4) 종속성에 다음 노드 모듈 추가(에 사용되는 사전 설정에 필요).babelrc)

npm install babel-preset-env babel-preset-es2015 babel-preset-stage-2 babel-preset-latest babel-preset-react --save-dev

5) 끝났어.이제 당신이 실행한다면webpack --progress --colors --watch그건 작동할 거야.

참조URL: https://stackoverflow.com/questions/43164716/error-in-using-import-in-webpack-config-babel-js

반응형