【问题标题】:Typescript Module + Webpack + RequireJS打字稿模块 + Webpack + RequireJS
【发布时间】:2017-08-14 22:34:24
【问题描述】:

正在开发我的第一个 typescript 模块,我希望能够在 Apache Cordova 应用程序和 .Net 4.6 MVC 站点(目前)中使用该模块。经过详尽的阅读后,我得出的结论是 CommonJS 最适合我的需求。但是我正在努力将模块集成到 MVC 站点中。

我使用 webpack 和 awesome-typescript-loader 将我的模块捆绑到一个 js 文件中,然后我使用 requirejs 将模块加载到 MVC 站点上。我尝试了多种方法来导入模块,但均未成功。

在我的 typescript 模块中,我有一个入口点 (index.ts),我只导出主类:export * from './src/engine';,并且我在 webpack 中使用这个类作为我的入口点:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = false;
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.ts', '.tsx'] },
        output: {
            filename: '[name].js',
            publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            loaders: [
                {
                    test: /\.tsx?$/,
                    loader: 'awesome-typescript-loader'
                }
            ]
        },
        plugins: [
            new CheckerPlugin()
        ]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'engine': './index.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.ContextReplacementPlugin(/moment[\\\/]locale$/, /^\.\/(en)$/)
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                new webpack.optimize.UglifyJsPlugin()
            ])
    });

    return [clientBundleConfig];
};

一旦我有了生成的 engine.js 文件,我将在 MVC 站点上使用 requirejs 来加载模块:

<script data-main="/scripts/main" src="~/Scripts/require.js"></script>

main.js

require(['/scripts/compliance-engine.js']);

最终目标是我可以从 javascript 调用 var compliance = require('compliance-engine');,实例化一个新的引擎实例。

遗憾的是,无论我尝试什么,我都无法完成上述工作(在任何时候都不会引发错误,我只是无法在网站上引用该模块)。

【问题讨论】:

    标签: typescript webpack requirejs


    【解决方案1】:

    使用webpack 时,除非您明确定义,否则不会导出任何内容,您应该将其添加到您的 webpack.config.js 中

    output: {
     library: 'MyLibrary'
     libraryTarget: 'var',
    }
    

    这将在窗口上放置一个名为MyLibrary 的变量,并且可以从您的其他代码中访问它。 libraryTarget 还有其他选项,例如 umd/amd 等。 你可以阅读它here

    【讨论】:

    • 你先生是神,这正是问题所在!非常感谢
    猜你喜欢
    • 2016-04-08
    • 2014-06-26
    • 2014-01-23
    • 1970-01-01
    • 2020-11-22
    • 2023-03-29
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多