Skip to content

Commit 326d0e2

Browse files
committed
docs update
1 parent 7ce27f4 commit 326d0e2

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# LP-Loader for Webpack
22

3-
> Frictionless *Language Packs* for apps loaded and bundled with Webpack. Keeping and loading any kind of dictionary data dynamically on demand in pure, conscious and flexible way.
3+
> Frictionless *Language Packs* for apps loaded and bundled with Webpack. Pure, conscious and flexible way of keeping and loading any kind of dictionary data dynamically on demand.
44
55
*Works with Webpack 3 and 4.*
66

77
## Why
88

9-
Because all the methods of i18n of JS frontend apps (particularly loading/delivering language-specific, usually ui-related data) I've manage to find out there - just ~~suck~~ could not suit my really basic requirements. Feels like **no one knows how to make it right.** So, this package is a try to approach the problem in a bit opinionated but at the same time simple, flexible and general way. This approach also turned to be useful for loading any kind of dictionary-like data sets.
9+
Webpack is really a great Packager Manager and Bundler, despite all its caveats (mostly related to configuration and performance) it really gives has much more capabilities and puts less limitations on the user than any other bunder I've tried. But some (quite basic) higher level problems seem do be not solved even there.
10+
11+
All the methods of i18n of JS frontend apps (particularly loading/delivering language-specific usually UI-related data) I've seen out there - just ~~suck~~ could not satisfy my really basic requirements (which are mostly - simplicity and flexibility). Sounds strange, but feels like **no one knows how to make it right.** So, this package trying to approach the problem in a bit opinionated but at the same time flexible and generalized way. And this approach turned to be also useful for loading any kind of dictionary-like data sets.
1012

1113
*Note that LP-Loader is still experimental and may probably fail to handle some particular cases (esp. if not aligned with proposed principles). But the concept has been proven and battle tested.*
1214

@@ -116,27 +118,25 @@ getDict(lang).then(dict => console.log(dict.name))
116118

