magic-comments-loader
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -58,2 +58,8 @@ "use strict"; | ||
if (value === false) { | ||
return { ...defaultConfig, | ||
active: false | ||
}; | ||
} | ||
if (Array.isArray(value) || typeof value === 'string') { | ||
@@ -60,0 +66,0 @@ return { ...defaultConfig, |
@@ -10,3 +10,3 @@ "use strict"; | ||
const getCommenter = (filepath, options) => (rgxMatch, capturedImportPath) => { | ||
const getCommenter = (filepath, options, logger = console) => (rgxMatch, capturedImportPath) => { | ||
const importPath = capturedImportPath.trim(); | ||
@@ -31,4 +31,3 @@ const bareImportPath = importPath.replace(/['"`]/g, ''); | ||
if (verbose) { | ||
// eslint-disable-next-line no-console | ||
console.log('\x1b[32m%s\x1b[0m', '[MCL]', `${filepath} : ${magicImport}`); | ||
logger.info('[MCL]', `${filepath} : ${magicImport}`); | ||
} | ||
@@ -35,0 +34,0 @@ |
@@ -16,3 +16,3 @@ "use strict"; | ||
const dynamicImportsWithoutComments = /(?<!\w|\*[\s\w]*?|\/\/\s*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`].+['"`]\s*)\)/g; | ||
const dynamicImportsWithoutComments = /(?<![\w.]|#!|\*[\s\w]*?|\/\/\s*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?![\s]*?\*\/)/g; | ||
@@ -22,2 +22,3 @@ const loader = function (source, map, meta) { | ||
const optionKeys = Object.keys(options); | ||
const logger = this.getLogger('MCL'); | ||
(0, _schemaUtils.validate)(_schema.schema, options, { | ||
@@ -30,3 +31,3 @@ name: 'magic-comments-loader' | ||
webpackChunkName: true | ||
}); | ||
}, logger); | ||
this.callback(null, source.replace(dynamicImportsWithoutComments, magicComments), map, meta); | ||
@@ -33,0 +34,0 @@ }; |
{ | ||
"name": "magic-comments-loader", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Add webpack magic comments to your dynamic imports during build time", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
"lint": "eslint . src __tests__ --ext .js,.cjs", | ||
"lint:fix": "npm run lint -- --fix", | ||
"test": "node --experimental-vm-modules ./node_modules/.bin/jest" | ||
@@ -21,0 +22,0 @@ }, |
@@ -22,3 +22,7 @@ "use strict"; | ||
mode: { | ||
instanceof: 'Function' | ||
oneOf: [{ | ||
enum: validModes | ||
}, { | ||
instanceof: 'Function' | ||
}] | ||
} | ||
@@ -48,3 +52,3 @@ }, | ||
active: true, | ||
mode: () => 'lazy' | ||
mode: 'lazy' | ||
}; | ||
@@ -57,5 +61,11 @@ | ||
if (value === false) { | ||
return { ...defaultConfig, | ||
active: false | ||
}; | ||
} | ||
if (typeof value === 'string') { | ||
return { ...defaultConfig, | ||
mode: () => value, | ||
mode: value, | ||
active: validModes.includes(value) | ||
@@ -83,18 +93,25 @@ }; | ||
const webpackMode = (filepath, importPath, value) => { | ||
let mode = ''; | ||
const config = getConfig(value, filepath); | ||
const isActive = typeof config.active === 'function' ? config.active(filepath, importPath) : config.active; | ||
if (!isActive || typeof config.mode !== 'function') { | ||
if (!isActive) { | ||
return ''; | ||
} | ||
const configMode = config.mode(filepath, importPath); | ||
if (typeof config.mode === 'function') { | ||
mode = config.mode(filepath, importPath); | ||
} | ||
if (!validModes.includes(configMode)) { | ||
if (typeof config.mode === 'string') { | ||
mode = config.mode; | ||
} | ||
if (!validModes.includes(mode)) { | ||
return ''; | ||
} | ||
return `webpackMode: "${configMode}"`; | ||
return `webpackMode: "${mode}"`; | ||
}; | ||
exports.webpackMode = webpackMode; |
{ | ||
"name": "magic-comments-loader", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Add webpack magic comments to your dynamic imports during build time", | ||
@@ -20,2 +20,3 @@ "main": "dist", | ||
"lint": "eslint . src __tests__ --ext .js,.cjs", | ||
"lint:fix": "npm run lint -- --fix", | ||
"test": "node --experimental-vm-modules ./node_modules/.bin/jest" | ||
@@ -22,0 +23,0 @@ }, |
# [`magic-comments-loader`](https://www.npmjs.com/package/magic-comments-loader) | ||
Adds [magic coments](https://webpack.js.org/api/module-methods/#magic-comments) to your dynamic `import()` statements. | ||
Keep your source code clean, add [magic coments](https://webpack.js.org/api/module-methods/#magic-comments) to your dynamic `import()` statements at build time. | ||
@@ -19,2 +19,22 @@ NOTE: **This loader ignores dynamic imports that already include comments of any kind**. | ||
Try *not* to have dynamic `import()` statements behind [comments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#comments). **It is better to remove unused code in production**. | ||
If you must, e.g. your comment is referencing usage of dynamic imports, then these styles are ok: | ||
```js | ||
// import('some-ignored-module') | ||
/* Some comment about a dynamic import('module') */ | ||
``` | ||
If you must have a multiline comment then this style is ok (only one `import()` per comment line preceded by an asterisk): | ||
```js | ||
/** | ||
* Comment about import('module/one') | ||
* Comment about import('module/two') | ||
* import('module/three'), etc. | ||
*/ | ||
``` | ||
This module uses a [RegExp](https://github.com/morganney/magic-comments-loader/blob/master/src/loader.js#L8) not a parser. If you would like to add better support for ignoring `import()` behind multiline comments please open a pull request. See some more [examples on regexr](https://regexr.com/65fg0). | ||
### Configuration | ||
@@ -56,4 +76,7 @@ | ||
webpackIgnore: 'src/ignore/**/*.js', | ||
webpackPreload: ['src/preload/**/*.js', '!src/preload/skip/**/*.js'], | ||
webpackPrefetch: 'src/prefetch/**/*.js' | ||
webpackPreload: [ | ||
'src/preload/**/*.js', | ||
'!src/preload/skip/**/*.js' | ||
] | ||
} | ||
@@ -112,8 +135,10 @@ } | ||
{ | ||
// Can be an array of globs too | ||
files: 'src/**/*.js', | ||
files: ['src/**/*.js'], | ||
config: { | ||
active: true, | ||
exports: ['foo', 'bar'] | ||
// Etc. | ||
mode: (modulePath, importPath) => { | ||
if (/eager/.test(importPath)) { | ||
return 'eager' | ||
} | ||
} | ||
} | ||
@@ -124,4 +149,2 @@ } | ||
**The `config.match` in an override is ignored. You can only have one, top-level `config.match`**. | ||
Here's a more complete example using `config` and `overrides` to customize how comments are applied: | ||
@@ -158,3 +181,6 @@ | ||
}, | ||
webpackPrefetch: ['src/prefetch/**/*.js', '!src/prefetch/skip/**/*.js'], | ||
webpackPrefetch: [ | ||
'src/prefetch/**/*.js', | ||
'!src/prefetch/skip/**/*.js' | ||
], | ||
webpackMode: { | ||
@@ -218,3 +244,3 @@ config: { | ||
These are the options that can be configured under the loader `options`. When using comments with a [`config`](#with-config-options) key, you may also specify [`overrides`](#overrides)(`config.match` is ignored inside overrides). | ||
These are the options that can be configured under the loader `options`. When using comments with a [`config`](#with-config-options) key, you may also specify [`overrides`](#overrides). | ||
@@ -254,3 +280,3 @@ * `verbose`: Boolean. Prints console statements of the module filepath and updated `import()` during the webpack build. Useful for debugging your custom configurations. | ||
* `config.active`: Boolean | `(modulePath, importPath) => Boolean`. Returning `false` does not add the comment. | ||
* `config.mode`: `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip. | ||
* `config.mode`: `String(lazy|lazy-once|eager|weak)` | `(modulePath, importPath) => String(lazy|lazy-once|eager|weak)`. Return falsy value to skip. | ||
* `webpackExports` | ||
@@ -257,0 +283,0 @@ * `Function`: `(modulePath, importPath) => [String(<module names|default>)]`. Return falsy value to skip. |
29061
578
279