Webpack 4 – 简单的 js 功能在打包文件后不起作用 [英] Webpack 4 – simple js function not working after bundled files
问题描述
我对 webpack 比较陌生.我试图让一个简单的架构工作,但似乎无法找出问题所在.我将尝试总结我的代码:
I’m relatively new to webpack. I’m trying to make a simple architecture work, but can’t seem to find out what’s wrong. I’m going to try to summarize my code:
package.json 文件:
package.json file:
"devDependencies": {
"@babel/preset-env": "^7.8.3",
"babel-loader": "^8.0.6",
"babel-preset-es2015": "^6.24.1",
"bootstrap": "^4.4.1",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.4.2",
"file-loader": "^5.0.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
"jquery": "^3.4.1",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.13.1",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"popper.js": "^1.16.1",
"sass-loader": "^8.0.2",
"style-loader": "^1.1.3",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1",
"webpack-merge": "^4.2.2"
}
webpack.config.js 文件:
webpack.config.js file:
"use strict";
const path = require("path");
const merge = require("webpack-merge");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
mode: "production",
devtool: "none",
entry: {
"backend":"./src/backend.src.js"
},
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist")
},
optimization: {
minimize: true,
concatenateModules: true,
namedModules: true,
minimizer: [
new TerserPlugin(),
new OptimizeCssAssetsPlugin()
]
},
plugins: [
new HtmlWebpackPlugin({
minify: {
collapseWhitespace: true,
removeComments: true
}
}),
new MiniCssExtractPlugin({filename: "styles-[name].bundle.css"}),
new CleanWebpackPlugin()
],
module: {
rules: [
//CSS.
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
},
//SASS.
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
//JS (transpile to es5)
{
test: /\.js$/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
}
}
]
},
//HTML
{
test: /\.html$/,
use: [
"html-loader"
]
},
//Files
{
test: /\.(svg|png|jpg|jpeg|gif)$/,
use: {
loader: "file-loader",
options: {
name: "[name].[hash].[ext]",
outputPath: "imgs",
esModule: false
}
}
}
]
}
};
backend.src.js 文件:
backend.src.js file:
"use strict";
import '../app_styles/styles-backend.css';
import { webpackDebugTest } from '../app_js/functions-syncsystem.js';
functions-syncsystem.js 文件:
functions-syncsystem.js file:
"use strict";
export function webpackDebugTest()
{
alert("Webpack test - inside function");
}
index.html 文件(简单的纯 html,我在其中导入捆绑文件以进行测试):
index.html file (simple, plain html where i´m importing the the bundled files in order to test):
(simple html)
<script>
webpackDebugTest();
</script>
(simple html)
当我运行索引时,css 文件加载正常,js 文件也加载正常,但它似乎无法识别我为测试架构而创建的简单 js 函数.控制台给了我这样的错误消息:
When I run the index, the css file loads fine, the js file also loads fine, but it doesn´t seem to recognize the simple js function I created in order to test the architecture. Console gives me an error message like this:
未捕获的引用错误:未定义 webpackDebugTest
Uncaught ReferenceError: webpackDebugTest is not defined
我还尝试将 functions-syncsystem.js 文件更改为以下内容:
I’ve also tried changing the functions-syncsystem.js file to the following:
"use strict";
function webpackDebugTest()
{
alert("Webpack test - inside function");
}
module.exports = {
webpackDebugTest: webpackDebugTest
};
并且在捆绑文件中,我可以找到以下代码:e.exports={webpackDebugTest:function(){alert("Webpack test - inside function")}
And in the bundled file, I can find the following code: e.exports={webpackDebugTest:function(){alert("Webpack test - inside function")}
它仍然在控制台面板上返回相同的错误消息.
And it still returns me that same error message on the console panel.
任何人都可以发现我的使用有什么问题?
Anyone can detect what could be the issue with my usage?
推荐答案
entry: {
backend:"./src/backend.src.js"
},
为了使用import",你需要安装这个
in order to use "import" you need to install this
npm i @babel/register --save
在您的入口点文件之上
require("@babel/register")
这篇关于Webpack 4 – 简单的 js 功能在打包文件后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!