首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将模块文件/流的内容读取到BLOB中

将模块文件/流的内容读取到BLOB中
EN

Stack Overflow用户
提问于 2021-09-11 17:42:56
回答 1查看 109关注 0票数 1

我创建一个BLOB文件并在其中编写JavaScipt代码,然后创建一个URL并从中导入模块。

代码语言:javascript
复制
const myJSFile = new Blob( [ 'export default true;' ], { type: 'application/javascript' } );
const myJSURL = URL.createObjectURL( myJSFile );
import( myJSURL ).then(async ( module ) => {
    console.log( module.default );
});

这在浏览器控制台中工作得很好。然而,我在使用Webpack构建项目时遇到了一个问题。

我怀疑问题出在WebPack或Babel的配置上。

Webpack常用配置:

代码语言:javascript
复制
const path = require('path');

const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
    // Where webpack looks to start building the bundle
    entry: [
        'core-js/modules/es6.promise',
        'core-js/modules/es6.array.iterator',
        './src/main.js',
    ],
    target: 'web',
    // Where webpack outputs the assets and bundles
    output: {
        path: path.resolve(__dirname, 'dist'),
        assetModuleFilename: '[name].[contenthash].[ext]',
        filename: '[name].[contenthash].bundle.js',
        chunkFilename: '[id].[chunkhash].bundle.js',
        // publicPath: '/',
    },

    // Determine how modules within the project are treated
    module: {
        rules: [
            {
                test: /\.(gif|png|jpe?g)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'assets/images/'
                        }
                    }
                ]
            },

            // JavaScript: Use Babel to transpile JavaScript files
            { test: /\.js$/, use: ['babel-loader'] },

            // Images: Copy image files to build folder
            { test: /\.(?:ico|gif|png|jpg|jpeg)$/i, type: 'asset/resource' },

            // Fonts and SVGs: Inline files
            { test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/inline' },
        ],
    },

    // Customize the webpack build process
    plugins: [
        // Generates an HTML file from a template
        new HtmlWebpackPlugin({
            // template: path.resolve(__dirname, 'src/index.html'), // шаблон
            template: 'src/index.html',
            // filename: 'index.html', // название выходного файла
            // inject: false, // true, 'head'
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true,
                removeStyleLinkTypeAttributes: true,
                keepClosingSlash: true,
                minifyJS: true,
                minifyCSS: true,
                minifyURLs: true
            },
            // chunks: 'all',
            // excludeChunks: [],
        }),


        new CopyWebpackPlugin(
            {
                patterns: [
                    { from: 'src/assets', to: 'assets' },
                    // { from: 'src/components', to: 'components' },
                ]
            }
        ),
    ],

    resolve: {
        // modules: [path.resolve(__dirname, 'src'), 'node_modules'],
        extensions: ['.js', '.jsx', '.json', '.ts'],
        alias: {
            '@': [
                path.resolve(__dirname, 'src'),
                './src/main.js'
            ],
        },
    },
}

Babel配置

代码语言:javascript
复制
module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          esmodules: true,
        },
      },
    ],
  ],
  plugins: [
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-transform-runtime',
    "@babel/plugin-syntax-dynamic-import"
  ]
};
EN

回答 1

Stack Overflow用户

发布于 2021-09-11 21:01:03

我以前使用过uses the native Module module内部的require-from-string来实现这一点。

一般来说,Blobs在浏览器和节点之间并不能很好地互操作,并且模块也没有被同等对待。所以blob+module有问题也就不足为奇了。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69145375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档