Skip to content

Commit

Permalink
Merge pull request #330 from ryoppippi/feature/webpack
Browse files Browse the repository at this point in the history
simplify webpack config
  • Loading branch information
ryoppippi authored Sep 18, 2024
2 parents ca1f37b + 34d1770 commit d498a5e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 81 deletions.
95 changes: 43 additions & 52 deletions examples/webpack-minimal/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,53 @@ import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import WorkboxWebpackPlugin from 'workbox-webpack-plugin';
import { Configuration } from 'webpack';
import * as tsx from 'tsx/cjs/api'

const {default: UnpluginTypia} = tsx.require('../../packages/unplugin-typia/src/webpack.ts', __filename)
import { tsImport } from 'tsx/esm/api'

const isProduction = process.env.NODE_ENV == 'production';

module.exports = async () => {
/** we use tsImport because of development. However, in real-world projecct, just use `import` instead */
// const { default: UnpluginTypia } = await import('@ryoppippi/unplugin-typia/webpack');
const { default: UnpluginTypia } = await tsImport('@ryoppippi/unplugin-typia/webpack', __dirname);

const config: Configuration = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
},
devServer: {
open: true,
host: 'localhost',
},
plugins: [
new HtmlWebpackPlugin({
template: 'index.html',
}),
UnpluginTypia()
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
},

// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
return {
mode: isProduction ? 'production' : 'development',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
},
devServer: {
open: true,
host: 'localhost',
},
plugins: [
new HtmlWebpackPlugin({
template: 'index.html',
}),
UnpluginTypia(),
isProduction && new WorkboxWebpackPlugin.GenerateSW(),
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
},
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
},
};
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
},

module.exports = () => {
if (isProduction) {
config.mode = 'production';

if (config.plugins == null) {
config.plugins = []
}
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());

} else {
config.mode = 'development';
}
return config;
// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
},
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
},
} as const satisfies Configuration;
};

28 changes: 11 additions & 17 deletions packages/unplugin-typia/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,22 @@ Examples:

> ⚠️ Note: Currently, this plugin works only with 'esm' target.
> If you want to use 'cjs' target on Node < 20.17.0 , please use with [`jiti`](https://github.com/unjs/jiti).
> If you want to use 'cjs' target on Node >= 20.17.0, please use with `require` and enable [`--experimental-require-modules` flag](https://github.com/nodejs/node/pull/51977).
> If you want to use 'esm' target, don't worry! You can use this plugin without any additional setup.
> If you want to use 'cjs', use dynamic import.
```sh
npm install jiti
```
> If you want to use 'esm' target, don't worry! You can use this plugin without any additional setup.
```js
// webpack.config.js

// if you use Node < 20.17.0
const jiti = require('jiti')(__filename);
const { default: UnpluginTypia } = jiti('@ryoppippi/unplugin-typia/webpack');

// if you use Node >= 20.17.0
// const { default: UnpluginTypia } = require("@ryoppippi/unplugin-typia/webpack");

module.exports = {
plugins: [
UnpluginTypia({ /* options */ }),
],
module.exports = async () => {
const { default: UnpluginTypia } = await import('@ryoppippi/unplugin-typia/webpack');
return {
plugins: [
new UnpluginTypia({
// options
}),
],
};
};
```

Expand Down
23 changes: 11 additions & 12 deletions packages/unplugin-typia/src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import unplugin from './core/index.js';
*
* Currently, this plugin works only with 'esm' target.
*
* If you want to use 'cjs' target on Node < 20.17.0 , please use with [`jiti`](https://github.com/unjs/jiti).
* If you want to use 'cjs' target on Node >= 20.17.0, please use with `require` and enable [`--experimental-require-modules` flag](https://github.com/nodejs/node/pull/51977).
* If you want to use 'cjs' use dynamic import
* If you want to use 'esm' target, don't worry! You can use this plugin without any additional setup.
*
* Refer this issue https://github.com/samchon/typia/issues/1094
Expand All @@ -21,16 +20,16 @@ import unplugin from './core/index.js';
* ```js
* // webpack.config.js
*
* // if you use Node < 20.17.0
* const jiti = require("jiti")(__filename);
* const { default: UnpluginTypia } = jiti("@ryoppippi/unplugin-typia/webpack");
*
* // if you use Node >= 20.17.0
* const { default: UnpluginTypia } = require("@ryoppippi/unplugin-typia/webpack");
*
* module.exports = {
* plugins: [UnpluginTypia({ /* your config *\/ })],
* }
* module.exports = async () => {
* const { default: UnpluginTypia } = await import('@ryoppippi/unplugin-typia/webpack');
* return {
* plugins: [
* new UnpluginTypia({
* // options
* }),
* ],
* };
* };
* ```
*
*/
Expand Down

0 comments on commit d498a5e

Please sign in to comment.