117119
[*You may want to look at the example app's code.*](./app)
118120

119-
# Configuration
121+
## Configuration
120122

121-
To get this code working in `webpack.config` you should define loader for dictionary index files:
123+
To get this code working, in `webpack.config` you should define loader for dictionary index files, just like that:
122124

123125
```ts
124126
{
125127
test: /dict(\\|\/)index\.ts/,
126128
loaders: [
127-
{ loader: 'lp-loader', options: { name: 'language.pack' } },
129+
{ loader: 'lp-loader' },
128130
]
129-
},
130-
{
131-
test: /\.ts$/,
132-
loader: 'ts-loader'
133-
},
131+
}
134132
```
135133

136134
[*There are example configs in corresponding webpackX folders*](./webpack4/webpack.config.ts). Actually no difference between Webpack 3 and 4.
137135

138136
## Loader options:
139137

138+
You may want to provide additional options for the loader to override the defaults.
139+
140140
```ts
141141
export interface LoaderOptions {
142142
/**
@@ -167,12 +167,16 @@ export interface LoaderOptions {
167167
include?: RegExp | ((filePath: string) => boolean),
168168
/**
169169
*
170-
* Do not consider folders as labeled data. By default `false`
170+
* Do not consider folders as labeled dictionary data. By default `false`
171171
*/
172172
excludeFolders?: boolean
173173
}
174174
```
175175

176+
## Caveats
177+
178+
There are currently some problems with created bundle's names due to the problem that chunk names are not always available during the Webpack build. But this probably is going to be solved somehow.
179+
176180
## How it works
177181

178182
It is quite simple. Loader finds chunk parents of the dictionary index, and generates special code that allow loading requested data on demand via `Promise` based API.

src/lp-loader.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ export interface LoaderOptions {
3636
*/
3737
include?: RegExp | ((filePath: string) => boolean),
3838
/**
39-
*
40-
* Do not consider folders as labeled data. By default `false`
41-
*/
42-
excludeFolders?: boolean
39+
*
40+
* Do not consider folders as labeled dictionary data. By default `false`
41+
*/
42+
excludeFolders?: boolean,
43+
/**
44+
*
45+
* Output some debug data
46+
*/
47+
debug?: boolean
4348
}
4449

4550
// https://webpack.js.org/api/stats/#module-objects
@@ -137,8 +142,9 @@ function getOptions(context: LoaderContext): LoaderOptions {
137142
}
138143

139144
module.exports = function (this: LoaderContext, source: string) {
145+
const options = getOptions(this)
140146
const parentChunks = findChunkParents(this._module)
141-
if (process.env.LP_DEBUG) {
147+
if (process.env.LP_DEBUG || options.debug) {
142148
console.log('LP-LOADER:', this._module.context)
143149
console.log('Static parents: ', this._module.context, parentChunks
144150
.filter(uniq)

webpack4/webpack.config.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ export const makeConfig = (isProduction = false) => {
1313
path.join(__dirname, '../app/app')
1414
]
1515

16-
isDevelopment && appEntry.push(
17-
//'webpack-dev-server/client?http://localhost:' + process.env.PORT,
18-
//'webpack/hot/only-dev-server'
19-
)
20-
2116
const buildDir = path.join(__dirname, 'build')
2217
const tsLoaderOptions = {
2318
compilerOptions: {
@@ -27,6 +22,7 @@ export const makeConfig = (isProduction = false) => {
2722
}
2823
}
2924
const lpTsIndexFiles = /dict(\\|\/)index\.ts/
25+
const lpLoader = path.join(__dirname, '../src/lp-loader')
3026
const config: webpack.Configuration = {
3127
//mode: 'development',
3228
entry: {
@@ -35,16 +31,18 @@ export const makeConfig = (isProduction = false) => {
3531
output: {
3632
path: buildDir,
3733
filename: '[name]-[hash].js',
38-
//-[chunkhash]
3934
chunkFilename: '[name]-[chunkhash].js',
4035
publicPath: '/'
4136
},
4237
plugins: [
43-
new CleanWebpackPlugin([buildDir], {
44-
root: path.resolve(__dirname, '../..')
45-
}),
46-
new (webpack as any).NamedModulesPlugin(),
47-
new webpack.HotModuleReplacementPlugin(),
38+
//new (webpack as any).NamedModulesPlugin(),
39+
...isDevelopment ? [
40+
new webpack.HotModuleReplacementPlugin()
41+
] : [
42+
new CleanWebpackPlugin([buildDir], {
43+
root: path.resolve(__dirname, '../..')
44+
})
45+
],
4846
new HtmlWebpackPlugin({
4947
title: 'LP-Loader ' + (isDevelopment ? 'Dev' : 'Build'),
5048
filename: 'index.html'
@@ -55,11 +53,11 @@ export const makeConfig = (isProduction = false) => {
5553
{
5654
test: lpTsIndexFiles, loaders: [
5755
{
58-
loader: path.join(__dirname, '../src/lp-loader'), query: {
56+
loader: lpLoader, query: {
5957
name: 'language.pack',
6058
include: /(\\|\/)\w{2}\.ts/
6159
} as LoaderOptions
62-
} as any,
60+
},
6361
//{ loader: 'ts-loader', query: tsLoaderOptions } as any
6462
//{ loader: path.join(__dirname, 'ts-simple-loader'), query: tsLoaderOptions } as any
6563
]
@@ -73,14 +71,14 @@ export const makeConfig = (isProduction = false) => {
7371
{ test: /\.css$/, loader: 'style!css' },
7472
]
7573
},
76-
devtool: 'eval',
74+
devtool: isDevelopment ? 'eval' : 'inline-source-map',
7775
resolve: {
7876
extensions: ['.ts', '.js'],
7977
alias: {
8078
//'lp': path.resolve(__dirname, '../../lib/lp-loader.ts'),
8179
}
8280
},
83-
resolveLoader: <any>{
81+
resolveLoader: {
8482
extensions: ['.ts', '.js'],
8583
alias: {
8684
//'ts': 'awesome-typescript-loader'

0 commit comments

Comments
 (0)