2016-05-04 27 views
8

Webpack構建成功,我可以瀏覽到我的網頁。但是,Javascript失敗,說:'進口聲明可能只出現在模塊的頂層'在webpack中,如何解決'導入聲明可能只出現在模塊的頂層?'

下面是我輸出的包含import語句的app.js。

如何更改我的webpack配置文件以在構建時擺脫導入語句?

webpackJsonp([0],{ 

/***/ 0: 
/***/ function(module, exports, __webpack_require__) { 

    __webpack_require__(1); 
    __webpack_require__(74); 
    module.exports = __webpack_require__(76); 


/***/ }, 

/***/ 76: 
/***/ function(module, exports) { 

    "use strict"; 

    import 'app/tools/typescriptImports.ts'; 
    import * as mainScreenHelper from 'app/tools/mainScreenHelper'; 
    import React from 'react'; 
    import * as reactDom from 'react-dom'; 
    import Router from 'react-router'; 
    import createBrowserHistory from 'history/lib/createBrowserHistory'; 
    import routes from 'app/tools/routes'; 
    import 'style/app.scss'; 
    import 'font-awesome/scss/font-awesome.scss'; 

    mainScreenHelper.removeLoadingScreen(); 
    mainScreenHelper.createReactApp(); 

/***/ } 

}); 
//# sourceMappingURL=app.js.map 

這是我目前的配置文件:

'use strict'; 

//https://webpack.github.io/docs/configuration.html 

var webpack = require('webpack'); 
var HtmlWebpackPlugin = require('html-webpack-plugin'); 
var CopyWebpackPlugin = require('copy-webpack-plugin'); 
var path = require('path'); 
var rootPath = __dirname; //ekaya 
var srcPath = path.join(rootPath, 'src/client'); //ekaya/src/client 
var distPath = path.join(rootPath, 'dist/client'); //ekaya/dist/client 

module.exports = 
{ 
    bail: true, 
    cache: true, 
    context: rootPath, 
    debug: true, 
    devtool: 'source-map', //inline-source-map, https://webpack.github.io/docs/configuration.html#devtool 
    target: 'web', //node, web 
    devServer: 
    { 
     contentBase: distPath, 
     historyApiFallback: true, 
     outputPath: path.join(distPath, 'devServer') 
    }, 
    entry: 
    { 
     app: path.join(srcPath, 'app/home.jsx'), 
     lib: ['react', 'react-router', 'react-dom', 'jquery', 'lodash', 'history'] 
    }, 
    output: 
    { 
     path: distPath, 
     publicPath: '', 
     filename: '[name].js', 
     pathInfo: true 
    }, 
    resolve: 
    { 
     root: srcPath, 
     extensions: ['', '.js', '.jsx', '.ts', '.tsx'], 
     modulesDirectories: ['node_modules', srcPath] 
    }, 
    module: 
    { 
     loaders: 
     [ 
      {test: /\.js$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules)/ }, 
      {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules)/ }, 
      {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules)/ }, 
      {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules)/ }, 
      {test: /\.scss$/, loaders: ['style', 'css', 'sass']}, 
      {test: /\.png$/, loader: 'file-loader'}, 
      {test: /\.jpg$/, loader: 'file-loader'}, 
      {test: /\.jpeg$/, loader: 'file-loader'}, 
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'}, 
      {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"}, 
      {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"}, 
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"}, 
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"} 
     ] 
    }, 
    plugins: 
    [ 
     new CopyWebpackPlugin 
     ([ 
      { from: path.join(srcPath, 'images'), to: 'images' } 
     ]), 
     new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js'), 
     new HtmlWebpackPlugin 
     ({ 
      inject: true, 
      template: path.join(srcPath, 'index.html') 
     }), 
     new webpack.NoErrorsPlugin() 
    ] 
}; 

我tsconfig.json

{ 
    "buildOnSave": false, 
    "compileOnSave": false, 
    "compilerOptions": 
    { 
     "allowJs": true, 
     "jsx": "react", 
     "noImplicitAny": true, 
     "module": "commonjs", 
     "outDir": "dist/client/ts", 
     "removeComments": true, 
     "sourceMap": false, 
     "target": "es5" 
    }, 
    "exclude": 
    [ 
     "node_modules", 
     "dist" 
    ] 
} 
+0

