【问题标题】:Minimal webpack setup for Aurelia without easy-webpack没有 easy-webpack 的 Aurelia 的最小 webpack 设置
【发布时间】:2017-06-07 12:44:20
【问题描述】:

我想设置一个支持 Aurelia 和 Babel(对于 ES2016)的 minimal webpack 配置。我想使用 easy-webpack 来实现这个 without (来自 aurelia 的官方骨架取决于 easy-webpack 但我想使用普通的webpack)。

知道如何正确设置最小的 webpack + aurelia + babel 吗?

【问题讨论】:

    标签: webpack aurelia


    【解决方案1】:

    2017 年 6 月 6 日更新: 此处提到的步骤已过时且无关紧要,因为官方框架已删除对 easy-webpack 的依赖。我保留这个只是出于历史原因。


    2016 年 11 月 9 日更新:有关此解决方案的更好版本,请参阅 thisthis


    好了,这里是基于Aurelia官方webpack骨架的完整流程。

    Aurelia github 下载skeleton-esnext-webpack 后,我们会将所有对@easy-webpack 的引用替换为标准的webpack 模块。

    package.json中,删除"devDependencies"中所有以@easy-webpack开头的模块:

        "@easy-webpack/config-aurelia": "^2.0.1",
        "@easy-webpack/config-babel": "^2.0.2",
        "@easy-webpack/config-common-chunks-simple": "^2.0.1",
        "@easy-webpack/config-copy-files": "^1.0.0",
        "@easy-webpack/config-css": "^2.3.2",
        "@easy-webpack/config-env-development": "^2.1.1",
        "@easy-webpack/config-env-production": "^2.1.0",
        "@easy-webpack/config-external-source-maps": "^2.0.1",
        "@easy-webpack/config-fonts-and-images": "^1.2.1",
        "@easy-webpack/config-generate-index-html": "^2.0.1",
        "@easy-webpack/config-global-bluebird": "^1.2.0",
        "@easy-webpack/config-global-jquery": "^1.2.0",
        "@easy-webpack/config-global-regenerator": "^1.2.0",
        "@easy-webpack/config-html": "^2.0.2",
        "@easy-webpack/config-json": "^2.0.2",
        "@easy-webpack/config-test-coverage-istanbul": "^2.0.2",
        "@easy-webpack/config-uglify": "^2.1.0",
        "@easy-webpack/core": "^1.3.2",
    

    并将它们替换为以下内容:

        "aurelia-webpack-plugin": "^1.1.0",
        "copy-webpack-plugin": "^3.0.1",
        "html-webpack-plugin": "^2.22.0",
        "babel-core": "^6.17.0",
        "babel-loader": "^6.2.5",
        "babel-polyfill": "^6.16.0",
        "css-loader": "^0.25.0",
        "file-loader": "^0.9.0",
        "html-loader": "^0.4.4",
        "sourcemap-istanbul-instrumenter-loader": "^0.2.0",
        "style-loader": "^0.13.1",
        "url-loader": "^0.5.7",
    

    并将以下内容用于webpack.config.js

    var path = require('path');
    var webpack = require('webpack');
    var HtmlWebpackPlugin = require('html-webpack-plugin');
    var CopyWebpackPlugin = require('copy-webpack-plugin');
    var AureliaWebpackPlugin = require('aurelia-webpack-plugin');
    var project = require('./package.json');
    
    const DEBUG = true;
    const title = 'Aurelia Navigation Skeleton';
    const baseUrl = '/';
    const rootDir = path.resolve();
    const srcDir = path.resolve('src');
    const outDir = path.resolve('dist');
    
    const aureliaBootstrap = [
        'aurelia-bootstrapper-webpack',
        'aurelia-polyfills',
        'aurelia-pal-browser',
        'regenerator-runtime',
    ];
    
    const aureliaModules = Object.keys(project.dependencies).filter(dep => dep.startsWith('aurelia-'));
    
    module.exports = {
        //debug: true,
        //devtool: 'source-map',
        entry: {
            'app': [], // <-- this array will be filled by the aurelia-webpack-plugin
            'aurelia-bootstrap': aureliaBootstrap,
            'aurelia-modules': aureliaModules.filter(pkg => aureliaBootstrap.indexOf(pkg) === -1)
        },
        output: {
            path: outDir,
            filename: '[name]-bundle.js'
        },
        module: {
            loaders: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/, // or include: path.resolve('src'),
                    loader: 'babel-loader',
                    query: {
                        presets: ['es2015', 'stage-1'],
                        plugins: ['transform-decorators-legacy']
                    }
                }, {
                    test: /\.html$/,
                    exclude: /index\.html$/,
                    loader: 'html-loader'
                }, {
                    test: /\.css$/,
                    loaders: ['style-loader', 'css-loader']
                }, {
                    test: /\.(png|jpe?g|gif|svg|eot|woff|woff2|ttf)(\?\S*)?$/,
                    loader: 'url-loader?limit=100000&name=[name].[ext]'
                }
            ]
        },
        plugins: [
            new webpack.ProvidePlugin({
                regeneratorRuntime: 'regenerator-runtime', // to support await/async syntax
                Promise: 'bluebird', // because Edge browser has slow native Promise object
                $: 'jquery', // because 'bootstrap' by Twitter depends on this
                jQuery: 'jquery', // just an alias
                'window.jQuery': 'jquery' // this doesn't expose jQuery property for window, but exposes it to every module
            }),
            new HtmlWebpackPlugin({
                title: title,
                template: 'index.html',
                chunksSortMode: 'dependency'
            }),
            new AureliaWebpackPlugin({
                root: rootDir,
                src: srcDir,
                title: title,
                baseUrl: baseUrl
            }),
            new CopyWebpackPlugin([{
                from: 'favicon.ico',
                to: 'favicon.ico'
            }]),
            new webpack.optimize.CommonsChunkPlugin({
                name: ['aurelia-modules', 'aurelia-bootstrap']
            }),
            /*new webpack.optimize.UglifyJsPlugin({
                beautify: DEBUG ? true : false,
                mangle: DEBUG ? false : {screw_ie8 : true, keep_fnames: true},
                dead_code: DEBUG ? false : true,
                unused: DEBUG ? false : true,
                deadCode: DEBUG ? false : true,
                comments: DEBUG ? true : false,
                compress: {
                    screw_ie8: true,
                    keep_fnames: true,
                    drop_debugger: false,
                    dead_code: false,
                    unused: false,
                    warnings: DEBUG ? true : false
                }
            }),*/
        ]
    };
    

    现在index.html需要稍微调整一下:

    <!DOCTYPE html>
    <html>
      <head>
        <title><%- htmlWebpackPlugin.options.title %></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <base href="<%- htmlWebpackPlugin.options.baseUrl %>">
        <!-- imported CSS are concatenated and added automatically -->
      </head>
      <body aurelia-app="main">
        <div class="splash">
          <div class="message"><%- htmlWebpackPlugin.options.title %></div>
          <i class="fa fa-spinner fa-spin"></i>
        </div>
        <% if (webpackConfig.debug) { %>
        <!-- Webpack Dev Server reload -->
        <script src="/webpack-dev-server.js"></script>
        <% } %>
      </body>
    </html>
    

    完成这些步骤后,就可以正常运行npm install,运行npm start

    希望这将帮助那些想要使用 standard webpack 而不是 @easy-webpack 的人。

    【讨论】:

    • 这些说明似乎不再有效。如果骨架包含一个非常小的 Webpack 启动器,那就太好了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2017-09-18
    • 1970-01-01
    • 2023-03-02
    • 2017-09-30
    • 2016-07-01
    相关资源
    最近更新 更多