This is a branch off of my previous question and applied suggestions。但我仍然有重大問題。Webpack - 儘管模塊存在,但找不到模塊
我現在有我的babel transpiler,以及.babelrc
文件。我的導入代碼導入我的模塊如下所示:
var init = require('./app/js/modules/toc');
init();
但是我得到這個:
ERROR in ./app/js/script.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./app/js/modules/toc in /projects/project-root/app/js
@ ./app/js/script.js 1:11-42
的WebPack配置:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
module.exports = {
context: __dirname,
devtool: debug ? "inline-sourcemap" : null,
entry: "./app/js/script.js",
module: {
rules: [{
test: /\.js$/,
use: 'babel-loader'
}]
},
output: {
path: __dirname + "public/javascripts",
filename: "scripts.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
.babelrc
{
"presets": ["es2015"]
}
Gulptask
//scripts task, also "Uglifies" JS
gulp.task('scripts', function() {
gulp.src('app/js/script.js')
.pipe(webpack(require('./webpack.config.js')))
.pipe(gulp.dest('public/javascripts'))
.pipe(livereload());
});
我完全失去了......我做錯了什麼?
對於我的導入代碼我也試過:
import {toc} from './modules/toc'
toc();
更新:由於建議我需要解決添加到我的配置。現在看起來是這樣的:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
module.exports = {
context: __dirname,
devtool: debug ? "inline-sourcemap" : null,
entry: "./app/js/script.js",
resolve: {
extensions: ['.js']
},
module: {
rules: [{
test: /\.js$/,
use: 'babel-loader'
}]
},
output: {
path: __dirname + "public/javascripts",
filename: "scripts.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
可悲的是我仍然得到:
ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory' ./app/js/script.js in /projects/project-root
難道我的文件結構需要改變嗎?
你還沒有嘗試過我的彙總示例,對嗎? :)對不起,打擾你與另一個打包機,但它只是比你想要現在工作的所有東西更簡單。 –
我還沒有,但我也是。 Webpack變得完全荒謬。我會讓你知道它是怎麼回事! :) – kawnah