你可以添加你的tsconfig.json? –

+0

babel-eslint。 https://stackoverflow.com/questions/39158552/ignore-eslint-error-import-and-export-may-only-appear-at-the-top-level –

回答

1

好吧,我得到這個工作弄好了,真的不知道哪部分做了,但這裏是我所有的配置文件,面向未來面臨同樣問題的任何人;

的WebPack:

'use strict'; 

//https://webpack.github.io/docs/configuration.html 

var webpack = require('webpack'); 
var HtmlWebpackPlugin = require('html-webpack-plugin'); 
var CopyWebpackPlugin = require('copy-webpack-plugin'); 
var path = require('path'); 
var rootPath = __dirname; 
var srcPath = path.join(rootPath, 'src/client'); 
var distPath = path.join(rootPath, 'dist/client'); 

module.exports = 
{ 
    bail: true, 
    cache: false, 
    context: rootPath, 
    debug: true, 
    devtool: 'source-map', //inline-source-map, https://webpack.github.io/docs/configuration.html#devtool 
    target: 'web', //node, web 
    devServer: 
    { 
     contentBase: distPath, 
     historyApiFallback: true, 
     outputPath: path.join(distPath, 'devServer') 
    }, 
    entry: 
    { 
     app: path.join(srcPath, 'app/home.jsx'), 
     lib: ['react', 'react-router', 'react-dom', 'jquery', 'lodash', 'history'] 
    }, 
    output: 
    { 
     path: distPath, 
     publicPath: '', 
     filename: '[name].js', 
     pathInfo: true 
    }, 
    resolve: 
    { 
     root: srcPath, 
     extensions: ['', '.js', '.jsx', '.ts', '.tsx'], 
     modulesDirectories: ['node_modules', srcPath, 'typings'] 
    }, 
    module: 
    { 
     loaders: 
     [ 
      {test: /\.js$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|typings)/ }, 
      {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|typings)/ }, 
      {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules|typings)/ }, 
      {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules|typings)/ }, 
      {test: /\.scss$/, loaders: ['style', 'css', 'sass']}, 
      {test: /\.png$/, loader: 'file-loader'}, 
      {test: /\.jpg$/, loader: 'file-loader'}, 
      {test: /\.jpeg$/, loader: 'file-loader'}, 
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'}, 
      {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"}, 
      {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"}, 
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"}, 
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"} 
     ] 
    }, 
    plugins: 
    [ 
     new CopyWebpackPlugin 
     ([ 
      { from: path.join(srcPath, 'images'), to: 'images' } 
     ]), 
     new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js'), 
     new HtmlWebpackPlugin 
     ({ 
      inject: true, 
      template: path.join(srcPath, 'index.html') 
     }), 
     new webpack.NoErrorsPlugin() 
    ] 
}; 

.babelrc:

{ 
    "presets": ["es2015", "react"] 
} 

的package.json

{ 
    "name": "ekaya", 
    "private": "true", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "dependencies": 
    { 
     "jquery": "2.2.3", 
     "lodash": "4.11.1", 
     "font-awesome": "4.6.1", 
     "history": "2.1.1", 
     "react": "15.0.2", 
     "react-dom": "15.0.2", 
     "react-router": "2.4.0", 

     "compression": "1.0.3", 
     "cors": "2.5.2", 
     "helmet": "1.3.0", 
     "loopback": "2.22.0", 
     "loopback-boot": "2.6.5", 
     "loopback-component-explorer": "2.4.0", 
     "loopback-datasource-juggler": "2.39.0", 
     "serve-favicon": "2.0.1" 
    }, 
    "devDependencies": 
    { 
    "node-sass": "3.7.0", 
    "nsp": "2.1.0", 
    "babel-core": "6.8.0", 
    "babel-loader": "6.2.4", 
    "babel-preset-es2015": "6.6.0", 
    "babel-preset-react": "6.5.0", 
    "css-loader": "0.23.1", 
    "file-loader": "0.8.5", 
    "jsx-loader": "0.13.2", 
    "font-awesome": "4.6.1", 
    "copy-webpack-plugin": "2.1.3", 
    "html-webpack-plugin": "2.16.1", 
    "sass-loader": "3.2.0", 
    "style-loader": "0.13.1", 
    "ts-loader": "0.8.2", 
    "typescript-loader": "1.1.3", 
    "typescript": "1.8.10", 
    "typings": "0.8.1", 
    "webpack": "1.13.0", 
    "webpack-dev-server": "1.14.1" 
    }, 
    "scripts": 
    { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "build": "./node_modules/.bin/webpack --progress --colors", 
    "run": "./node_modules/.bin/webpack-dev-server --hot --inline --progress --colors --watch" 
    }, 
    "repository": 
    { 
    "type": "git", 
    "url": "" 
    }, 
    "config": 
    { 
    "port": "8080" 
    }, 
    "author": "", 
    "license": "private", 
    "homepage": "" 
} 

