做一些简单的Webpack学习。遵循本教程:https://www.valentinog.com/blog/webpack-tutorial/
当前只是通过运行npm run build来尝试将.src / index.js构建为./dist/main.js
./ src文件夹中除了index.js之外没有其他内容,。/ src / index.js的全部内容都是
window.console.log('hello world');
然后我在控制台中收到此错误。
Hash: 61965fd874c7fad84f98
Version: webpack 4.19.0
Time: 76ms
Built at: 09/16/2018 4:37:31 PM
1 asset
Entrypoint main = main.js
[0] ./src/index.js 177 bytes {0} [built] [failed] [1 error]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production'
for this value. Set 'mode' option to 'development' or 'production' to enable
defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more:
https://webpack.js.org/concepts/mode/
ERROR in ./src/index.js 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
这不会建立捆绑包。
但是,当./src/index.js的内容为空且完全为空时,它会很好地构建。
main.js在上述条件下构建时的内容:
!function(e){var t = {}; function r(n){if(t [n])return t [n] .exports; var o = t [n] = {i:n,l :!1,exports:{}};返回e [n] .call(o.exports,o,o.exports,r),ol =!0,o.exports} rm = e,rc = t,rd = function(e,t,n){ro(e,t)|| Object.defineProperty(e,t,{enumerable:!0,get:n})},rr = function(e){“ undefined”!= typeof Symbol && Symbol.toStringTag && Object.defineProperty(e,Symbol.toStringTag,{value:“ Module”}),Object.defineProperty(e,“ __ esModule”,{value:!0})},rt = function(e,t){ if(1&t &&(e = r(e)),8&t)返回e; if(4&t &&“ object” == e && e && e .__ esModule)的类型返回e; var n = Object.create(null); if(rr(n), Object.defineProperty(n,“ default”,{enumerable:!0,value:e}),2&t &&“ string”!= typeof e)for(var o in e)rd(n,o,function(t){return e [t]}。bind(null,o));返回n},rn = function(e){var t = e && e .__ esModule?function(){return e.default}:function(){return e};返回rd(t,“ a”,t),t},ro = function(e,t){返回Object.prototype.hasOwnProperty.call(e,t)},rp =“”,r(rs = 0) }([function(e,t){}]);
我现在有点困惑,对于我来说,webpack不需要为常规js使用特殊的加载器。
其他信息
节点-v:10.4.1(由nvm管理)
npm -v:6.1.0
Webpack:^ 4.19.0
webpack-cli:^ 3.1.0
答案 0 :(得分:2)
错误似乎是评论中提到的字节顺序标记(bom)Phil。当我尝试此修复程序时解决:在index.js上https://unix.stackexchange.com/questions/381230/how-can-i-remove-the-bom-from-a-utf-8-file,然后重新构建。