diff --git a/Documentation/config.js b/Documentation/config.js index d183b5188c4..9a852b69f36 100644 --- a/Documentation/config.js +++ b/Documentation/config.js @@ -1,5 +1,3 @@ -const path = require('path'); - /* eslint-disable no-template-curly-in-string */ /* eslint-disable no-useless-escape */ @@ -7,7 +5,6 @@ module.exports = { baseUrl: '/vtk-js', work: './build-tmp', api: ['../Sources'], - examples: [{ path: '../Examples', regexp: 'index.js' }, '../Sources'], config: { title: 'vtk.js', description: '"Visualization Toolkit for the Web."', @@ -20,66 +17,5 @@ module.exports = { google_analytics: 'UA-90338862-1', google_analytics_4: 'G-5XH2Z0Y9LQ', }, - parallelWebpack: { - maxConcurrentWorkers: 2, - rootPath: path.resolve(path.join(__dirname, '..')), - templatePath: path.resolve( - path.join(__dirname, '../Utilities/ExampleRunner/template.html') - ), - output: { - publicPath: '', - }, - plugins: [], - rules: [ - ` - { - test: /\\.js$/, - exclude: /dist\\/esm/, - use: [ - { - loader: 'babel-loader?cacheDirectory', - }, - ], - }, - { - test: /\.css$/, - exclude: /\.module\.css$/, - use: [ - { loader: 'style-loader' }, - { loader: 'css-loader' }, - { loader: 'postcss-loader' }, - ], - }, - { - test: /\.module\.css$/, - use: [ - { loader: 'style-loader' }, - { - loader: 'css-loader', - options: { - modules: { - localIdentName: '[name]-[local]_[sha512:hash:base64:5]', - }, - }, - }, - { loader: 'postcss-loader' }, - ], - }, - { - test: /\\.svg$/, - type: 'asset/source', - }, - { test: /\\.(png|jpg)$/, type: 'asset' }, - { test: /\\.html$/, loader: 'html-loader' }, - { test: /\\.cjson$/, loader: 'hson-loader' }, - { test: /\\.hson$/, loader: 'hson-loader' }, - `, - ], - alias: [ - "'vtk.js/Sources': `${rootPath}/dist/esm`,", - "'@kitware/vtk.js': `${rootPath}/dist/esm`,", - "'vtk.js': `${rootPath}`,", - ], - }, copy: [{ src: '../Data/*', dest: './build-tmp/public/data' }], }; diff --git a/Utilities/ExampleRunner/template.html b/Utilities/ExampleRunner/template.html index 1de1b92f7f0..c335bec3047 100644 --- a/Utilities/ExampleRunner/template.html +++ b/Utilities/ExampleRunner/template.html @@ -3,7 +3,6 @@ - diff --git a/webpack.examples.config.js b/webpack.examples.config.js new file mode 100644 index 00000000000..f59661678c9 --- /dev/null +++ b/webpack.examples.config.js @@ -0,0 +1,127 @@ +const path = require('path'); +const glob = require('glob'); +const WebpackHtmlPlugin = require('html-webpack-plugin'); + +const settings = require('./webpack.settings.js'); + +function configureVtkRules() { + return [ + { + test: /\.glsl$/i, + loader: 'shader-loader', + }, + { + test: /\.js$/, + include: path.resolve(__dirname, 'Sources'), + use: [ + { + loader: 'babel-loader', + options: { + presets: ['@babel/preset-env'], + }, + }, + ], + }, + { + test: /\.css$/, + exclude: /\.module\.css$/, + use: [ + { loader: 'style-loader' }, + { loader: 'css-loader' }, + { loader: 'postcss-loader' }, + ], + }, + { + test: /\.module\.css$/, + use: [ + { loader: 'style-loader' }, + { + loader: 'css-loader', + options: { + modules: { + localIdentName: '[name]-[local]_[sha512:hash:base64:5]', + }, + }, + }, + { loader: 'postcss-loader' }, + ], + }, + { + test: /\.svg$/, + type: 'asset/source', + }, + { + test: /\.worker\.js$/, + use: [{ loader: 'worker-loader', options: { inline: 'no-fallback' } }], + }, + ]; +} + +function configureEntries() { + const entries = {}; + + glob.sync('Examples/**/index.js').forEach((entry) => { + const name = path.dirname(entry); + entries[name] = path.resolve(__dirname, entry); + }); + + glob.sync('Sources/**/example/index.js').forEach((entry) => { + const name = path.dirname(path.dirname(entry)); + entries[name] = path.resolve(__dirname, entry); + }); + + return entries; +} + +function configureHtmlPlugins(entries) { + return Object.keys(entries).map((chunkName) => { + const entry = entries[chunkName]; + return new WebpackHtmlPlugin({ + filename: path.resolve( + __dirname, + settings.paths.examples.base, + `${chunkName}.html` + ), + template: path.join(__dirname, 'Utilities/ExampleRunner/template.html'), + chunks: [chunkName], + }); + }); +} + +const entries = configureEntries(); +const htmlPlugins = configureHtmlPlugins(entries); +const webpackConfig = { + mode: 'production', + entry: entries, + output: { + path: path.resolve(__dirname, settings.paths.examples.base), + filename: '[name].bundle.js', + }, + resolve: { + alias: { + '@kitware/vtk.js': path.resolve(__dirname, 'Sources'), + 'vtk.js': __dirname, + }, + fallback: { stream: require.resolve('stream-browserify') }, + }, + module: { + rules: [ + ...configureVtkRules(), + { + test: /\.html$/i, + loader: 'html-loader', + }, + { + test: /\.(png|jpg)$/i, + type: 'asset', + }, + { + test: /\.(cj|h)son$/i, + loader: 'hson-loader', + }, + ], + }, + plugins: [...htmlPlugins], +}; + +module.exports = webpackConfig; diff --git a/webpack.settings.js b/webpack.settings.js index ec9b814d4b9..36791812a6e 100644 --- a/webpack.settings.js +++ b/webpack.settings.js @@ -12,6 +12,9 @@ module.exports = { dist: { base: './dist/umd/', }, + examples: { + base: './dist/examples', + }, }, urls: { publicPath: '/dist/umd/',