tsconfig.json:

{ 
    "buildOnSave": false, 
    "compileOnSave": false, 
    "compilerOptions": 
    { 
     "allowJs": true, 
     "jsx": "react", 
     "noImplicitAny": true, 
     "module": "commonjs", 
     "outDir": "dist/client/ts", 
     "removeComments": true, 
     "sourceMap": false, 
     "target": "es6" 
    }, 
    "exclude": 
    [ 
     "node_modules", 
     "dist", 
     "typings" 
    ] 
} 

typings.json

{ 
    "ambientDependencies": { 
    "jquery": "registry:dt/jquery#1.10.0+20160417213236", 
    "lodash": "registry:dt/lodash#3.10.0+20160330154726", 
    "react": "registry:dt/react#0.14.0+20160423065914", 
    "react-dom": "registry:dt/react-dom#0.14.0+20160412154040" 
    }, 
    "dependencies": {} 
} 

我也建議您刪除編譯輸出(我的 'DIST' 文件夾)和重建,使用的WebPack devServer。

而且由於一些反應路由器的歷史分型的似乎不工作,自己寫:

declare module 'history/lib/createBrowserHistory' { 
    const x: any; 
    export = x; 
} 

declare module 'react-router' { 
    const x: any; 
    export = x; 
} 
11

我有同樣的問題。你安裝了ES6。導入失敗而不是它。

Babel file is copied without being transformed

編輯:

默認情況下,巴貝爾6.x中不執行任何轉換。你需要告訴它要運行什麼轉變:

npm install babel-preset-es2015 

和運行

babel --presets es2015 proxy.js --out-file proxified.js 

或創建。含babelrc文件

{ 
    "presets": [ 
     "es2015" 
    ] 
} 
+0

@armatita你實現了一個最小的答覆,實際上提供了新的信息總比沒有好,對吧?因爲我是唯一回答的人。這不是一個鏈接唯一的答案。 – DoktorDooLittle

+0

DoktorDooLittle我沒有downvote你的答案(如果這是你所設想的)。當時我做了評論(並且注意到它出現在評論中可能是因爲有人將其標記爲低質量),您的回答不符合SO標準。因此評論建議(這是一個預先格式化的文本)。考慮到附加信息,我很樂意刪除我的評論評論。無論如何,似乎@理查德找到了一個解決方案。 – armatita

+0

@armatita無論如何。我確實假設,對此感到遺憾。是的,他無意中偶然發現了這個解決方案。我完全用下一個Google的回答來回答這個問題。 – DoktorDooLittle

4

我遇到同樣的問題,並發現我的文件結構是問題:

模塊只能從相同或更低的水平爲切入點,在webpack.config.js配置進口在module.exports.entry,即:

module.exports = { 
    entry: path.resolve(__dirname, 'javascripts', 'main.js') 
} 

我試圖從更高的層次導入語言環境

├── javascripts 
│ └── main.js 
└── locales 
    ├── de.js 
    └── en.js 

移動區域設置目錄後,導入工作:

└── javascripts 
    ├── locales 
    │ ├── de.js 
    │ └── en.js 
    └── main.js 
2

如果你下面就https://webpack.js.org指導,你可能不知道該網站只記錄的WebPack 2或更高版本,不Webpack 1. Webpack 2的一個新功能是它具有本地ES6 import,exportSystem.import

你需要安裝的WebPack 2第一:

npm install --save-dev [email protected] 

如果你想看到所有的WebPack版本的列表,請運行:

npm show webpack versions --json 
相關問題